Skip to content

Commit dfd0215

Browse files
tniessentargos
authored andcommitted
test: avoid deep comparisons with literals
Comparing any value to any non-RegExp literal or undefined using strictEqual (or notStrictEqual) passes if and only if deepStrictEqual (or notDeepStrictEqual, respectively) passes. Unnecessarily using deep comparisons adds confusion. This patch adds an ESLint rule that forbids the use of deepStrictEqual and notDeepStrictEqual when the expected value (i.e., the second argument) is a non-RegExp literal or undefined. For reference, an ESTree literal is defined as follows. extend interface Literal <: Expression { type: "Literal"; value: string | boolean | null | number | RegExp | bigint; } The value `undefined` is an `Identifier` with `name: 'undefined'`. PR-URL: #40634 Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Voltrex <[email protected]>
1 parent 3805b80 commit dfd0215

Some content is hidden

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

45 files changed

+114
-110
lines changed

test/.eslintrc.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ rules:
1313
no-restricted-syntax:
1414
# Config copied from .eslintrc.js
1515
- error
16+
- selector: "CallExpression:matches([callee.name='deepStrictEqual'], [callee.property.name='deepStrictEqual']):matches([arguments.1.type='Literal']:not([arguments.1.regex]), [arguments.1.type='Identifier'][arguments.1.name='undefined'])"
17+
message: "Use strictEqual instead of deepStrictEqual for literals or undefined."
18+
- selector: "CallExpression:matches([callee.name='notDeepStrictEqual'], [callee.property.name='notDeepStrictEqual']):matches([arguments.1.type='Literal']:not([arguments.1.regex]), [arguments.1.type='Identifier'][arguments.1.name='undefined'])"
19+
message: "Use notStrictEqual instead of notDeepStrictEqual for literals or undefined."
1620
- selector: "CallExpression:matches([callee.name='deepStrictEqual'], [callee.property.name='deepStrictEqual'])[arguments.2.type='Literal']"
1721
message: "Do not use a literal for the third argument of assert.deepStrictEqual()"
1822
- selector: "CallExpression:matches([callee.name='doesNotThrow'], [callee.property.name='doesNotThrow'])"

