Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
test: add a bunch of tests from bluebird
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: #12442
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Myles Borins <myles.borins@gmail.com>
Reviewed-By: Evan Lucas <evanlucas@me.com>
Reviewed-By: William Kapke <william.kapke@gmail.com>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
Reviewed-By: Teddy Katz <teddy.katz@gmail.com>
  • Loading branch information
MadaraUchiha authored and addaleax committed May 9, 2017
commit 3ea2301e38677a61af0cb3093138869294358339
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);
})
]);
}