Skip to content

Commit 53828e8

Browse files
lucamaraschimhdawson
authored andcommitted
test: extended test to makeCallback cb type check
makeCallback and makeStatsCallback are both tested intedependently. PR-URL: #12140 Fixes: #12136 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
1 parent 7eb1b46 commit 53828e8

File tree

2 files changed

+47
-25
lines changed

2 files changed

+47
-25
lines changed

test/parallel/test-fs-make-callback.js

+17-25
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,29 @@ const common = require('../common');
33
const assert = require('assert');
44
const fs = require('fs');
55
const cbTypeError = /^TypeError: "callback" argument must be a function$/;
6+
const callbackThrowValues = [null, true, false, 0, 1, 'foo', /foo/, [], {}];
67

7-
function test(cb) {
8+
const { sep } = require('path');
9+
const warn = 'Calling an asynchronous function without callback is deprecated.';
10+
11+
common.refreshTmpDir();
12+
13+
function testMakeCallback(cb) {
814
return function() {
9-
// fs.stat() calls makeCallback() on its second argument
10-
fs.stat(__filename, cb);
15+
// fs.mkdtemp() calls makeCallback() on its third argument
16+
fs.mkdtemp(`${common.tmpDir}${sep}`, {}, cb);
1117
};
1218
}
1319

14-
// Verify the case where a callback function is provided
15-
assert.doesNotThrow(test(common.noop));
16-
17-
process.once('warning', common.mustCall((warning) => {
18-
assert.strictEqual(
19-
warning.message,
20-
'Calling an asynchronous function without callback is deprecated.'
21-
);
22-
23-
invalidArgumentsTests();
24-
}));
20+
common.expectWarning('DeprecationWarning', warn);
2521

2622
// Passing undefined/nothing calls rethrow() internally, which emits a warning
27-
assert.doesNotThrow(test());
23+
assert.doesNotThrow(testMakeCallback());
2824

29-
function invalidArgumentsTests() {
30-
assert.throws(test(null), cbTypeError);
31-
assert.throws(test(true), cbTypeError);
32-
assert.throws(test(false), cbTypeError);
33-
assert.throws(test(1), cbTypeError);
34-
assert.throws(test(0), cbTypeError);
35-
assert.throws(test('foo'), cbTypeError);
36-
assert.throws(test(/foo/), cbTypeError);
37-
assert.throws(test([]), cbTypeError);
38-
assert.throws(test({}), cbTypeError);
25+
function invalidCallbackThrowsTests() {
26+
callbackThrowValues.forEach((value) => {
27+
assert.throws(testMakeCallback(value), cbTypeError);
28+
});
3929
}
30+
31+
invalidCallbackThrowsTests();
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const fs = require('fs');
5+
const cbTypeError = /^TypeError: "callback" argument must be a function$/;
6+
const callbackThrowValues = [null, true, false, 0, 1, 'foo', /foo/, [], {}];
7+
const warn = 'Calling an asynchronous function without callback is deprecated.';
8+
9+
function testMakeStatsCallback(cb) {
10+
return function() {
11+
// fs.stat() calls makeStatsCallback() on its second argument
12+
fs.stat(__filename, cb);
13+
};
14+
}
15+
16+
common.expectWarning('DeprecationWarning', warn);
17+
18+
// Verify the case where a callback function is provided
19+
assert.doesNotThrow(testMakeStatsCallback(common.noop));
20+
21+
// Passing undefined/nothing calls rethrow() internally, which emits a warning
22+
assert.doesNotThrow(testMakeStatsCallback());
23+
24+
function invalidCallbackThrowsTests() {
25+
callbackThrowValues.forEach((value) => {
26+
assert.throws(testMakeStatsCallback(value), cbTypeError);
27+
});
28+
}
29+
30+
invalidCallbackThrowsTests();

0 commit comments

Comments
 (0)