Skip to content

Commit 3ea2301

Browse files
MadaraUchihaaddaleax
authored andcommitted
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 <[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]>
1 parent 99da8e8 commit 3ea2301

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed

test/parallel/test-util-promisify.js

+94
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,97 @@ const stat = promisify(fs.stat);
7474
assert.strictEqual(value, undefined);
7575
}));
7676
}
77+
78+
{
79+
function fn(err, val, callback) {
80+
callback(err, val);
81+
}
82+
promisify(fn)(null, 42).then(common.mustCall((value) => {
83+
assert.strictEqual(value, 42);
84+
}));
85+
}
86+
87+
{
88+
function fn(err, val, callback) {
89+
callback(err, val);
90+
}
91+
promisify(fn)(new Error('oops'), null).catch(common.mustCall((err) => {
92+
assert.strictEqual(err.message, 'oops');
93+
}));
94+
}
95+
96+
{
97+
function fn(err, val, callback) {
98+
callback(err, val);
99+
}
100+
101+
(async () => {
102+
const value = await promisify(fn)(null, 42);
103+
assert.strictEqual(value, 42);
104+
})();
105+
}
106+
107+
{
108+
const o = {};
109+
const fn = promisify(function(cb) {
110+
111+
cb(null, this === o);
112+
});
113+
114+
o.fn = fn;
115+
116+
o.fn().then(common.mustCall(function(val) {
117+
assert(val);
118+
}));
119+
}
120+
121+
{
122+
const err = new Error('Should not have called the callback with the error.');
123+
const stack = err.stack;
124+
125+
const fn = promisify(function(cb) {
126+
cb(null);
127+
cb(err);
128+
});
129+
130+
(async () => {
131+
await fn();
132+
await Promise.resolve();
133+
return assert.strictEqual(stack, err.stack);
134+
})();
135+
}
136+
137+
{
138+
function c() { }
139+
const a = promisify(function() { });
140+
const b = promisify(a);
141+
assert.notStrictEqual(c, a);
142+
assert.strictEqual(a, b);
143+
}
144+
145+
{
146+
let errToThrow;
147+
const thrower = promisify(function(a, b, c, cb) {
148+
errToThrow = new Error();
149+
throw errToThrow;
150+
});
151+
thrower(1, 2, 3)
152+
.then(assert.fail)
153+
.then(assert.fail, (e) => assert.strictEqual(e, errToThrow));
154+
}
155+
156+
{
157+
const err = new Error();
158+
159+
const a = promisify((cb) => cb(err))();
160+
const b = promisify(() => { throw err; })();
161+
162+
Promise.all([
163+
a.then(assert.fail, function(e) {
164+
assert.strictEqual(err, e);
165+
}),
166+
b.then(assert.fail, function(e) {
167+
assert.strictEqual(err, e);
168+
})
169+
]);
170+
}

0 commit comments

Comments
 (0)