Skip to content

Commit 7744f66

Browse files
lundibundicodebytere
authored andcommitted
test: print arguments passed to mustNotCall function
Refs: #33949 (comment) Signed-off-by: Denys Otrishko <[email protected]> PR-URL: #33951 Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 437f387 commit 7744f66

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

test/common/index.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -418,9 +418,12 @@ function getCallSite(top) {
418418

419419
function mustNotCall(msg) {
420420
const callSite = getCallSite(mustNotCall);
421-
return function mustNotCall() {
421+
return function mustNotCall(...args) {
422+
const argsInfo = args.length > 0 ?
423+
`\ncalled with arguments: ${args.map(util.inspect).join(', ')}` : '';
422424
assert.fail(
423-
`${msg || 'function should not have been called'} at ${callSite}`);
425+
`${msg || 'function should not have been called'} at ${callSite}` +
426+
argsInfo);
424427
};
425428
}
426429

test/parallel/test-common-must-not-call.js

+22-7
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,39 @@
33
const common = require('../common');
44
const assert = require('assert');
55
const path = require('path');
6+
const util = require('util');
67

78
const message = 'message';
8-
const testFunction = common.mustNotCall(message);
9+
const testFunction1 = common.mustNotCall(message);
910

10-
const validateError = common.mustCall((e) => {
11+
const testFunction2 = common.mustNotCall(message);
12+
13+
const createValidate = (line, args = []) => common.mustCall((e) => {
1114
const prefix = `${message} at `;
1215
assert.ok(e.message.startsWith(prefix));
1316
if (process.platform === 'win32') {
1417
e.message = e.message.substring(2); // remove 'C:'
1518
}
16-
const [ fileName, lineNumber ] = e.message
17-
.substring(prefix.length).split(':');
19+
const msg = e.message.substring(prefix.length);
20+
const firstColon = msg.indexOf(':');
21+
const fileName = msg.substring(0, firstColon);
22+
const rest = msg.substring(firstColon + 1);
1823
assert.strictEqual(path.basename(fileName), 'test-common-must-not-call.js');
19-
assert.strictEqual(lineNumber, '8');
24+
const argsInfo = args.length > 0 ?
25+
`\ncalled with arguments: ${args.map(util.inspect).join(', ')}` : '';
26+
assert.strictEqual(rest, line + argsInfo);
2027
});
2128

29+
const validate1 = createValidate('9');
30+
try {
31+
testFunction1();
32+
} catch (e) {
33+
validate1(e);
34+
}
35+
36+
const validate2 = createValidate('11', ['hello', 42]);
2237
try {
23-
testFunction();
38+
testFunction2('hello', 42);
2439
} catch (e) {
25-
validateError(e);
40+
validate2(e);
2641
}

0 commit comments

Comments
 (0)