PromiseUtilsTests.js 7.5 KB

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