0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-21 21:19:50 +01:00
nodejs/test/parallel/test-common-must-not-call.js
Antoine du Hamel 5114c46c69
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: https://github.com/nodejs/node/pull/42917
Reviewed-By: Michaël Zasso <targos@protonmail.com>
2022-06-12 11:40:18 +02:00

57 lines
1.4 KiB
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const path = require('path');
const util = require('util');
const message = 'message';
const testFunction1 = common.mustNotCall(message);
const testFunction2 = common.mustNotCall(message);
const createValidate = (line, args = []) => common.mustCall((e) => {
const prefix = `${message} at `;
assert.ok(e.message.startsWith(prefix));
if (process.platform === 'win32') {
e.message = e.message.substring(2); // remove 'C:'
}
const msg = e.message.substring(prefix.length);
const firstColon = msg.indexOf(':');
const fileName = msg.substring(0, firstColon);
const rest = msg.substring(firstColon + 1);
assert.strictEqual(path.basename(fileName), 'test-common-must-not-call.js');
const argsInfo = args.length > 0 ?
`\ncalled with arguments: ${args.map(util.inspect).join(', ')}` : '';
assert.strictEqual(rest, line + argsInfo);
});
const validate1 = createValidate('9');
try {
testFunction1();
} catch (e) {
validate1(e);
}
const validate2 = createValidate('11', ['hello', 42]);
try {
testFunction2('hello', 42);
} catch (e) {
validate2(e);
}
assert.throws(
() => new Proxy({ prop: Symbol() }, { get: common.mustNotCall() }).prop,
{ code: 'ERR_ASSERTION' }
);
{
const { inspect } = util;
delete util.inspect;
assert.throws(
() => common.mustNotCall()(null),
{ code: 'ERR_ASSERTION' }
);
util.inspect = inspect;
}