PromiseUtilsTests.js 8.9 KB

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