Skip to content
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

test: make common.noop a getter #12732

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 4 additions & 1 deletion test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,10 @@ Returns `true` if the exit code `exitCode` and/or signal name `signal` represent

### noop

A non-op `Function` that can be used for a variety of scenarios.
A getter that returns a non-op `Function` which can be used for a variety of
scenarios. A new function object is always created, so the result of
`common.noop` must be saved in a variable if it is intended to be used in
assertions.

For instance,

Expand Down
16 changes: 9 additions & 7 deletions test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ const execSync = require('child_process').execSync;
const testRoot = process.env.NODE_TEST_DIR ?
fs.realpathSync(process.env.NODE_TEST_DIR) : __dirname;

const noop = () => {};
Object.defineProperty(exports, 'noop', {
enumerable: true,
get: () => () => {}
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about keeping the old noop and add

export.newNoop = () => { return () => {}; };


exports.noop = noop;
exports.fixturesDir = path.join(__dirname, 'fixtures');
exports.tmpDirName = 'tmp';
// PORT should match the definition in test/testpy/__init__.py.
Expand Down Expand Up @@ -434,9 +436,9 @@ function runCallChecks(exitCode) {
exports.mustCall = function(fn, expected) {
if (typeof fn === 'number') {
expected = fn;
fn = noop;
fn = exports.noop;
} else if (fn === undefined) {
fn = noop;
fn = exports.noop;
}

if (expected === undefined)
Expand Down Expand Up @@ -530,9 +532,9 @@ util.inherits(ArrayStream, stream.Stream);
exports.ArrayStream = ArrayStream;
ArrayStream.prototype.readable = true;
ArrayStream.prototype.writable = true;
ArrayStream.prototype.pause = noop;
ArrayStream.prototype.resume = noop;
ArrayStream.prototype.write = noop;
ArrayStream.prototype.pause = exports.noop;
ArrayStream.prototype.resume = exports.noop;
ArrayStream.prototype.write = exports.noop;

// Returns true if the exit code "exitCode" and/or signal name "signal"
// represent the exit code and/or signal name of a node process that aborted,
Expand Down
5 changes: 5 additions & 0 deletions test/parallel/test-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ assert.throws(function() {
}, /^TypeError: Invalid expected value: \/foo\/$/);


// common.noop() tests
assert.strictEqual(common.noop(), undefined);
assert.notStrictEqual(common.noop, common.noop);


// assert.fail() tests
assert.throws(
() => { assert.fail('fhqwhgads'); },
Expand Down
21 changes: 12 additions & 9 deletions test/parallel/test-event-emitter-remove-all-listeners.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ const common = require('../common');
const assert = require('assert');
const events = require('events');


Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops, unintentional style change. I think I had written something there and then deleted too much.

function expect(expected) {
const actual = [];
process.on('exit', function() {
Expand All @@ -38,24 +37,28 @@ function expect(expected) {

{
const ee = new events.EventEmitter();
ee.on('foo', common.noop);
ee.on('bar', common.noop);
ee.on('baz', common.noop);
ee.on('baz', common.noop);
const fooListener = common.noop;
const barListener = common.noop;
const bazListener1 = common.noop;
const bazListener2 = common.noop;
ee.on('foo', fooListener);
ee.on('bar', barListener);
ee.on('baz', bazListener1);
ee.on('baz', bazListener2);
const fooListeners = ee.listeners('foo');
const barListeners = ee.listeners('bar');
const bazListeners = ee.listeners('baz');
ee.on('removeListener', expect(['bar', 'baz', 'baz']));
ee.removeAllListeners('bar');
ee.removeAllListeners('baz');
assert.deepStrictEqual(ee.listeners('foo'), [common.noop]);
assert.deepStrictEqual(ee.listeners('foo'), [fooListener]);
assert.deepStrictEqual(ee.listeners('bar'), []);
assert.deepStrictEqual(ee.listeners('baz'), []);
// After calling removeAllListeners(),
// the old listeners array should stay unchanged.
assert.deepStrictEqual(fooListeners, [common.noop]);
assert.deepStrictEqual(barListeners, [common.noop]);
assert.deepStrictEqual(bazListeners, [common.noop, common.noop]);
assert.deepStrictEqual(fooListeners, [fooListener]);
assert.deepStrictEqual(barListeners, [barListener]);
assert.deepStrictEqual(bazListeners, [bazListener1, bazListener2]);
// After calling removeAllListeners(),
// new listeners arrays is different from the old.
assert.notStrictEqual(ee.listeners('bar'), barListeners);
Expand Down