|
| 1 | +var promisify = require('../../').promisify; |
| 2 | +var test = require('tape'); |
| 3 | + |
| 4 | +var hasSymbol = typeof Symbol !== 'undefined'; |
| 5 | + |
| 6 | +if (typeof Promise === 'undefined') { |
| 7 | + console.log('no global Promise found, skipping promisify tests'); |
| 8 | + return; |
| 9 | +} |
| 10 | + |
| 11 | +var callbacker = function (arg, cb) { |
| 12 | + setTimeout(function () { |
| 13 | + if (typeof arg === 'string') |
| 14 | + cb(null, arg.toUpperCase()); |
| 15 | + else cb(new TypeError('incorrect type')); |
| 16 | + }, 5); |
| 17 | +} |
| 18 | +var promiser = promisify(callbacker); |
| 19 | + |
| 20 | +test('util.promisify resolves', function (t) { |
| 21 | + var promise = promiser(__filename); |
| 22 | + t.ok(promise instanceof Promise); |
| 23 | + promise.then(function (value) { |
| 24 | + t.deepEqual(value, __filename.toUpperCase()); |
| 25 | + t.end(); |
| 26 | + }); |
| 27 | +}); |
| 28 | + |
| 29 | +test('util.promisify rejects', function (t) { |
| 30 | + var promise = promiser(42); |
| 31 | + promise.catch(function (error) { |
| 32 | + t.equal(error.message, 'incorrect type'); |
| 33 | + t.end(); |
| 34 | + }); |
| 35 | +}); |
| 36 | + |
| 37 | +test('util.promisify custom', { skip: !hasSymbol }, function (t) { |
| 38 | + function fn() {} |
| 39 | + function promisifedFn() {} |
| 40 | + fn[promisify.custom] = promisifedFn; |
| 41 | + t.strictEqual(promisify(fn), promisifedFn); |
| 42 | + t.strictEqual(promisify(promisify(fn)), promisifedFn); |
| 43 | + t.end(); |
| 44 | +}); |
| 45 | + |
| 46 | +test('util.promisify custom of invalid type', { skip: !hasSymbol }, function (t) { |
| 47 | + function fn2() {} |
| 48 | + fn2[promisify.custom] = 42; |
| 49 | + t.throws( |
| 50 | + function () { promisify(fn2); }, |
| 51 | + /must be of type Function/ |
| 52 | + ); |
| 53 | + t.end(); |
| 54 | +}); |
| 55 | + |
| 56 | +test('util.promisify multiple callback values', function (t) { |
| 57 | + function fn5(callback) { |
| 58 | + callback(null, 'foo', 'bar'); |
| 59 | + } |
| 60 | + promisify(fn5)().then(function (value) { |
| 61 | + t.strictEqual(value, 'foo'); |
| 62 | + t.end(); |
| 63 | + }); |
| 64 | +}); |
| 65 | + |
| 66 | +test('util.promisify no callback success value', function (t) { |
| 67 | + function fn6(callback) { |
| 68 | + callback(null); |
| 69 | + } |
| 70 | + promisify(fn6)().then(function (value) { |
| 71 | + t.strictEqual(value, undefined); |
| 72 | + t.end(); |
| 73 | + }); |
| 74 | +}); |
| 75 | + |
| 76 | +test('util.promisify no callback arguments at all', function (t) { |
| 77 | + function fn7(callback) { |
| 78 | + callback(); |
| 79 | + } |
| 80 | + promisify(fn7)().then(function (value) { |
| 81 | + t.strictEqual(value, undefined); |
| 82 | + t.end(); |
| 83 | + }); |
| 84 | +}); |
| 85 | + |
| 86 | +test('util.promisify passing arguments', function (t) { |
| 87 | + function fn8(err, val, callback) { |
| 88 | + callback(err, val); |
| 89 | + } |
| 90 | + promisify(fn8)(null, 42).then(function (value) { |
| 91 | + t.strictEqual(value, 42); |
| 92 | + t.end(); |
| 93 | + }); |
| 94 | +}); |
| 95 | + |
| 96 | +test('util.promisify passing arguments (rejects)', function (t) { |
| 97 | + function fn9(err, val, callback) { |
| 98 | + callback(err, val); |
| 99 | + } |
| 100 | + promisify(fn9)(new Error('oops'), null).catch(function (err) { |
| 101 | + t.strictEqual(err.message, 'oops'); |
| 102 | + t.end(); |
| 103 | + }); |
| 104 | +}); |
| 105 | + |
| 106 | +test('util.promisify chain', function (t) { |
| 107 | + function fn9(err, val, callback) { |
| 108 | + callback(err, val); |
| 109 | + } |
| 110 | + |
| 111 | + |
| 112 | + Promise.resolve() |
| 113 | + .then(function () { return promisify(fn9)(null, 42); }) |
| 114 | + .then(function (value) { |
| 115 | + t.strictEqual(value, 42); |
| 116 | + t.end(); |
| 117 | + }); |
| 118 | +}); |
| 119 | + |
| 120 | +test('util.promisify keeps correct `this`', function (t) { |
| 121 | + var o = {}; |
| 122 | + var fn10 = promisify(function(cb) { |
| 123 | + |
| 124 | + cb(null, this === o); |
| 125 | + }); |
| 126 | + |
| 127 | + o.fn = fn10; |
| 128 | + |
| 129 | + o.fn().then(function(val) { |
| 130 | + t.ok(val); |
| 131 | + t.end(); |
| 132 | + }); |
| 133 | +}); |
| 134 | + |
| 135 | +test('util.promisify calling callback multiple times', function (t) { |
| 136 | + var err = new Error('Should not have called the callback with the error.'); |
| 137 | + var stack = err.stack; |
| 138 | + |
| 139 | + var fn11 = promisify(function(cb) { |
| 140 | + cb(null); |
| 141 | + cb(err); |
| 142 | + }); |
| 143 | + |
| 144 | + Promise.resolve() |
| 145 | + .then(function () { return fn11(); }) |
| 146 | + .then(function () { return Promise.resolve(); }) |
| 147 | + .then(function () { |
| 148 | + t.strictEqual(stack, err.stack); |
| 149 | + t.end(); |
| 150 | + }); |
| 151 | +}); |
| 152 | + |
| 153 | +// Can't do this without Symbol() unfortunately |
| 154 | +test('util.promisify promisifying a promisified function', { skip: !hasSymbol }, function (t) { |
| 155 | + function c() { } |
| 156 | + var a = promisify(function() { }); |
| 157 | + var b = promisify(a); |
| 158 | + t.notStrictEqual(c, a); |
| 159 | + t.strictEqual(a, b); |
| 160 | + t.end(); |
| 161 | +}); |
| 162 | + |
| 163 | +test('util.promisify sync throw becomes rejection', function (t) { |
| 164 | + var errToThrow; |
| 165 | + var thrower = promisify(function(a, b, c, cb) { |
| 166 | + errToThrow = new Error(); |
| 167 | + throw errToThrow; |
| 168 | + }); |
| 169 | + thrower(1, 2, 3) |
| 170 | + .then(t.fail) |
| 171 | + .then(t.fail, function (e) { |
| 172 | + t.strictEqual(e, errToThrow); |
| 173 | + t.end(); |
| 174 | + }); |
| 175 | +}); |
| 176 | + |
| 177 | +test('util.promisify callback and sync throw', function (t) { |
| 178 | + var err = new Error(); |
| 179 | + |
| 180 | + var a = promisify(function (cb) { cb(err) })(); |
| 181 | + var b = promisify(function () { throw err; })(); |
| 182 | + |
| 183 | + Promise.all([ |
| 184 | + a.then(t.fail, function(e) { |
| 185 | + t.strictEqual(err, e); |
| 186 | + }), |
| 187 | + b.then(t.fail, function(e) { |
| 188 | + t.strictEqual(err, e); |
| 189 | + }) |
| 190 | + ]).then(function () { |
| 191 | + t.end(); |
| 192 | + }); |
| 193 | +}); |
| 194 | + |
| 195 | +test('util.promisify throws for incorrect argument types', function (t) { |
| 196 | + var array = [undefined, null, true, 0, 'str', {}, []]; |
| 197 | + if (typeof Symbol !== 'undefined') array.push(Symbol()); |
| 198 | + array.forEach(function (input) { |
| 199 | + t.throws( |
| 200 | + function () { promisify(input); }, |
| 201 | + 'The "original" argument must be of type Function' |
| 202 | + ); |
| 203 | + }); |
| 204 | + t.end(); |
| 205 | +}); |
0 commit comments