test/es-module/test-esm-data-urls.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function createBase64URL(mime, body) {
1515
const plainESMURL = createURL('text/javascript', body);
1616
const ns = await import(plainESMURL);
1717
assert.deepStrictEqual(Object.keys(ns), ['default']);
18-
assert.deepStrictEqual(ns.default.a, 'aaa');
18+
assert.strictEqual(ns.default.a, 'aaa');
1919
const importerOfURL = createURL(
2020
'text/javascript',
2121
`export {default as default} from ${JSON.stringify(plainESMURL)}`
@@ -35,40 +35,40 @@ function createBase64URL(mime, body) {
3535
const plainESMURL = createURL('text/javascript', body);
3636
const ns = await import(plainESMURL);
3737
assert.deepStrictEqual(Object.keys(ns), ['default']);
38-
assert.deepStrictEqual(ns.default, plainESMURL);
38+
assert.strictEqual(ns.default, plainESMURL);
3939
}
4040
{
4141
const body = 'export default import.meta.url;';
4242
const plainESMURL = createURL('text/javascript;charset=UTF-8', body);
4343
const ns = await import(plainESMURL);
4444
assert.deepStrictEqual(Object.keys(ns), ['default']);
45-
assert.deepStrictEqual(ns.default, plainESMURL);
45+
assert.strictEqual(ns.default, plainESMURL);
4646
}
4747
{
4848
const body = 'export default import.meta.url;';
4949
const plainESMURL = createURL('text/javascript;charset="UTF-8"', body);
5050
const ns = await import(plainESMURL);
5151
assert.deepStrictEqual(Object.keys(ns), ['default']);
52-
assert.deepStrictEqual(ns.default, plainESMURL);
52+
assert.strictEqual(ns.default, plainESMURL);
5353
}
5454
{
5555
const body = 'export default import.meta.url;';
5656
const plainESMURL = createURL('text/javascript;;a=a;b=b;;', body);
5757
const ns = await import(plainESMURL);
5858
assert.deepStrictEqual(Object.keys(ns), ['default']);
59-
assert.deepStrictEqual(ns.default, plainESMURL);
59+
assert.strictEqual(ns.default, plainESMURL);
6060
}
6161
{
6262
const ns = await import('data:application/json;foo="test,"this"');
6363
assert.deepStrictEqual(Object.keys(ns), ['default']);
64-
assert.deepStrictEqual(ns.default, 'this');
64+
assert.strictEqual(ns.default, 'this');
6565
}
6666
{
6767
const ns = await import(`data:application/json;foo=${
6868
encodeURIComponent('test,')
6969
},0`);
7070
assert.deepStrictEqual(Object.keys(ns), ['default']);
71-
assert.deepStrictEqual(ns.default, 0);
71+
assert.strictEqual(ns.default, 0);
7272
}
7373
{
7474
await assert.rejects(async () => {
@@ -83,14 +83,14 @@ function createBase64URL(mime, body) {
8383
const plainESMURL = createURL('application/json', body);
8484
const ns = await import(plainESMURL);
8585
assert.deepStrictEqual(Object.keys(ns), ['default']);
86-
assert.deepStrictEqual(ns.default.x, 1);
86+
assert.strictEqual(ns.default.x, 1);
8787
}
8888
{
8989
const body = '{"default": 2}';
9090
const plainESMURL = createURL('application/json', body);
9191
const ns = await import(plainESMURL);
9292
assert.deepStrictEqual(Object.keys(ns), ['default']);
93-
assert.deepStrictEqual(ns.default.default, 2);
93+
assert.strictEqual(ns.default.default, 2);
9494
}
9595
{
9696
const body = 'null';

test/js-native-api/test_conversions/test.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,10 @@ assert.deepStrictEqual(new String(''), test.toObject(''));
118118
assert.deepStrictEqual(new Number(0), test.toObject(0));
119119
assert.deepStrictEqual(new Number(Number.NaN), test.toObject(Number.NaN));
120120
assert.deepStrictEqual(new Object(testSym), test.toObject(testSym));
121-
assert.notDeepStrictEqual(test.toObject(false), false);
122-
assert.notDeepStrictEqual(test.toObject(true), true);
123-
assert.notDeepStrictEqual(test.toObject(''), '');
124-
assert.notDeepStrictEqual(test.toObject(0), 0);
121+
assert.notStrictEqual(test.toObject(false), false);
122+
assert.notStrictEqual(test.toObject(true), true);
123+
assert.notStrictEqual(test.toObject(''), '');
124+
assert.notStrictEqual(test.toObject(0), 0);
125125
assert.ok(!Number.isNaN(test.toObject(Number.NaN)));
126126

127127
assert.strictEqual(test.toString(''), '');

test/parallel/test-assert-deep.js

+2
Original file line numberDiff line numberDiff line change
@@ -856,11 +856,13 @@ assert.throws(
856856
}
857857

858858
assert.throws(
859+
// eslint-disable-next-line no-restricted-syntax
859860
() => assert.deepStrictEqual(4, '4'),
860861
{ message: `${defaultMsgStart}\n4 !== '4'\n` }
861862
);
862863

863864
assert.throws(
865+
// eslint-disable-next-line no-restricted-syntax
864866
() => assert.deepStrictEqual(true, 1),
865867
{ message: `${defaultMsgStart}\ntrue !== 1\n` }
866868
);

test/parallel/test-assert.js

+1
Original file line numberDiff line numberDiff line change
@@ -1243,6 +1243,7 @@ assert.throws(
12431243
{
12441244
let threw = false;
12451245
try {
1246+
// eslint-disable-next-line no-restricted-syntax
12461247
assert.deepStrictEqual(Array(100).fill(1), 'foobar');
12471248
} catch (err) {
12481249
threw = true;

test/parallel/test-crypto-hmac.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -422,8 +422,8 @@ assert.strictEqual(
422422
}
423423
{
424424
const h = crypto.createHmac('sha1', 'key').update('data');
425-
assert.deepStrictEqual(h.digest('latin1'), expected);
426-
assert.deepStrictEqual(h.digest('latin1'), '');
425+
assert.strictEqual(h.digest('latin1'), expected);
426+
assert.strictEqual(h.digest('latin1'), '');
427427
}
428428
}
429429

@@ -440,8 +440,8 @@ assert.strictEqual(
440440
}
441441
{
442442
const h = crypto.createHmac('sha1', 'key');
443-
assert.deepStrictEqual(h.digest('latin1'), expected);
444-
assert.deepStrictEqual(h.digest('latin1'), '');
443+
assert.strictEqual(h.digest('latin1'), expected);
444+
assert.strictEqual(h.digest('latin1'), '');
445445
}
446446
}
447447

test/parallel/test-dns-lookup.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ dns.lookup('127.0.0.1', {
137137
family: 4,
138138
all: false
139139
}, common.mustSucceed((result, addressType) => {
140-
assert.deepStrictEqual(result, '127.0.0.1');
140+
assert.strictEqual(result, '127.0.0.1');
141141
assert.strictEqual(addressType, 4);
142142
}));
143143

test/parallel/test-dns.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -336,10 +336,10 @@ assert.throws(() => {
336336

337337
{
338338
dns.resolveMx('foo.onion', function(err) {
339-
assert.deepStrictEqual(err.code, 'ENOTFOUND');
340-
assert.deepStrictEqual(err.syscall, 'queryMx');
341-
assert.deepStrictEqual(err.hostname, 'foo.onion');
342-
assert.deepStrictEqual(err.message, 'queryMx ENOTFOUND foo.onion');
339+
assert.strictEqual(err.code, 'ENOTFOUND');
340+
assert.strictEqual(err.syscall, 'queryMx');
341+
assert.strictEqual(err.hostname, 'foo.onion');
342+
assert.strictEqual(err.message, 'queryMx ENOTFOUND foo.onion');
343343
});
344344
}
345345

test/parallel/test-error-serdes.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,5 @@ assert.strictEqual(cycle(Function), '[Function: Function]');
6464
}
6565

6666
serializeError(new DynamicError());
67-
assert.deepStrictEqual(called, true);
67+
assert.strictEqual(called, true);
6868
}

test/parallel/test-fs-promises-file-handle-chmod.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ async function validateFilePermission() {
1919
const fileHandle = await open(filePath, 'w+', 0o444);
2020
// File created with r--r--r-- 444
2121
const statsBeforeMod = fs.statSync(filePath);
22-
assert.deepStrictEqual(statsBeforeMod.mode & 0o444, 0o444);
22+
assert.strictEqual(statsBeforeMod.mode & 0o444, 0o444);
2323

2424
let expectedAccess;
2525
const newPermissions = 0o765;

test/parallel/test-fs-promises-file-handle-truncate.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ async function validateTruncate() {
1616
const buffer = Buffer.from(text, 'utf8');
1717
await fileHandle.write(buffer, 0, buffer.length);
1818

19-
assert.deepStrictEqual((await readFile(filename)).toString(), text);
19+
assert.strictEqual((await readFile(filename)).toString(), text);
2020

2121
await fileHandle.truncate(5);
22-
assert.deepStrictEqual((await readFile(filename)).toString(), 'Hello');
22+
assert.strictEqual((await readFile(filename)).toString(), 'Hello');
2323

2424
await fileHandle.close();
2525
}

test/parallel/test-fs-promises-readfile-with-fd.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ async function readFileTest() {
2222
const buf = Buffer.alloc(5);
2323
const { bytesRead } = await handle.read(buf, 0, 5, null);
2424
assert.strictEqual(bytesRead, 5);
25-
assert.deepStrictEqual(buf.toString(), 'Hello');
25+
assert.strictEqual(buf.toString(), 'Hello');
2626

2727
/* readFile() should read from position five, instead of zero. */
28-
assert.deepStrictEqual((await handle.readFile()).toString(), ' World');
28+
assert.strictEqual((await handle.readFile()).toString(), ' World');
2929

3030
await handle.close();
3131
}

test/parallel/test-fs-promises-writefile-with-fd.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ async function writeFileTest() {
2626
await handle.writeFile('World');
2727

2828
/* New content should be written at position five, instead of zero. */
29-
assert.deepStrictEqual(readFileSync(fn).toString(), 'HelloWorld');
29+
assert.strictEqual(readFileSync(fn).toString(), 'HelloWorld');
3030

3131
await handle.close();
3232
}

test/parallel/test-fs-promises.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ async function executeOnHandle(dest, func) {
183183
assert.strictEqual(ret.bytesRead, bufLen);
184184
assert.deepStrictEqual(ret.buffer, buf);
185185
await truncate(dest, 5);
186-
assert.deepStrictEqual((await readFile(dest)).toString(), 'hello');
186+
assert.strictEqual((await readFile(dest)).toString(), 'hello');
187187
});
188188
}
189189

test/parallel/test-fs-readfile-fd.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ function tempFdSync(callback) {
6464

6565
// Read only five bytes, so that the position moves to five.
6666
const buf = Buffer.alloc(5);
67-
assert.deepStrictEqual(fs.readSync(fd, buf, 0, 5), 5);
68-
assert.deepStrictEqual(buf.toString(), 'Hello');
67+
assert.strictEqual(fs.readSync(fd, buf, 0, 5), 5);
68+
assert.strictEqual(buf.toString(), 'Hello');
6969

7070
// readFileSync() should read from position five, instead of zero.
71-
assert.deepStrictEqual(fs.readFileSync(fd).toString(), ' World');
71+
assert.strictEqual(fs.readFileSync(fd).toString(), ' World');
7272

7373
fs.closeSync(fd);
7474
}
@@ -81,11 +81,11 @@ function tempFdSync(callback) {
8181
// Read only five bytes, so that the position moves to five.
8282
fs.read(fd, buf, 0, 5, null, common.mustSucceed((bytes) => {
8383
assert.strictEqual(bytes, 5);
84-
assert.deepStrictEqual(buf.toString(), 'Hello');
84+
assert.strictEqual(buf.toString(), 'Hello');
8585

8686
fs.readFile(fd, common.mustSucceed((data) => {
8787
// readFile() should read from position five, instead of zero.
88-
assert.deepStrictEqual(data.toString(), ' World');
88+
assert.strictEqual(data.toString(), ' World');
8989

9090
fs.closeSync(fd);
9191
}));

test/parallel/test-fs-readv-promises.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ const allocateEmptyBuffers = (combinedLength) => {
3535

3636
let { bytesRead, buffers } = await handle.readv([Buffer.from('')],
3737
null);
38-
assert.deepStrictEqual(bytesRead, 0);
38+
assert.strictEqual(bytesRead, 0);
3939
assert.deepStrictEqual(buffers, [Buffer.from('')]);
4040

4141
({ bytesRead, buffers } = await handle.readv(bufferArr, null));
42-
assert.deepStrictEqual(bytesRead, expectedLength);
42+
assert.strictEqual(bytesRead, expectedLength);
4343
assert.deepStrictEqual(buffers, bufferArr);
4444
assert(Buffer.concat(bufferArr).equals(await fs.readFile(filename)));
4545
handle.close();
@@ -54,11 +54,11 @@ const allocateEmptyBuffers = (combinedLength) => {
5454
const expectedLength = exptectedBuff.length;
5555

5656
let { bytesRead, buffers } = await handle.readv([Buffer.from('')]);
57-
assert.deepStrictEqual(bytesRead, 0);
57+
assert.strictEqual(bytesRead, 0);
5858
assert.deepStrictEqual(buffers, [Buffer.from('')]);
5959

6060
({ bytesRead, buffers } = await handle.readv(bufferArr));
61-
assert.deepStrictEqual(bytesRead, expectedLength);
61+
assert.strictEqual(bytesRead, expectedLength);
6262
assert.deepStrictEqual(buffers, bufferArr);
6363
assert(Buffer.concat(bufferArr).equals(await fs.readFile(filename)));
6464
handle.close();

test/parallel/test-fs-readv-sync.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ const allocateEmptyBuffers = (combinedLength) => {
3232
const bufferArr = allocateEmptyBuffers(exptectedBuff.length);
3333

3434
let read = fs.readvSync(fd, [Buffer.from('')], 0);
35-
assert.deepStrictEqual(read, 0);
35+
assert.strictEqual(read, 0);
3636

3737
read = fs.readvSync(fd, bufferArr, 0);
38-
assert.deepStrictEqual(read, expectedLength);
38+
assert.strictEqual(read, expectedLength);
3939

4040
fs.closeSync(fd);
4141

@@ -49,10 +49,10 @@ const allocateEmptyBuffers = (combinedLength) => {
4949
const bufferArr = allocateEmptyBuffers(exptectedBuff.length);
5050

5151
let read = fs.readvSync(fd, [Buffer.from('')]);
52-
assert.deepStrictEqual(read, 0);
52+
assert.strictEqual(read, 0);
5353

5454
read = fs.readvSync(fd, bufferArr);
55-
assert.deepStrictEqual(read, expectedLength);
55+
assert.strictEqual(read, expectedLength);
5656

5757
fs.closeSync(fd);
5858

test/parallel/test-fs-writefile-with-fd.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ tmpdir.refresh();
2020
const fd = fs.openSync(filename, 'w');
2121
try {
2222
/* Write only five characters, so that the position moves to five. */
23-
assert.deepStrictEqual(fs.writeSync(fd, 'Hello'), 5);
24-
assert.deepStrictEqual(fs.readFileSync(filename).toString(), 'Hello');
23+
assert.strictEqual(fs.writeSync(fd, 'Hello'), 5);
24+
assert.strictEqual(fs.readFileSync(filename).toString(), 'Hello');
2525

2626
/* Write some more with writeFileSync(). */
2727
fs.writeFileSync(fd, 'World');
2828

2929
/* New content should be written at position five, instead of zero. */
30-
assert.deepStrictEqual(fs.readFileSync(filename).toString(), 'HelloWorld');
30+
assert.strictEqual(fs.readFileSync(filename).toString(), 'HelloWorld');
3131
} finally {
3232
fs.closeSync(fd);
3333
}
@@ -54,12 +54,12 @@ process.on('beforeExit', common.mustCall(() => {
5454
/* Write only five characters, so that the position moves to five. */
5555
fs.write(fd, 'Hello', common.mustSucceed((bytes) => {
5656
assert.strictEqual(bytes, 5);
57-
assert.deepStrictEqual(fs.readFileSync(file).toString(), 'Hello');
57+
assert.strictEqual(fs.readFileSync(file).toString(), 'Hello');
5858

5959
/* Write some more with writeFile(). */
6060
fs.writeFile(fd, 'World', common.mustSucceed(() => {
6161
/* New content should be written at position five, instead of zero. */
62-
assert.deepStrictEqual(fs.readFileSync(file).toString(), 'HelloWorld');
62+
assert.strictEqual(fs.readFileSync(file).toString(), 'HelloWorld');
6363
}));
6464
}));
6565
}));

test/parallel/test-fs-writev-promises.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ tmpdir.refresh();
2222
const expectedLength = bufferArr.length * buffer.byteLength;
2323
let { bytesWritten, buffers } = await handle.writev([Buffer.from('')],
2424
null);
25-
assert.deepStrictEqual(bytesWritten, 0);
25+
assert.strictEqual(bytesWritten, 0);
2626
assert.deepStrictEqual(buffers, [Buffer.from('')]);
2727
({ bytesWritten, buffers } = await handle.writev(bufferArr, null));
2828
assert.deepStrictEqual(bytesWritten, expectedLength);
@@ -39,7 +39,7 @@ tmpdir.refresh();
3939
const bufferArr = [buffer, buffer, buffer];
4040
const expectedLength = bufferArr.length * buffer.byteLength;
4141
let { bytesWritten, buffers } = await handle.writev([Buffer.from('')]);
42-
assert.deepStrictEqual(bytesWritten, 0);
42+
assert.strictEqual(bytesWritten, 0);
4343
assert.deepStrictEqual(buffers, [Buffer.from('')]);
4444
({ bytesWritten, buffers } = await handle.writev(bufferArr));
4545
assert.deepStrictEqual(bytesWritten, expectedLength);

test/parallel/test-fs-writev-sync.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ const getFileName = (i) => path.join(tmpdir.path, `writev_sync_${i}.txt`);
2626
const expectedLength = bufferArr.length * buffer.byteLength;
2727

2828
let written = fs.writevSync(fd, [Buffer.from('')], null);
29-
assert.deepStrictEqual(written, 0);
29+
assert.strictEqual(written, 0);
3030

3131
written = fs.writevSync(fd, bufferArr, null);
32-
assert.deepStrictEqual(written, expectedLength);
32+
assert.strictEqual(written, expectedLength);
3333

3434
fs.closeSync(fd);
3535

@@ -46,10 +46,10 @@ const getFileName = (i) => path.join(tmpdir.path, `writev_sync_${i}.txt`);
4646
const expectedLength = bufferArr.length * buffer.byteLength;
4747

4848
let written = fs.writevSync(fd, [Buffer.from('')]);
49-
assert.deepStrictEqual(written, 0);
49+
assert.strictEqual(written, 0);
5050

5151
written = fs.writevSync(fd, bufferArr);
52-
assert.deepStrictEqual(written, expectedLength);
52+
assert.strictEqual(written, expectedLength);
5353

5454
fs.closeSync(fd);
5555

test/parallel/test-http-agent-keepalive.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ function second() {
8282
function remoteClose() {
8383
// Mock remote server close the socket
8484
const req = get('/remote_close', common.mustCall((res) => {
85-
assert.deepStrictEqual(req.reusedSocket, true);
86-
assert.deepStrictEqual(res.statusCode, 200);
85+
assert.strictEqual(req.reusedSocket, true);
86+
assert.strictEqual(res.statusCode, 200);
8787
res.on('data', checkDataAndSockets);
8888
res.on('end', common.mustCall(() => {
8989
assert.strictEqual(agent.sockets[name].length, 1);

0 commit comments

Comments
 (0)