Skip to content

Commit 4451309

Browse files
committed
test: add lint rule to forbid use of assert.ok(/regex/.test(…))
PR-URL: #60832 Reviewed-By: LiviaMedeiros <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Chengzhong Wu <[email protected]>
1 parent 9098093 commit 4451309

12 files changed

+43
-33
lines changed

test/common/benchmark.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function runBenchmark(name, env) {
4242

4343
for (let testIdx = 1; testIdx < splitTests.length - 1; testIdx++) {
4444
const lines = splitTests[testIdx].split('\n');
45-
assert.ok(/.+/.test(lines[0]));
45+
assert.match(lines[0], /.+/);
4646

4747
if (!lines[1].includes('group="')) {
4848
assert.strictEqual(lines.length, 2, `benchmark file not running exactly one configuration in test: ${stdout}`);

test/eslint.config_partial.mjs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,14 @@ export default [
109109
selector: 'CallExpression[callee.property.name="catch"]>:first-child:matches(CallExpression[callee.object.name="common"][callee.property.name="mustNotCall"], CallExpression[callee.name="mustNotCall"])',
110110
message: 'Calling `.catch(common.mustNotCall())` will not detect never-settling promises. Use `.then(common.mustCall())` instead.',
111111
},
112+
{
113+
selector: 'CallExpression[callee.type="MemberExpression"][callee.object.type="Identifier"][callee.object.name="assert"][callee.property.type="Identifier"][callee.property.name="ok"][arguments.0.type="CallExpression"][arguments.0.callee.type="MemberExpression"][arguments.0.callee.object.regex][arguments.0.callee.property.type="Identifier"][arguments.0.callee.property.name="test"]',
114+
message: 'Use assert.match instead',
115+
},
116+
{
117+
selector: 'CallExpression[callee.type="MemberExpression"][callee.object.type="Identifier"][callee.object.name="assert"][callee.property.type="Identifier"][callee.property.name="ok"][arguments.0.type="UnaryExpression"][arguments.0.operator="!"][arguments.0.argument.type="CallExpression"][arguments.0.argument.callee.type="MemberExpression"][arguments.0.argument.callee.object.regex][arguments.0.argument.callee.property.name="test"]',
118+
message: 'Use assert.doesNotMatch instead',
119+
},
112120
],
113121

114122
// Stylistic rules.

test/js-native-api/test_general/testV8Instanceof2.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ function InstanceTest(x, func) {
304304
const answer = addon.doInstanceOf(x, func);
305305
assert.strictEqual(correct_answers[correct_answer_index], answer);
306306
} catch (e) {
307-
assert.ok(/prototype/.test(e));
307+
assert.match(`${e}`, /prototype/);
308308
assert.strictEqual(correct_answers[correct_answer_index], except);
309309
}
310310
correct_answer_index++;

test/parallel/test-dns-resolver-max-timeout.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ const dgram = require('dgram');
99
-1,
1010
1.1,
1111
NaN,
12-
undefined,
1312
{},
1413
[],
1514
null,
@@ -18,11 +17,9 @@ const dgram = require('dgram');
1817
true,
1918
Infinity,
2019
].forEach((maxTimeout) => {
21-
try {
20+
assert.throws(() => {
2221
new dns.Resolver({ maxTimeout });
23-
} catch (e) {
24-
assert.ok(/ERR_OUT_OF_RANGE|ERR_INVALID_ARG_TYPE/i.test(e.code));
25-
}
22+
}, /ERR_OUT_OF_RANGE|ERR_INVALID_ARG_TYPE/i);
2623
});
2724

2825
const server = dgram.createSocket('udp4');

test/parallel/test-http-set-trailers.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ function testHttp10(port, callback) {
4343

4444
c.on('end', common.mustCall(() => {
4545
c.end();
46+
// Ensure no trailer being in HTTP/1.0 response
4647
assert.doesNotMatch(
4748
res_buffer,
4849
/x-foo/,
49-
`No trailer in HTTP/1.0 response. Response buffer: ${res_buffer}`
5050
);
5151
callback();
5252
}));
@@ -69,10 +69,10 @@ function testHttp11(port, callback) {
6969
res_buffer += chunk;
7070
if (/0\r\n/.test(res_buffer)) { // got the end.
7171
clearTimeout(tid);
72+
// Ensure trailer being in HTTP/1.1 response
7273
assert.match(
7374
res_buffer,
7475
/0\r\nx-foo: bar\r\n\r\n$/,
75-
`No trailer in HTTP/1.1 response. Response buffer: ${res_buffer}`
7676
);
7777
callback();
7878
}

test/parallel/test-http2-https-fallback.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ function onSession(session, next) {
165165
.setEncoding('utf8')
166166
.on('data', (chunk) => text += chunk)
167167
.on('end', common.mustCall(() => {
168-
assert.ok(/Missing ALPN Protocol, expected `h2` to be available/.test(text));
168+
assert.match(text, /Missing ALPN Protocol, expected `h2` to be available/);
169169
cleanup();
170170
}));
171171
}

test/parallel/test-inspector.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ function checkListResponse(response) {
1414
`Expected response length ${response.length} to be ${expectedLength}.`
1515
);
1616
assert.ok(response[0].devtoolsFrontendUrl);
17-
assert.ok(
18-
/ws:\/\/localhost:\d+\/[0-9A-Fa-f]{8}-/
19-
.test(response[0].webSocketDebuggerUrl),
20-
response[0].webSocketDebuggerUrl);
17+
assert.match(
18+
response[0].webSocketDebuggerUrl,
19+
/ws:\/\/localhost:\d+\/[0-9A-Fa-f]{8}-/,
20+
);
2121
}
2222

2323
function checkVersion(response) {

test/parallel/test-runner-mock-timers-date.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,10 @@ describe('Mock Timers Date Test Suite', () => {
6262
const returned = Date();
6363
// Matches the format: 'Mon Jan 01 1970 00:00:00'
6464
// We don't care about the date, just the format
65-
assert.ok(/\w{3}\s\w{3}\s\d{1,2}\s\d{2,4}\s\d{1,2}:\d{2}:\d{2}/.test(returned));
65+
assert.match(
66+
returned,
67+
/\w{3}\s\w{3}\s\d{1,2}\s\d{2,4}\s\d{1,2}:\d{2}:\d{2}/,
68+
);
6669
});
6770

6871
it('should return the date with different argument calls', (t) => {

test/parallel/test-tls-server-failed-handshake-emits-clienterror.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ const server = tls.createServer({})
2020
}).on('tlsClientError', common.mustCall(function(e) {
2121
assert.ok(e instanceof Error,
2222
'Instance of Error should be passed to error handler');
23-
assert.ok(
24-
/SSL routines:[^:]*:wrong version number/.test(
25-
e.message),
26-
'Expecting SSL unknown protocol');
23+
assert.match(
24+
e.message,
25+
/SSL routines:[^:]*:wrong version number/,
26+
);
2727

2828
server.close();
2929
}));

test/parallel/test-tls-socket-failed-handshake-emits-error.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ const server = net.createServer(common.mustCall((c) => {
2020
s.on('error', common.mustCall(function(e) {
2121
assert.ok(e instanceof Error,
2222
'Instance of Error should be passed to error handler');
23-
assert.ok(
24-
/SSL routines:[^:]*:wrong version number/.test(
25-
e.message),
26-
'Expecting SSL unknown protocol');
23+
assert.match(
24+
e.message,
25+
/SSL routines:[^:]*:wrong version number/,
26+
);
2727
}));
2828

2929
s.on('close', function() {

0 commit comments

Comments
 (0)