PromiseUtilsTests.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. const { expect } = require('chai')
  2. const {
  3. promisifyAll,
  4. promisifyClass,
  5. callbackifyMultiResult,
  6. callbackifyClass,
  7. callbackifyAll,
  8. expressify,
  9. expressifyErrorHandler,
  10. } = require('../..')
  11. describe('promisifyAll', function () {
  12. describe('basic functionality', function () {
  13. before(function () {
  14. this.module = {
  15. SOME_CONSTANT: 1,
  16. asyncAdd(a, b, callback) {
  17. callback(null, a + b)
  18. },
  19. asyncDouble(x, callback) {
  20. this.asyncAdd(x, x, callback)
  21. },
  22. }
  23. this.promisified = promisifyAll(this.module)
  24. })
  25. it('promisifies functions in the module', async function () {
  26. const sum = await this.promisified.asyncAdd(29, 33)
  27. expect(sum).to.equal(62)
  28. })
  29. it('binds this to the original module', async function () {
  30. const sum = await this.promisified.asyncDouble(38)
  31. expect(sum).to.equal(76)
  32. })
  33. it('does not copy over non-functions', async function () {
  34. expect(this.promisified).not.to.have.property('SOME_CONSTANT')
  35. })
  36. it('does not modify the prototype of the module', async function () {
  37. expect(this.promisified.toString()).to.equal('[object Object]')
  38. })
  39. })
  40. describe('without option', function () {
  41. before(function () {
  42. this.module = {
  43. asyncAdd(a, b, callback) {
  44. callback(null, a + b)
  45. },
  46. syncAdd(a, b) {
  47. return a + b
  48. },
  49. }
  50. this.promisified = promisifyAll(this.module, { without: ['syncAdd'] })
  51. })
  52. it('does not promisify excluded functions', function () {
  53. expect(this.promisified.syncAdd).not.to.exist
  54. })
  55. it('promisifies other functions', async function () {
  56. const sum = await this.promisified.asyncAdd(12, 89)
  57. expect(sum).to.equal(101)
  58. })
  59. })
  60. describe('multiResult option', function () {
  61. before(function () {
  62. this.module = {
  63. asyncAdd(a, b, callback) {
  64. callback(null, a + b)
  65. },
  66. asyncArithmetic(a, b, callback) {
  67. callback(null, a + b, a * b)
  68. },
  69. }
  70. this.promisified = promisifyAll(this.module, {
  71. multiResult: { asyncArithmetic: ['sum', 'product'] },
  72. })
  73. })
  74. it('promisifies multi-result functions', async function () {
  75. const result = await this.promisified.asyncArithmetic(3, 6)
  76. expect(result).to.deep.equal({ sum: 9, product: 18 })
  77. })
  78. it('promisifies other functions normally', async function () {
  79. const sum = await this.promisified.asyncAdd(6, 1)
  80. expect(sum).to.equal(7)
  81. })
  82. })
  83. })
  84. describe('promisifyClass', function () {
  85. describe('basic functionality', function () {
  86. before(function () {
  87. this.Class = class {
  88. constructor(a) {
  89. this.a = a
  90. }
  91. asyncAdd(b, callback) {
  92. callback(null, this.a + b)
  93. }
  94. }
  95. this.Promisified = promisifyClass(this.Class)
  96. })
  97. it('promisifies the class methods', async function () {
  98. const adder = new this.Promisified(1)
  99. const sum = await adder.asyncAdd(2)
  100. expect(sum).to.equal(3)
  101. })
  102. })
  103. describe('without option', function () {
  104. before(function () {
  105. this.Class = class {
  106. constructor(a) {
  107. this.a = a
  108. }
  109. asyncAdd(b, callback) {
  110. callback(null, this.a + b)
  111. }
  112. syncAdd(b) {
  113. return this.a + b
  114. }
  115. }
  116. this.Promisified = promisifyClass(this.Class, { without: ['syncAdd'] })
  117. })
  118. it('does not promisify excluded functions', function () {
  119. const adder = new this.Promisified(10)
  120. const sum = adder.syncAdd(12)
  121. expect(sum).to.equal(22)
  122. })
  123. it('promisifies other functions', async function () {
  124. const adder = new this.Promisified(23)
  125. const sum = await adder.asyncAdd(3)
  126. expect(sum).to.equal(26)
  127. })
  128. })
  129. describe('multiResult option', function () {
  130. before(function () {
  131. this.Class = class {
  132. constructor(a) {
  133. this.a = a
  134. }
  135. asyncAdd(b, callback) {
  136. callback(null, this.a + b)
  137. }
  138. asyncArithmetic(b, callback) {
  139. callback(null, this.a + b, this.a * b)
  140. }
  141. }
  142. this.Promisified = promisifyClass(this.Class, {
  143. multiResult: { asyncArithmetic: ['sum', 'product'] },
  144. })
  145. })
  146. it('promisifies multi-result functions', async function () {
  147. const adder = new this.Promisified(3)
  148. const result = await adder.asyncArithmetic(6)
  149. expect(result).to.deep.equal({ sum: 9, product: 18 })
  150. })
  151. it('promisifies other functions normally', async function () {
  152. const adder = new this.Promisified(6)
  153. const sum = await adder.asyncAdd(1)
  154. expect(sum).to.equal(7)
  155. })
  156. })
  157. })
  158. describe('callbackifyMultiResult', function () {
  159. it('callbackifies a multi-result function', function (done) {
  160. async function asyncArithmetic(a, b) {
  161. return { sum: a + b, product: a * b }
  162. }
  163. const callbackified = callbackifyMultiResult(asyncArithmetic, [
  164. 'sum',
  165. 'product',
  166. ])
  167. callbackified(3, 11, (err, sum, product) => {
  168. if (err != null) {
  169. return done(err)
  170. }
  171. expect(sum).to.equal(14)
  172. expect(product).to.equal(33)
  173. done()
  174. })
  175. })
  176. it('propagates errors', function (done) {
  177. async function asyncBomb() {
  178. throw new Error('BOOM!')
  179. }
  180. const callbackified = callbackifyMultiResult(asyncBomb, [
  181. 'explosives',
  182. 'dynamite',
  183. ])
  184. callbackified(err => {
  185. expect(err).to.exist
  186. done()
  187. })
  188. })
  189. })
  190. describe('callbackifyAll', function () {
  191. describe('basic functionality', function () {
  192. before(function () {
  193. this.module = {
  194. SOME_CONSTANT: 1,
  195. async asyncAdd(a, b) {
  196. return a + b
  197. },
  198. async asyncDouble(x, callback) {
  199. return await this.asyncAdd(x, x)
  200. },
  201. dashConcat(a, b) {
  202. return `${a}-${b}`
  203. },
  204. }
  205. this.callbackified = callbackifyAll(this.module)
  206. })
  207. it('callbackifies async functions in the module', function (done) {
  208. this.callbackified.asyncAdd(77, 18, (err, sum) => {
  209. if (err) {
  210. return done(err)
  211. }
  212. expect(sum).to.equal(95)
  213. done()
  214. })
  215. })
  216. it('binds this to the original module', function (done) {
  217. this.callbackified.asyncDouble(20, (err, double) => {
  218. if (err) {
  219. return done(err)
  220. }
  221. expect(double).to.equal(40)
  222. done()
  223. })
  224. })
  225. it('copies over regular functions', function () {
  226. const s = this.callbackified.dashConcat('ping', 'pong')
  227. expect(s).to.equal('ping-pong')
  228. })
  229. it('copies over non-functions', function () {
  230. expect(this.callbackified.SOME_CONSTANT).to.equal(1)
  231. })
  232. })
  233. describe('multiResult option', function () {
  234. before(function () {
  235. this.module = {
  236. async asyncAdd(a, b) {
  237. return a + b
  238. },
  239. async asyncArithmetic(a, b) {
  240. return { sum: a + b, product: a * b }
  241. },
  242. }
  243. this.callbackified = callbackifyAll(this.module, {
  244. multiResult: { asyncArithmetic: ['sum', 'product'] },
  245. })
  246. })
  247. it('callbackifies multi-result functions', function (done) {
  248. this.callbackified.asyncArithmetic(4, 5, (err, sum, product) => {
  249. if (err) {
  250. return done(err)
  251. }
  252. expect(sum).to.equal(9)
  253. expect(product).to.equal(20)
  254. done()
  255. })
  256. })
  257. it('callbackifies other functions normally', function (done) {
  258. this.callbackified.asyncAdd(77, 18, (err, sum) => {
  259. if (err) {
  260. return done(err)
  261. }
  262. expect(sum).to.equal(95)
  263. done()
  264. })
  265. })
  266. })
  267. describe('without option', function () {
  268. before(function () {
  269. this.module = {
  270. async asyncAdd(a, b) {
  271. return a + b
  272. },
  273. async asyncArithmetic(a, b) {
  274. return { sum: a + b, product: a * b }
  275. },
  276. }
  277. this.callbackified = callbackifyAll(this.module, {
  278. without: ['asyncAdd'],
  279. })
  280. })
  281. it('does not callbackify excluded functions', function () {
  282. expect(this.callbackified.asyncAdd).not.to.exist
  283. })
  284. it('callbackifies other functions', async function () {
  285. this.callbackified.asyncArithmetic(5, 6, (err, { sum, product }) => {
  286. expect(err).not.to.exist
  287. expect(sum).to.equal(11)
  288. expect(product).to.equal(30)
  289. })
  290. })
  291. })
  292. })
  293. describe('callbackifyClass', function () {
  294. describe('basic functionality', function () {
  295. before(function () {
  296. this.Class = class {
  297. constructor(a) {
  298. this.a = a
  299. }
  300. async asyncAdd(b) {
  301. return this.a + b
  302. }
  303. }
  304. this.Callbackified = callbackifyClass(this.Class)
  305. })
  306. it('callbackifies the class methods', function (done) {
  307. const adder = new this.Callbackified(1)
  308. adder.asyncAdd(2, (err, sum) => {
  309. expect(err).not.to.exist
  310. expect(sum).to.equal(3)
  311. done()
  312. })
  313. })
  314. })
  315. describe('without option', function () {
  316. before(function () {
  317. this.Class = class {
  318. constructor(a) {
  319. this.a = a
  320. }
  321. async asyncAdd(b) {
  322. return this.a + b
  323. }
  324. syncAdd(b) {
  325. return this.a + b
  326. }
  327. }
  328. this.Callbackified = callbackifyClass(this.Class, {
  329. without: ['syncAdd'],
  330. })
  331. })
  332. it('does not callbackify excluded functions', function () {
  333. const adder = new this.Callbackified(10)
  334. const sum = adder.syncAdd(12)
  335. expect(sum).to.equal(22)
  336. })
  337. it('callbackifies other functions', function (done) {
  338. const adder = new this.Callbackified(1)
  339. adder.asyncAdd(2, (err, sum) => {
  340. expect(err).not.to.exist
  341. expect(sum).to.equal(3)
  342. done()
  343. })
  344. })
  345. })
  346. describe('multiResult option', function () {
  347. before(function () {
  348. this.Class = class {
  349. constructor(a) {
  350. this.a = a
  351. }
  352. async asyncAdd(b) {
  353. return this.a + b
  354. }
  355. async asyncArithmetic(b) {
  356. return { sum: this.a + b, product: this.a * b }
  357. }
  358. }
  359. this.Callbackified = callbackifyClass(this.Class, {
  360. multiResult: { asyncArithmetic: ['sum', 'product'] },
  361. })
  362. })
  363. it('callbackifies multi-result functions', function (done) {
  364. const adder = new this.Callbackified(3)
  365. adder.asyncArithmetic(6, (err, sum, product) => {
  366. expect(err).not.to.exist
  367. expect(sum).to.equal(9)
  368. expect(product).to.equal(18)
  369. done()
  370. })
  371. })
  372. it('callbackifies other functions normally', function (done) {
  373. const adder = new this.Callbackified(6)
  374. adder.asyncAdd(2, (err, sum) => {
  375. expect(err).not.to.exist
  376. expect(sum).to.equal(8)
  377. done()
  378. })
  379. })
  380. })
  381. })
  382. describe('expressify', function () {
  383. it('should propagate any rejection to the "next" callback', function (done) {
  384. const fn = () => Promise.reject(new Error('rejected'))
  385. expressify(fn)({}, {}, error => {
  386. expect(error.message).to.equal('rejected')
  387. done()
  388. })
  389. })
  390. })
  391. describe('expressifyErrorHandler', function () {
  392. it('should propagate any rejection to the "next" callback', function (done) {
  393. const fn = () => Promise.reject(new Error('rejected'))
  394. expressifyErrorHandler(fn)({}, {}, {}, error => {
  395. expect(error.message).to.equal('rejected')
  396. done()
  397. })
  398. })
  399. })