Skip to content

Commit

Permalink
test: add a bunch of tests from bluebird
Browse files Browse the repository at this point in the history
Take tests from Bluebird's promisify's tests and adapted them to the
format in use here.
Add tests making sure things work with async functions.
Add basic usability tests.

PR-URL: nodejs#12442
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Myles Borins <[email protected]>
Reviewed-By: Evan Lucas <[email protected]>
Reviewed-By: William Kapke <[email protected]>
Reviewed-By: Timothy Gu <[email protected]>
Reviewed-By: Teddy Katz <[email protected]>
  • Loading branch information
MadaraUchiha authored and Olivier Martin committed May 19, 2017
1 parent 49d6e23 commit 0170dc6
Showing 1 changed file with 94 additions and 0 deletions.
94 changes: 94 additions & 0 deletions test/parallel/test-util-promisify.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,97 @@ const stat = promisify(fs.stat);
assert.strictEqual(value, undefined);
}));
}

{
function fn(err, val, callback) {
callback(err, val);
}
promisify(fn)(null, 42).then(common.mustCall((value) => {
assert.strictEqual(value, 42);
}));
}

{
function fn(err, val, callback) {
callback(err, val);
}
promisify(fn)(new Error('oops'), null).catch(common.mustCall((err) => {
assert.strictEqual(err.message, 'oops');
}));
}

{
function fn(err, val, callback) {
callback(err, val);
}

(async () => {
const value = await promisify(fn)(null, 42);
assert.strictEqual(value, 42);
})();
}

{
const o = {};
const fn = promisify(function(cb) {

cb(null, this === o);
});

o.fn = fn;

o.fn().then(common.mustCall(function(val) {
assert(val);
}));
}

{
const err = new Error('Should not have called the callback with the error.');
const stack = err.stack;

const fn = promisify(function(cb) {
cb(null);
cb(err);
});

(async () => {
await fn();
await Promise.resolve();
return assert.strictEqual(stack, err.stack);
})();
}

{
function c() { }
const a = promisify(function() { });
const b = promisify(a);
assert.notStrictEqual(c, a);
assert.strictEqual(a, b);
}

{
let errToThrow;
const thrower = promisify(function(a, b, c, cb) {
errToThrow = new Error();
throw errToThrow;
});
thrower(1, 2, 3)
.then(assert.fail)
.then(assert.fail, (e) => assert.strictEqual(e, errToThrow));
}

{
const err = new Error();

const a = promisify((cb) => cb(err))();
const b = promisify(() => { throw err; })();

Promise.all([
a.then(assert.fail, function(e) {
assert.strictEqual(err, e);
}),
b.then(assert.fail, function(e) {
assert.strictEqual(err, e);
})
]);
}

0 comments on commit 0170dc6

Please sign in to comment.