Skip to content

test: return frozen function from common.must[Not]Call #43825

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions test/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ function _mustCallInner(fn, criteria = 1, field) {
configurable: true,
},
});
return _return;
return Object.freeze(_return);
}

function hasMultiLocalhost() {
Expand Down Expand Up @@ -510,13 +510,13 @@ function getCallSite(top) {

function mustNotCall(msg) {
const callSite = getCallSite(mustNotCall);
return function mustNotCall(...args) {
return Object.freeze(function mustNotCall(...args) {
const argsInfo = args.length > 0 ?
`\ncalled with arguments: ${args.map((arg) => inspect(arg)).join(', ')}` : '';
assert.fail(
`${msg || 'function should not have been called'} at ${callSite}` +
argsInfo);
};
});
}

const _mustNotMutateObjectDeepProxies = new WeakMap();
Expand Down
21 changes: 14 additions & 7 deletions test/parallel/test-timers-user-call.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
// Make sure `setTimeout()` and friends don't throw if the user-supplied
// function has .call() and .apply() monkey-patched to undesirable values.
// function doesn't provide valid .call() or .apply().

// Refs: https://github.com/nodejs/node/issues/12956

'use strict';

const common = require('../common');
const assert = require('node:assert');

{
const fn = common.mustCall(10);
fn.call = 'not a function';
fn.apply = 'also not a function';
const fn = new Proxy(common.mustCall(10), {
get(target, property, receiver) {
// We don't want `call` and `apply` to be accessed in particular
assert.fail(`tried to access .${property} property on fn`);
}
});
setTimeout(fn, 1);
setTimeout(fn, 1, 'oneArg');
setTimeout(fn, 1, 'two', 'args');
Expand All @@ -26,9 +30,12 @@ const common = require('../common');

{
const testInterval = (...args) => {
const fn = common.mustCall(() => { clearInterval(interval); });
fn.call = 'not a function';
fn.apply = 'also not a function';
const fn = new Proxy(common.mustCall(() => { clearInterval(interval); }), {
get(target, property, receiver) {
// We don't want `call` and `apply` to be accessed in particular
assert.fail(`tried to access .${property} property on fn`);
}
});
const interval = setInterval(fn, 1, ...args);
};

Expand Down