Skip to content

Commit 688e5ed

Browse files
TrottMylesBorins
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 40a61e1 commit 688e5ed

File tree

62 files changed

+153
-159
lines changed

Some content is hidden

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

62 files changed

+153
-159
lines changed

test/common/README.md

+4-17
Original file line numberDiff line numberDiff line change
@@ -183,26 +183,26 @@ Gets IP of localhost
183183
Array of IPV6 hosts.
184184

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

190190
Returns a function that calls `fn`. If the returned function has not been called
191191
exactly `expected` number of times when the test is complete, then the test will
192192
fail.
193193

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

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

201201
Returns a function that calls `fn`. If the returned function has not been called
202202
at least `minimum` number of times when the test is complete, then the test will
203203
fail.
204204

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

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

218218
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.
219219

220-
### noop
221-
222-
A non-op `Function` that can be used for a variety of scenarios.
223-
224-
For instance,
225-
226-
<!-- eslint-disable strict, no-undef -->
227-
```js
228-
const common = require('../common');
229-
230-
someAsyncAPI('foo', common.mustCall(common.noop));
231-
```
232-
233220
### opensslCli
234221
* return [&lt;Boolean>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type)
235222

test/common/index.js

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

1515
const noop = () => {};
1616

17-
exports.noop = noop;
1817
exports.fixturesDir = path.join(__dirname, '..', 'fixtures');
1918
exports.tmpDirName = 'tmp';
2019
// PORT should match the definition in test/testpy/__init__.py.

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(common.noop, 1000000);';
132+
'setInterval(() => {}, 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(common.noop);
175+
c.reqContinue(() => {});
176176
});
177177
c.on('ready', function() {
178178
connectCount++;

test/parallel/test-assert.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
const a = require('assert');
55

@@ -515,7 +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]');
518+
testAssertionMessage(() => {}, '[Function]');
519519
testAssertionMessage({}, '{}');
520520
testAssertionMessage(circular, '{ y: 1, x: [Circular] }');
521521
testAssertionMessage({a: undefined, b: null}, '{ a: undefined, b: null }');

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ const domain = require('domain');
88

99
if (cluster.isWorker) {
1010
const d = domain.create();
11-
d.run(function() { });
11+
d.run(() => {});
1212

1313
const http = require('http');
14-
http.Server(function() { }).listen(0, '127.0.0.1');
14+
http.Server(() => {}).listen(0, '127.0.0.1');
1515

1616
} else if (cluster.isMaster) {
1717
let worker;

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', function discard() {});
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

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

1212
assert.throws(function() {
13-
common.mustCall(common.noop, 'foo');
13+
common.mustCall(() => {}, 'foo');
1414
}, /^TypeError: Invalid exact value: foo$/);
1515

1616
assert.throws(function() {
17-
common.mustCall(common.noop, /foo/);
17+
common.mustCall(() => {}, /foo/);
1818
}, /^TypeError: Invalid exact value: \/foo\/$/);
1919

2020
const fnOnce = common.mustCall(() => {});

test/parallel/test-crypto-random.js

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

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

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: common.noop });
32+
test({ NODE_NO_WARNINGS: () => {} });
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-
const common = require('../common');
2+
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', common.noop);
10+
e.on('default', () => {});
1111
}
1212
assert.ok(!e._events['default'].hasOwnProperty('warned'));
13-
e.on('default', common.noop);
13+
e.on('default', () => {});
1414
assert.ok(e._events['default'].warned);
1515

1616
// symbol
1717
const symbol = Symbol('symbol');
1818
e.setMaxListeners(1);
19-
e.on(symbol, common.noop);
19+
e.on(symbol, () => {});
2020
assert.ok(!e._events[symbol].hasOwnProperty('warned'));
21-
e.on(symbol, common.noop);
21+
e.on(symbol, () => {});
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', common.noop);
27+
e.on('specific', () => {});
2828
}
2929
assert.ok(!e._events['specific'].hasOwnProperty('warned'));
30-
e.on('specific', common.noop);
30+
e.on('specific', () => {});
3131
assert.ok(e._events['specific'].warned);
3232

