Skip to content

Commit e879a56

Browse files
Trottaddaleax
authored andcommitted
test: remove common.noop
This change removes `common.noop` from the Node.js internal testing common module. Over the last few weeks, I've grown to dislike the `common.noop` abstraction. First, new (and experienced) contributors are unaware of it and so it results in a large number of low-value nits on PRs. It also increases the number of things newcomers and infrequent contributors have to be aware of to be effective on the project. Second, it is confusing. Is it a singleton/property or a getter? Which should be expected? This can lead to subtle and hard-to-find bugs. (To my knowledge, none have landed on master. But I also think it's only a matter of time.) Third, the abstraction is low-value in my opinion. What does it really get us? A case could me made that it is without value at all. Lastly, and this is minor, but the abstraction is wordier than not using the abstraction. `common.noop` doesn't save anything over `() => {}`. So, I propose removing it. PR-URL: #12822 Backport-PR-URL: #14174 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Refael Ackermann <[email protected]>
1 parent 76ba1b5 commit e879a56

File tree

67 files changed

+135
-149
lines changed

Some content is hidden

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

67 files changed

+135
-149
lines changed

test/common/README.md

+4-17
Original file line numberDiff line numberDiff line change
@@ -209,26 +209,26 @@ Gets IP of localhost
209209
Array of IPV6 hosts.
210210

211211
### mustCall([fn][, exact])
212-
* `fn` [&lt;Function>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function) default = `common.noop`
212+
* `fn` [&lt;Function>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function) default = () => {}
213213
* `exact` [&lt;Number>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type) default = 1
214214
* return [&lt;Function>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function)
215215

216216
Returns a function that calls `fn`. If the returned function has not been called
217217
exactly `expected` number of times when the test is complete, then the test will
218218
fail.
219219

220-
If `fn` is not provided, `common.noop` will be used.
220+
If `fn` is not provided, an empty function will be used.
221221

222222
### mustCallAtLeast([fn][, minimum])
223-
* `fn` [&lt;Function>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function) default = `common.noop`
223+
* `fn` [&lt;Function>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function) default = () => {}
224224
* `minimum` [&lt;Number>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type) default = 1
225225
* return [&lt;Function>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function)
226226

227227
Returns a function that calls `fn`. If the returned function has not been called
228228
at least `minimum` number of times when the test is complete, then the test will
229229
fail.
230230

231-
If `fn` is not provided, `common.noop` will be used.
231+
If `fn` is not provided, an empty function will be used.
232232

233233
### mustNotCall([msg])
234234
* `msg` [&lt;String>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type) default = 'function should not have been called'
@@ -243,19 +243,6 @@ Returns a function that triggers an `AssertionError` if it is invoked. `msg` is
243243

244244
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, `false` otherwise.
245245

246-
### noop
247-
248-
A non-op `Function` that can be used for a variety of scenarios.
249-
250-
For instance,
251-
252-
<!-- eslint-disable strict, no-undef -->
253-
```js
254-
const common = require('../common');
255-
256-
someAsyncAPI('foo', common.mustCall(common.noop));
257-
```
258-
259246
### opensslCli
260247
* return [&lt;Boolean>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type)
261248

test/common/index.js

-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ const testRoot = process.env.NODE_TEST_DIR ?
3737

3838
const noop = () => {};
3939

40-
exports.noop = noop;
4140
exports.fixturesDir = path.join(__dirname, '..', 'fixtures');
4241
exports.tmpDirName = 'tmp';
4342
// PORT should match the definition in test/testpy/__init__.py.
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Flags: --trace-warnings
22
'use strict';
3-
const common = require('../common');
3+
require('../common');
44
const p = Promise.reject(new Error('This was rejected'));
5-
setImmediate(() => p.catch(common.noop));
5+
setImmediate(() => p.catch(() => {}));

test/parallel/test-assert.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -571,29 +571,30 @@ a.throws(makeBlock(a.deepEqual, args, []));
571571

