Skip to content

Commit f8d26c6

Browse files
aduh95danielleadams
authored andcommitted
test: fix common.mustNotCall error message
When using `util.inspect` as a callback, the array index (second argument) is picked up as the `showHidden` param, and the argument array (third argument) as the `depth` param. This fails with a seemingly unrelated error message when the argument array contains a `Symbol`-related property. PR-URL: #42917 Reviewed-By: Michaël Zasso <[email protected]>
1 parent e30d4c1 commit f8d26c6

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

test/common/index.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const fs = require('fs');
2929
// Do not require 'os' until needed so that test-os-checked-function can
3030
// monkey patch it. If 'os' is required here, that test will fail.
3131
const path = require('path');
32-
const util = require('util');
32+
const { inspect } = require('util');
3333
const { isMainThread } = require('worker_threads');
3434

3535
const tmpdir = require('./tmpdir');
@@ -97,7 +97,7 @@ if (process.argv.length === 2 &&
9797
(process.features.inspector || !flag.startsWith('--inspect'))) {
9898
console.log(
9999
'NOTE: The test started as a child_process using these flags:',
100-
util.inspect(flags),
100+
inspect(flags),
101101
'Use NODE_SKIP_FLAG_CHECK to run the test with the original flags.'
102102
);
103103
const args = [...flags, ...process.execArgv, ...process.argv.slice(1)];
@@ -149,7 +149,7 @@ if (process.env.NODE_TEST_WITH_ASYNC_HOOKS) {
149149
process.on('exit', () => {
150150
// Iterate through handles to make sure nothing crashes
151151
for (const k in initHandles)
152-
util.inspect(initHandles[k]);
152+
inspect(initHandles[k]);
153153
});
154154

155155
const _queueDestroyAsyncId = async_wrap.queueDestroyAsyncId;
@@ -159,7 +159,7 @@ if (process.env.NODE_TEST_WITH_ASYNC_HOOKS) {
159159
process._rawDebug();
160160
throw new Error(`same id added to destroy list twice (${id})`);
161161
}
162-
destroyListList[id] = util.inspect(new Error());
162+
destroyListList[id] = inspect(new Error());
163163
_queueDestroyAsyncId(id);
164164
};
165165

@@ -173,7 +173,7 @@ if (process.env.NODE_TEST_WITH_ASYNC_HOOKS) {
173173
}
174174
initHandles[id] = {
175175
resource,
176-
stack: util.inspect(new Error()).substr(6)
176+
stack: inspect(new Error()).substr(6)
177177
};
178178
},
179179
before() { },
@@ -184,7 +184,7 @@ if (process.env.NODE_TEST_WITH_ASYNC_HOOKS) {
184184
process._rawDebug();
185185
throw new Error(`destroy called for same id (${id})`);
186186
}
187-
destroydIdsList[id] = util.inspect(new Error());
187+
destroydIdsList[id] = inspect(new Error());
188188
},
189189
}).enable();
190190
}
@@ -424,7 +424,7 @@ function _mustCallInner(fn, criteria = 1, field) {
424424
const context = {
425425
[field]: criteria,
426426
actual: 0,
427-
stack: util.inspect(new Error()),
427+
stack: inspect(new Error()),
428428
name: fn.name || '<anonymous>'
429429
};
430430

@@ -512,7 +512,7 @@ function mustNotCall(msg) {
512512
const callSite = getCallSite(mustNotCall);
513513
return function mustNotCall(...args) {
514514
const argsInfo = args.length > 0 ?
515-
`\ncalled with arguments: ${args.map(util.inspect).join(', ')}` : '';
515+
`\ncalled with arguments: ${args.map((arg) => inspect(arg)).join(', ')}` : '';
516516
assert.fail(
517517
`${msg || 'function should not have been called'} at ${callSite}` +
518518
argsInfo);
@@ -614,7 +614,7 @@ function expectWarning(nameOrMap, expected, code) {
614614
if (!catchWarning[warning.name]) {
615615
throw new TypeError(
616616
`"${warning.name}" was triggered without being expected.\n` +
617-
util.inspect(warning)
617+
inspect(warning)
618618
);
619619
}
620620
catchWarning[warning.name](warning);
@@ -635,7 +635,7 @@ function expectsError(validator, exact) {
635635
if (args.length !== 1) {
636636
// Do not use `assert.strictEqual()` to prevent `inspect` from
637637
// always being called.
638-
assert.fail(`Expected one argument, got ${util.inspect(args)}`);
638+
assert.fail(`Expected one argument, got ${inspect(args)}`);
639639
}
640640
const error = args.pop();
641641
const descriptor = Object.getOwnPropertyDescriptor(error, 'message');
@@ -740,9 +740,9 @@ function invalidArgTypeHelper(input) {
740740
if (input.constructor && input.constructor.name) {
741741
return ` Received an instance of ${input.constructor.name}`;
742742
}
743-
return ` Received ${util.inspect(input, { depth: -1 })}`;
743+
return ` Received ${inspect(input, { depth: -1 })}`;
744744
}
745-
let inspected = util.inspect(input, { colors: false });
745+
let inspected = inspect(input, { colors: false });
746746
if (inspected.length > 25)
747747
inspected = `${inspected.slice(0, 25)}...`;
748748
return ` Received type ${typeof input} (${inspected})`;

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

+15
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,18 @@ try {
3939
} catch (e) {
4040
validate2(e);
4141
}
42+
43+
assert.throws(
44+
() => new Proxy({ prop: Symbol() }, { get: common.mustNotCall() }).prop,
45+
{ code: 'ERR_ASSERTION' }
46+
);
47+
48+
{
49+
const { inspect } = util;
50+
delete util.inspect;
51+
assert.throws(
52+
() => common.mustNotCall()(null),
53+
{ code: 'ERR_ASSERTION' }
54+
);
55+
util.inspect = inspect;
56+
}

0 commit comments

Comments
 (0)