3333
// only one
3434
e.setMaxListeners(1);
35-
e.on('only one', common.noop);
35+
e.on('only one', () => {});
3636
assert.ok(!e._events['only one'].hasOwnProperty('warned'));
37-
e.on('only one', common.noop);
37+
e.on('only one', () => {});
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', common.noop);
43+
e.on('unlimited', () => {});
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', common.noop);
52+
e.on('fortytwo', () => {});
5353
}
5454
assert.ok(!e._events['fortytwo'].hasOwnProperty('warned'));
55-
e.on('fortytwo', common.noop);
55+
e.on('fortytwo', () => {});
5656
assert.ok(e._events['fortytwo'].hasOwnProperty('warned'));
5757
delete e._events['fortytwo'].warned;
5858

5959
events.EventEmitter.defaultMaxListeners = 44;
60-
e.on('fortytwo', common.noop);
60+
e.on('fortytwo', () => {});
6161
assert.ok(!e._events['fortytwo'].hasOwnProperty('warned'));
62-
e.on('fortytwo', common.noop);
62+
e.on('fortytwo', () => {});
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', common.noop);
69+
e.on('uno', () => {});
7070
assert.ok(!e._events['uno'].hasOwnProperty('warned'));
71-
e.on('uno', common.noop);
71+
e.on('uno', () => {});
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-
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-all-listeners.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ function listener() {}
7070
ee.on('removeListener', function(name, listener) {
7171
assert.strictEqual(expectLength--, this.listeners('baz').length);
7272
});
73-
ee.on('baz', common.noop);
74-
ee.on('baz', common.noop);
75-
ee.on('baz', common.noop);
73+
ee.on('baz', () => {});
74+
ee.on('baz', () => {});
75+
ee.on('baz', () => {});
7676
assert.strictEqual(ee.listeners('baz').length, expectLength + 1);
7777
ee.removeAllListeners('baz');
7878
assert.strictEqual(ee.listeners('baz').length, 0);

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,6 @@ MyEE2.prototype = new EventEmitter();
4141
const ee1 = new MyEE2();
4242
const ee2 = new MyEE2();
4343

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

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

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
@@ -56,4 +56,4 @@ common.refreshTmpDir();
5656

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

test/parallel/test-fs-read-stream-inherit.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ let paused = false;
139139
let file7 =
140140
fs.createReadStream(rangeFile, Object.create({autoClose: false }));
141141
assert.strictEqual(file7.autoClose, false);
142-
file7.on('data', common.noop);
142+
file7.on('data', () => {});
143143
file7.on('end', common.mustCall(function() {
144144
process.nextTick(common.mustCall(function() {
145145
assert(!file7.closed);
@@ -169,7 +169,7 @@ let paused = false;
169169
{
170170
const options = Object.create({fd: 13337, autoClose: false});
171171
const file8 = fs.createReadStream(null, options);
172-
file8.on('data', common.noop);
172+
file8.on('data', () => {});
173173
file8.on('error', common.mustCall());
174174
process.on('exit', function() {
175175
assert(!file8.closed);
@@ -181,7 +181,7 @@ let paused = false;
181181
// Make sure stream is destroyed when file does not exist.
182182
{
183183
const file9 = fs.createReadStream('/path/to/file/that/does/not/exist');
184-
file9.on('data', common.noop);
184+
file9.on('data', () => {});
185185
file9.on('error', common.mustCall());
186186

187187
process.on('exit', function() {

test/parallel/test-fs-read-stream.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ pauseRes.pause();
138138
pauseRes.resume();
139139

140140
let file7 = fs.createReadStream(rangeFile, {autoClose: false });
141-
file7.on('data', common.noop);
141+
file7.on('data', () => {});
142142
file7.on('end', function() {
143143
process.nextTick(function() {
144144
assert(!file7.closed);
@@ -161,12 +161,12 @@ function file7Next() {
161161

162162
// Just to make sure autoClose won't close the stream because of error.
163163
const file8 = fs.createReadStream(null, {fd: 13337, autoClose: false });
164-
file8.on('data', common.noop);
164+
file8.on('data', () => {});
165165
file8.on('error', common.mustCall());
166166

167167
// Make sure stream is destroyed when file does not exist.
168168
const file9 = fs.createReadStream('/path/to/file/that/does/not/exist');
169-
file9.on('data', common.noop);
169+
file9.on('data', () => {});
170170
file9.on('error', common.mustCall());
171171

172172
process.on('exit', function() {

0 commit comments

Comments
 (0)