Skip to content

Commit a7b9450

Browse files
jasnellMylesBorins
authored andcommitted
test: add common.noop, default for common.mustCall()
Export a new common.noop no-operation function for general use. Allow using common.mustCall() without a fn argument to simplify test cases. Replace various non-op functions throughout tests with common.noop PR-URL: #12027 Reviewed-By: Jeremiah Senkpiel <[email protected]> Reviewed-By: Richard Lau <[email protected]> Reviewed-By: Gibson Fahnestock <[email protected]> Reviewed-By: Teddy Katz <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Franziska Hinkelmann <[email protected]>
1 parent e79c054 commit a7b9450

File tree

98 files changed

+236
-225
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

98 files changed

+236
-225
lines changed

test/addons/async-hello-world/test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ const binding = require(`./build/${common.buildType}/binding`);
66
binding(5, common.mustCall(function(err, val) {
77
assert.strictEqual(err, null);
88
assert.strictEqual(val, 10);
9-
process.nextTick(common.mustCall(function() {}));
9+
process.nextTick(common.mustCall());
1010
}));

test/common/index.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ const Timer = process.binding('timer_wrap').Timer;
1212
const testRoot = process.env.NODE_TEST_DIR ?
1313
fs.realpathSync(process.env.NODE_TEST_DIR) : path.resolve(__dirname, '..');
1414