572572
// check messages from assert.throws()
573573
{
574+
const noop = () => {};
574575
assert.throws(
575-
() => { a.throws(common.noop); },
576+
() => { a.throws((noop)); },
576577
common.expectsError({
577578
code: 'ERR_ASSERTION',
578579
message: /^Missing expected exception\.$/
579580
}));
580581

581582
assert.throws(
582-
() => { a.throws(common.noop, TypeError); },
583+
() => { a.throws(noop, TypeError); },
583584
common.expectsError({
584585
code: 'ERR_ASSERTION',
585586
message: /^Missing expected exception \(TypeError\)\.$/
586587
}));
587588

588589
assert.throws(
589-
() => { a.throws(common.noop, 'fhqwhgads'); },
590+
() => { a.throws(noop, 'fhqwhgads'); },
590591
common.expectsError({
591592
code: 'ERR_ASSERTION',
592593
message: /^Missing expected exception: fhqwhgads$/
593594
}));
594595

595596
assert.throws(
596-
() => { a.throws(common.noop, TypeError, 'fhqwhgads'); },
597+
() => { a.throws(noop, TypeError, 'fhqwhgads'); },
597598
common.expectsError({
598599
code: 'ERR_ASSERTION',
599600
message: /^Missing expected exception \(TypeError\): fhqwhgads$/

test/parallel/test-buffer-includes.js

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

55
const b = Buffer.from('abcdef');
@@ -274,7 +274,7 @@ for (let lengthIndex = 0; lengthIndex < lengths.length; lengthIndex++) {
274274
const expectedError =
275275
/^TypeError: "val" argument must be string, number, Buffer or Uint8Array$/;
276276
assert.throws(() => {
277-
b.includes(common.noop);
277+
b.includes(() => {});
278278
}, expectedError);
279279
assert.throws(() => {
280280
b.includes({});

test/parallel/test-child-process-bad-stdio.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const assert = require('assert');
55
const cp = require('child_process');
66

77
if (process.argv[2] === 'child') {
8-
setTimeout(common.noop, common.platformTimeout(100));
8+
setTimeout(() => {}, common.platformTimeout(100));
99
return;
1010
}
1111

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const assert = require('assert');
44
const cp = require('child_process');
55

66
if (process.argv[2] === 'child') {
7-
setInterval(common.noop, 1000);
7+
setInterval(() => {}, 1000);
88
} else {
99
const { SIGKILL } = process.binding('constants').os.signals;
1010

test/parallel/test-cluster-rr-domain-listen.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

2222
'use strict';
23-
const common = require('../common');
23+
require('../common');
2424
const cluster = require('cluster');
2525
const domain = require('domain');
2626

@@ -29,10 +29,10 @@ const domain = require('domain');
2929

3030
if (cluster.isWorker) {
3131
const d = domain.create();
32-
d.run(common.noop);
32+
d.run(() => {});
3333

3434
const http = require('http');
35-
http.Server(common.noop).listen(0, '127.0.0.1');
35+
http.Server(() => {}).listen(0, '127.0.0.1');
3636

3737
} else if (cluster.isMaster) {
3838

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ if (cluster.isWorker) {
1111
const server = net.createServer(function(socket) {
1212
// Wait for any data, then close connection
1313
socket.write('.');
14-
socket.on('data', common.noop);
14+
socket.on('data', () => {});
1515
}).listen(0, common.localhostIPv4);
1616

1717
server.once('close', function() {
@@ -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(common.noop, 9999);
23+
const keepOpen = setInterval(() => {}, 9999);
2424

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

test/parallel/test-common.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ const HIJACK_TEST_ARRAY = [ 'foo\n', 'bar\n', 'baz\n' ];
101101
assert.notStrictEqual(originalWrite, stream.write);
102102

103103
HIJACK_TEST_ARRAY.forEach((val) => {
104-
stream.write(val, common.mustCall(common.noop));
104+
stream.write(val, common.mustCall());
105105
});
106106

107107
assert.strictEqual(HIJACK_TEST_ARRAY.length, stream.writeTimes);

test/parallel/test-console-instance.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ assert.throws(() => {
4343

4444
// Console constructor should throw if stderr exists but is not writable
4545
assert.throws(() => {
46-
out.write = common.noop;
46+
out.write = () => {};
4747
err.write = undefined;
4848
new Console(out, err);
4949
}, /^TypeError: Console expects writable stream instances$/);

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-
const common = require('../common');
2+
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', common.noop);
19-
EventEmitter.prototype.on.call(recv, 'event', common.noop);
18+
EventEmitter.prototype.on.call(recv, 'event', () => {});
19+
EventEmitter.prototype.on.call(recv, 'event', () => {});

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-
const common = require('../common');
3+
require('../common');
44
const assert = require('assert');
55
const EventEmitter = require('events');
66

77
const emitter = new EventEmitter();
8-
emitter.on('foo', common.noop);
9-
emitter.on('foo', common.noop);
10-
emitter.on('baz', common.noop);
8+
emitter.on('foo', () => {});
9+
emitter.on('foo', () => {});
10+
emitter.on('baz', () => {});
1111
// Allow any type
12-
emitter.on(123, common.noop);
12+
emitter.on(123, () => {});
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, common.noop);
22-
e.on(null, common.noop);
21+
e.on(null, () => {});
22+
e.on(null, () => {});

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, common.noop);
24-
e.on(symbol, common.noop);
23+
e.on(symbol, () => {});
24+
e.on(symbol, () => {});

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

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

21-
e.on('event-type', common.noop);
22-
e.on('event-type', common.noop); // Trigger warning.
23-
e.on('event-type', common.noop); // Verify that warning is emitted only once.
21+
e.on('event-type', () => {});
22+
e.on('event-type', () => {}); // Trigger warning.
23+
e.on('event-type', () => {}); // Verify that warning is emitted only once.

test/parallel/test-event-emitter-remove-listeners.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ function listener2() {}
140140
{
141141
const ee = new EventEmitter();
142142

143-
assert.deepStrictEqual(ee, ee.removeListener('foo', common.noop));
143+
assert.deepStrictEqual(ee, ee.removeListener('foo', () => {}));
144144
}
145145

146146
// Verify that the removed listener must be a function
@@ -152,7 +152,7 @@ assert.throws(() => {
152152

153153
{
154154
const ee = new EventEmitter();
155-
const listener = common.noop;
155+
const listener = () => {};
156156
ee._events = undefined;
157157
const e = ee.removeListener('foo', listener);
158158
assert.strictEqual(e, ee);

test/parallel/test-event-emitter-special-event-names.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const EventEmitter = require('events');
55
const assert = require('assert');
66

77
const ee = new EventEmitter();
8-
const handler = common.noop;
8+
const handler = () => {};
99

1010
assert.deepStrictEqual(ee.eventNames(), []);
1111

test/parallel/test-event-emitter-subclass.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,6 @@ MyEE2.prototype = new EventEmitter();
6262
const ee1 = new MyEE2();
6363
const ee2 = new MyEE2();
6464

65-
ee1.on('x', common.noop);
65+
ee1.on('x', () => {});
6666

6767
assert.strictEqual(ee2.listenerCount('x'), 0);

test/parallel/test-events-list.js

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const EventEmitter = require('events');
55
const assert = require('assert');
66

77
const EE = new EventEmitter();
8-
// Do not use common.noop here, these need to be separate listener functions
98
const m = () => {};
109
EE.on('foo', () => {});
1110
assert.deepStrictEqual(['foo'], EE.eventNames());

test/parallel/test-fs-buffertype-writesync.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const fs = require('fs');
99
const path = require('path');
1010

1111
const filePath = path.join(common.tmpDir, 'test_buffer_type');
12-
const v = [true, false, 0, 1, Infinity, common.noop, {}, [], undefined, null];
12+
const v = [true, false, 0, 1, Infinity, () => {}, {}, [], undefined, null];
1313

1414
common.refreshTmpDir();
1515

test/parallel/test-fs-mkdir.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,4 @@ common.refreshTmpDir();
7777

7878
// Keep the event loop alive so the async mkdir() requests
7979
// have a chance to run (since they don't ref the event loop).
80-
process.nextTick(common.noop);
80+
process.nextTick(() => {});

test/parallel/test-handle-wrap-isrefed.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ const strictEqual = require('assert').strictEqual;
8787
// tcp
8888
{
8989
const net = require('net');
90-
const server = net.createServer(common.noop).listen(0);
90+
const server = net.createServer(() => {}).listen(0);
9191
strictEqual(Object.getPrototypeOf(server._handle).hasOwnProperty('hasRef'),
9292
true, 'tcp_wrap: hasRef() missing');
9393
strictEqual(server._handle.hasRef(),
@@ -112,7 +112,7 @@ const strictEqual = require('assert').strictEqual;
112112

113113
// timers
114114
{
115-
const timer = setTimeout(common.noop, 500);
115+
const timer = setTimeout(() => {}, 500);
116116
timer.unref();
117117
strictEqual(Object.getPrototypeOf(timer._handle).hasOwnProperty('hasRef'),
118118
true, 'timer_wrap: hasRef() missing');
+4-4
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
'use strict';
22

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

77
{
8-
const req = new ClientRequest({ createConnection: common.noop });
8+
const req = new ClientRequest({ createConnection: () => {} });
99
assert.strictEqual(req.path, '/');
1010
assert.strictEqual(req.method, 'GET');
1111
}
1212

1313
{
14-
const req = new ClientRequest({ method: '', createConnection: common.noop });
14+
const req = new ClientRequest({ method: '', createConnection: () => {} });
1515
assert.strictEqual(req.path, '/');
1616
assert.strictEqual(req.method, 'GET');
1717
}
1818

1919
{
20-
const req = new ClientRequest({ path: '', createConnection: common.noop });
20+
const req = new ClientRequest({ path: '', createConnection: () => {} });
2121
assert.strictEqual(req.path, '/');
2222
assert.strictEqual(req.method, 'GET');
2323
}

test/parallel/test-http-parser-bad-ref.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
// Flags: --expose_gc
66

7-
const common = require('../common');
7+
require('../common');
88
const assert = require('assert');
99
const HTTPParser = process.binding('http_parser').HTTPParser;
1010

@@ -39,7 +39,7 @@ function demoBug(part1, part2) {
3939
console.log('url', info.url);
4040
};
4141

42-
parser[kOnBody] = common.noop;
42+
parser[kOnBody] = () => {};
4343

4444
parser[kOnMessageComplete] = function() {
4545
messagesComplete++;

test/parallel/test-http-upgrade-server.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
'use strict';
2323

24-
const common = require('../common');
24+
require('../common');
2525
const assert = require('assert');
2626

2727
const util = require('util');
@@ -38,7 +38,7 @@ function createTestServer() {
3838
}
3939

4040
function testServer() {
41-
http.Server.call(this, common.noop);
41+
http.Server.call(this, () => {});
4242

4343
this.on('connection', function() {
4444
requests_recv++;

0 commit comments

Comments
 (0)