15+
const noop = () => {};
16+
17+
exports.noop = noop;
1518
exports.fixturesDir = path.join(__dirname, '..', 'fixtures');
1619
exports.tmpDirName = 'tmp';
1720
// PORT should match the definition in test/testpy/__init__.py.
@@ -419,6 +422,13 @@ function runCallChecks(exitCode) {
419422

420423

421424
exports.mustCall = function(fn, expected) {
425+
if (typeof fn === 'number') {
426+
expected = fn;
427+
fn = noop;
428+
} else if (fn === undefined) {
429+
fn = noop;
430+
}
431+
422432
if (expected === undefined)
423433
expected = 1;
424434
else if (typeof expected !== 'number')
@@ -487,9 +497,9 @@ util.inherits(ArrayStream, stream.Stream);
487497
exports.ArrayStream = ArrayStream;
488498
ArrayStream.prototype.readable = true;
489499
ArrayStream.prototype.writable = true;
490-
ArrayStream.prototype.pause = function() {};
491-
ArrayStream.prototype.resume = function() {};
492-
ArrayStream.prototype.write = function() {};
500+
ArrayStream.prototype.pause = noop;
501+
ArrayStream.prototype.resume = noop;
502+
ArrayStream.prototype.write = noop;
493503

494504
// Returns true if the exit code "exitCode" and/or signal name "signal"
495505
// represent the exit code and/or signal name of a node process that aborted,

test/debugger/test-debugger-client.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ addTest(function(client, done) {
129129

130130
let connectCount = 0;
131131
const script = 'setTimeout(function() { console.log("blah"); });' +
132-
'setInterval(function() {}, 1000000);';
132+
'setInterval(common.noop, 1000000);';
133133

134134
let nodeProcess;
135135

@@ -172,7 +172,7 @@ function doTest(cb, done) {
172172
console.error('>>> connecting...');
173173
c.connect(debug.port);
174174
c.on('break', function() {
175-
c.reqContinue(function() {});
175+
c.reqContinue(common.noop);
176176
});
177177
c.on('ready', function() {
178178
connectCount++;

test/parallel/test-assert.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict';
2-
require('../common');
2+
const common = require('../common');
33
const assert = require('assert');
44
const a = require('assert');
55

@@ -515,6 +515,7 @@ testAssertionMessage(/a/, '/a/');
515515
testAssertionMessage(/abc/gim, '/abc/gim');
516516
testAssertionMessage(function f() {}, '[Function: f]');
517517
testAssertionMessage(function() {}, '[Function]');
518+
testAssertionMessage(common.noop, '[Function: noop]');
518519
testAssertionMessage({}, '{}');
519520
testAssertionMessage(circular, '{ y: 1, x: [Circular] }');
520521
testAssertionMessage({a: undefined, b: null}, '{ a: undefined, b: null }');

test/parallel/test-child-process-disconnect.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ if (process.argv[2] === 'child') {
5757
}));
5858

5959
// the process should also self terminate without using signals
60-
child.on('exit', common.mustCall(function() {}));
60+
child.on('exit', common.mustCall());
6161

6262
// when child is listening
6363
child.on('message', function(obj) {

test/parallel/test-child-process-kill.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ const assert = require('assert');
44
const spawn = require('child_process').spawn;
55
const cat = spawn(common.isWindows ? 'cmd' : 'cat');
66

7-
cat.stdout.on('end', common.mustCall(function() {}));
7+
cat.stdout.on('end', common.mustCall());
88
cat.stderr.on('data', common.mustNotCall());
9-
cat.stderr.on('end', common.mustCall(function() {}));
9+
cat.stderr.on('end', common.mustCall());
1010

1111
cat.on('exit', common.mustCall(function(code, signal) {
1212
assert.strictEqual(code, null);

test/parallel/test-child-process-stdin.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ cat.stdout.on('data', function(chunk) {
2222
response += chunk;
2323
});
2424

25-
cat.stdout.on('end', common.mustCall(function() {}));
25+
cat.stdout.on('end', common.mustCall());
2626

2727
cat.stderr.on('data', common.mustNotCall());
2828

29-
cat.stderr.on('end', common.mustCall(function() {}));
29+
cat.stderr.on('end', common.mustCall());
3030

3131
cat.on('exit', common.mustCall(function(status) {
3232
assert.strictEqual(0, status);

test/parallel/test-cluster-eaccess.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ if (cluster.isMaster) {
1414
const worker = cluster.fork();
1515

1616
// makes sure master is able to fork the worker
17-
cluster.on('fork', common.mustCall(function() {}));
17+
cluster.on('fork', common.mustCall());
1818

1919
// makes sure the worker is ready
20-
worker.on('online', common.mustCall(function() {}));
20+
worker.on('online', common.mustCall());
2121

2222
worker.on('message', common.mustCall(function(err) {
2323
// disconnect first, so that we will not leave zombies

test/parallel/test-cluster-setup-master-emit.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ function emitAndCatch2(next) {
2222
}
2323

2424
emitAndCatch(common.mustCall(function() {
25-
emitAndCatch2(common.mustCall(function() {}));
25+
emitAndCatch2(common.mustCall());
2626
}));

test/parallel/test-cluster-worker-destroy.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ if (cluster.isMaster) {
1717
worker2 = cluster.fork();
1818

1919
[worker1, worker2].forEach(function(worker) {
20-
worker.on('disconnect', common.mustCall(function() {}));
21-
worker.on('exit', common.mustCall(function() {}));
20+
worker.on('disconnect', common.mustCall());
21+
worker.on('exit', common.mustCall());
2222
});
2323
} else {
2424
if (cluster.worker.id === 1) {

test/parallel/test-cluster-worker-wait-server-close.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ if (cluster.isWorker) {
2020

2121
// Although not typical, the worker process can exit before the disconnect
2222
// event fires. Use this to keep the process open until the event has fired.
23-
const keepOpen = setInterval(function() {}, 9999);
23+
const keepOpen = setInterval(common.noop, 9999);
2424

2525
// Check worker events and properties
2626
process.once('disconnect', function() {

test/parallel/test-common.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ global.gc = 42; // Not a valid global unless --expose_gc is set.
77
assert.deepStrictEqual(common.leakedGlobals(), ['gc']);
88

99
assert.throws(function() {
10-
common.mustCall(function() {}, 'foo');
10+
common.mustCall(common.noop, 'foo');
1111
}, /^TypeError: Invalid expected value: foo$/);
1212

1313
assert.throws(function() {
14-
common.mustCall(function() {}, /foo/);
14+
common.mustCall(common.noop, /foo/);
1515
}, /^TypeError: Invalid expected value: \/foo\/$/);

test/parallel/test-crypto-random.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const expectedErrorRegexp = /^TypeError: size must be a number >= 0$/;
1717
[crypto.randomBytes, crypto.pseudoRandomBytes].forEach(function(f) {
1818
[-1, undefined, null, false, true, {}, []].forEach(function(value) {
1919
assert.throws(function() { f(value); }, expectedErrorRegexp);
20-
assert.throws(function() { f(value, function() {}); }, expectedErrorRegexp);
20+
assert.throws(function() { f(value, common.noop); }, expectedErrorRegexp);
2121
});
2222

2323
[0, 1, 2, 4, 16, 256, 1024].forEach(function(len) {

test/parallel/test-dgram-close-is-not-callback.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ const portGetter = dgram.createSocket('udp4')
1717
socket.close('bad argument');
1818
portGetter.close();
1919

20-
socket.on('close', common.mustCall(function() {}));
20+
socket.on('close', common.mustCall());
2121
}));

test/parallel/test-dgram-close.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ const portGetter = dgram.createSocket('udp4')
1818
portGetter.address().port,
1919
portGetter.address().address);
2020

21-
assert.strictEqual(socket.close(common.mustCall(function() {})), socket);
22-
socket.on('close', common.mustCall(function() {}));
21+
assert.strictEqual(socket.close(common.mustCall()), socket);
22+
socket.on('close', common.mustCall());
2323
socket = null;
2424

2525
// Verify that accessing handle after closure doesn't throw

test/parallel/test-dgram-send-empty-buffer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ client.bind(0, common.mustCall(function() {
2222

2323
const buf = Buffer.alloc(0);
2424
let interval = setInterval(function() {
25-
client.send(buf, 0, 0, port, '127.0.0.1', common.mustCall(function() {}));
25+
client.send(buf, 0, 0, port, '127.0.0.1', common.mustCall());
2626
}, 10);
2727
}));

test/parallel/test-domain-crypto.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ global.domain = require('domain');
1717

1818
// should not throw a 'TypeError: undefined is not a function' exception
1919
crypto.randomBytes(8);
20-
crypto.randomBytes(8, function() {});
20+
crypto.randomBytes(8, common.noop);
2121
crypto.pseudoRandomBytes(8);
22-
crypto.pseudoRandomBytes(8, function() {});
23-
crypto.pbkdf2('password', 'salt', 8, 8, function() {});
22+
crypto.pseudoRandomBytes(8, common.noop);
23+
crypto.pbkdf2('password', 'salt', 8, 8, common.noop);

test/parallel/test-domain-exit-dispose.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function err() {
2222
function err2() {
2323
// this timeout should never be called, since the domain gets
2424
// disposed when the error happens.
25-
setTimeout(common.mustCall(() => {}, 0), 1);
25+
setTimeout(common.mustNotCall(), 1);
2626

2727
// this function doesn't exist, and throws an error as a result.
2828
err3(); // eslint-disable-line no-undef

test/parallel/test-domain-timers.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ immediated.run(function() {
3232
});
3333
});
3434

35-
timeout = setTimeout(function() {}, 10 * 1000);
35+
timeout = setTimeout(common.noop, 10 * 1000);

test/parallel/test-domain.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22
// Simple tests of most basic domain functionality.
33

4-
require('../common');
4+
const common = require('../common');
55
const assert = require('assert');
66
const domain = require('domain');
77
const events = require('events');
@@ -238,7 +238,7 @@ let fst = fs.createReadStream('stream for nonexistent file');
238238
d.add(fst);
239239
expectCaught++;
240240

241-
[42, null, , false, function() {}, 'string'].forEach(function(something) {
241+
[42, null, , false, common.noop, 'string'].forEach(function(something) {
242242
const d = new domain.Domain();
243243
d.run(function() {
244244
process.nextTick(function() {

test/parallel/test-env-var-no-warnings.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ if (process.argv[2] === 'child') {
2929
test({ NODE_NO_WARNINGS: false });
3030
test({ NODE_NO_WARNINGS: {} });
3131
test({ NODE_NO_WARNINGS: [] });
32-
test({ NODE_NO_WARNINGS: function() {} });
32+
test({ NODE_NO_WARNINGS: common.noop });
3333
test({ NODE_NO_WARNINGS: 0 });
3434
test({ NODE_NO_WARNINGS: -1 });
3535
test({ NODE_NO_WARNINGS: '0' });

test/parallel/test-event-emitter-check-listener-leaks.js

+16-16
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,46 @@
11
'use strict';
2-
require('../common');
2+
const common = require('../common');
33
const assert = require('assert');
44
const events = require('events');
55

66
let e = new events.EventEmitter();
77

88
// default
99
for (let i = 0; i < 10; i++) {
10-
e.on('default', function() {});
10+
e.on('default', common.noop);
1111
}
1212
assert.ok(!e._events['default'].hasOwnProperty('warned'));
13-
e.on('default', function() {});
13+
e.on('default', common.noop);
1414
assert.ok(e._events['default'].warned);
1515

1616
// symbol
1717
const symbol = Symbol('symbol');
1818
e.setMaxListeners(1);
19-
e.on(symbol, function() {});
19+
e.on(symbol, common.noop);
2020
assert.ok(!e._events[symbol].hasOwnProperty('warned'));
21-
e.on(symbol, function() {});
21+
e.on(symbol, common.noop);
2222
assert.ok(e._events[symbol].hasOwnProperty('warned'));
2323

2424
// specific
2525
e.setMaxListeners(5);
2626
for (let i = 0; i < 5; i++) {
27-
e.on('specific', function() {});
27+
e.on('specific', common.noop);
2828
}
2929
assert.ok(!e._events['specific'].hasOwnProperty('warned'));
30-
e.on('specific', function() {});
30+
e.on('specific', common.noop);
3131
assert.ok(e._events['specific'].warned);
3232

3333
// only one
3434
e.setMaxListeners(1);
35-
e.on('only one', function() {});
35+
e.on('only one', common.noop);
3636
assert.ok(!e._events['only one'].hasOwnProperty('warned'));
37-
e.on('only one', function() {});
37+
e.on('only one', common.noop);
3838
assert.ok(e._events['only one'].hasOwnProperty('warned'));
3939

4040
// unlimited
4141
e.setMaxListeners(0);
4242
for (let i = 0; i < 1000; i++) {
43-
e.on('unlimited', function() {});
43+
e.on('unlimited', common.noop);
4444
}
4545
assert.ok(!e._events['unlimited'].hasOwnProperty('warned'));
4646

@@ -49,26 +49,26 @@ events.EventEmitter.defaultMaxListeners = 42;
4949
e = new events.EventEmitter();
5050

5151
for (let i = 0; i < 42; ++i) {
52-
e.on('fortytwo', function() {});
52+
e.on('fortytwo', common.noop);
5353
}
5454
assert.ok(!e._events['fortytwo'].hasOwnProperty('warned'));
55-
e.on('fortytwo', function() {});
55+
e.on('fortytwo', common.noop);
5656
assert.ok(e._events['fortytwo'].hasOwnProperty('warned'));
5757
delete e._events['fortytwo'].warned;
5858

5959
events.EventEmitter.defaultMaxListeners = 44;
60-
e.on('fortytwo', function() {});
60+
e.on('fortytwo', common.noop);
6161
assert.ok(!e._events['fortytwo'].hasOwnProperty('warned'));
62-
e.on('fortytwo', function() {});
62+
e.on('fortytwo', common.noop);
6363
assert.ok(e._events['fortytwo'].hasOwnProperty('warned'));
6464

6565
// but _maxListeners still has precedence over defaultMaxListeners
6666
events.EventEmitter.defaultMaxListeners = 42;
6767
e = new events.EventEmitter();
6868
e.setMaxListeners(1);
69-
e.on('uno', function() {});
69+
e.on('uno', common.noop);
7070
assert.ok(!e._events['uno'].hasOwnProperty('warned'));
71-
e.on('uno', function() {});
71+
e.on('uno', common.noop);
7272
assert.ok(e._events['uno'].hasOwnProperty('warned'));
7373

7474
// chainable

test/parallel/test-event-emitter-get-max-listeners.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict';
2-
require('../common');
2+
const common = require('../common');
33
const assert = require('assert');
44
const EventEmitter = require('events');
55

@@ -15,5 +15,5 @@ assert.strictEqual(emitter.getMaxListeners(), 3);
1515

1616
// https://github.com/nodejs/node/issues/523 - second call should not throw.
1717
const recv = {};
18-
EventEmitter.prototype.on.call(recv, 'event', function() {});
19-
EventEmitter.prototype.on.call(recv, 'event', function() {});
18+
EventEmitter.prototype.on.call(recv, 'event', common.noop);
19+
EventEmitter.prototype.on.call(recv, 'event', common.noop);

test/parallel/test-event-emitter-listener-count.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
'use strict';
22

3-
require('../common');
3+
const common = require('../common');
44
const assert = require('assert');
55
const EventEmitter = require('events');
66

77
const emitter = new EventEmitter();
8-
emitter.on('foo', function() {});
9-
emitter.on('foo', function() {});
10-
emitter.on('baz', function() {});
8+
emitter.on('foo', common.noop);
9+
emitter.on('foo', common.noop);
10+
emitter.on('baz', common.noop);
1111
// Allow any type
12-
emitter.on(123, function() {});
12+
emitter.on(123, common.noop);
1313

1414
assert.strictEqual(EventEmitter.listenerCount(emitter, 'foo'), 2);
1515
assert.strictEqual(emitter.listenerCount('foo'), 2);

test/parallel/test-event-emitter-max-listeners-warning-for-null.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ process.on('warning', common.mustCall((warning) => {
1818
assert.ok(warning.message.includes('2 null listeners added.'));
1919
}));
2020

21-
e.on(null, function() {});
22-
e.on(null, function() {});
21+
e.on(null, common.noop);
22+
e.on(null, common.noop);

test/parallel/test-event-emitter-max-listeners-warning-for-symbol.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ process.on('warning', common.mustCall((warning) => {
2020
assert.ok(warning.message.includes('2 Symbol(symbol) listeners added.'));
2121
}));
2222

23-
e.on(symbol, function() {});
24-
e.on(symbol, function() {});
23+
e.on(symbol, common.noop);
24+
e.on(symbol, common.noop);

0 commit comments

Comments
 (0)