Skip to content

Commit 2cdc4a5

Browse files
committed
lib,test: enforce use of assert.fail via a lint rule
1 parent 0ffa607 commit 2cdc4a5

17 files changed

+40
-35
lines changed

β€Ždoc/api/assert.mdβ€Ž

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1682,6 +1682,8 @@ If no arguments are passed in at all `message` will be set to the string:
16821682
Be aware that in the `repl` the error message will be different to the one
16831683
thrown in a file! See below for further details.
16841684

1685+
<!-- eslint-skip -->
1686+
16851687
```mjs
16861688
import assert from 'node:assert/strict';
16871689

@@ -1717,6 +1719,8 @@ assert.ok(0);
17171719
// assert.ok(0)
17181720
```
17191721

1722+
<!-- eslint-skip -->
1723+
17201724
```cjs
17211725
const assert = require('node:assert/strict');
17221726

@@ -1756,20 +1760,20 @@ assert.ok(0);
17561760
import assert from 'node:assert/strict';
17571761

17581762
// Using `assert()` works the same:
1759-
assert(0);
1763+
assert(2 + 2 > 5);
17601764
// AssertionError: The expression evaluated to a falsy value:
17611765
//
1762-
// assert(0)
1766+
// assert(2 + 2 > 5)
17631767
```
17641768

17651769
```cjs
17661770
const assert = require('node:assert');
17671771

17681772
// Using `assert()` works the same:
1769-
assert(0);
1773+
assert(2 + 2 > 5);
17701774
// AssertionError: The expression evaluated to a falsy value:
17711775
//
1772-
// assert(0)
1776+
// assert(2 + 2 > 5)
17731777
```
17741778

17751779
## `assert.rejects(asyncFn[, error][, message])`

β€Ždoc/api/test.mdβ€Ž

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1786,7 +1786,7 @@ This function creates a hook that runs before executing a suite.
17861786
describe('tests', async () => {
17871787
before(() => console.log('about to run some test'));
17881788
it('is a subtest', () => {
1789-
assert.ok('some relevant assertion here');
1789+
// Some relevant assertions here
17901790
});
17911791
});
17921792
```
@@ -1816,7 +1816,7 @@ This function creates a hook that runs after executing a suite.
18161816
describe('tests', async () => {
18171817
after(() => console.log('finished running tests'));
18181818
it('is a subtest', () => {
1819-
assert.ok('some relevant assertion here');
1819+
// Some relevant assertion here
18201820
});
18211821
});
18221822
```
@@ -1849,7 +1849,7 @@ This function creates a hook that runs before each test in the current suite.
18491849
describe('tests', async () => {
18501850
beforeEach(() => console.log('about to run a test'));
18511851
it('is a subtest', () => {
1852-
assert.ok('some relevant assertion here');
1852+
// Some relevant assertion here
18531853
});
18541854
});
18551855
```
@@ -1880,7 +1880,7 @@ The `afterEach()` hook is run even if the test fails.
18801880
describe('tests', async () => {
18811881
afterEach(() => console.log('finished running a test'));
18821882
it('is a subtest', () => {
1883-
assert.ok('some relevant assertion here');
1883+
// Some relevant assertion here
18841884
});
18851885
});
18861886
```
@@ -3472,7 +3472,7 @@ test('top level test', async (t) => {
34723472
await t.test(
34733473
'This is a subtest',
34743474
(t) => {
3475-
assert.ok('some relevant assertion here');
3475+
// Some relevant assertion here
34763476
},
34773477
);
34783478
});
@@ -3503,7 +3503,7 @@ finishes.
35033503
```js
35043504
test('top level test', async (t) => {
35053505
t.after((t) => t.diagnostic(`finished running ${t.name}`));
3506-
assert.ok('some relevant assertion here');
3506+
// Some relevant assertion here
35073507
});
35083508
```
35093509

@@ -3535,7 +3535,7 @@ test('top level test', async (t) => {
35353535
await t.test(
35363536
'This is a subtest',
35373537
(t) => {
3538-
assert.ok('some relevant assertion here');
3538+
// Some relevant assertion here
35393539
},
35403540
);
35413541
});

β€Žlib/internal/crypto/cipher.jsβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ function getDecoder(decoder, encoding) {
9797
if (normalizedEncoding === undefined) {
9898
throw new ERR_UNKNOWN_ENCODING(encoding);
9999
}
100-
assert(false, 'Cannot change encoding');
100+
assert.fail('Cannot change encoding');
101101
}
102102
return decoder;
103103
}

β€Žtest/abort/test-abort-fatal-error.jsβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ exec(...cmdline, common.mustCall((err, stdout, stderr) => {
3636
if (!err) {
3737
console.log(stdout);
3838
console.log(stderr);
39-
assert(false, 'this test should fail');
39+
assert.fail('this test should fail');
4040
}
4141

4242
assert(common.nodeProcessAborted(err.code, err.signal));

β€Žtest/message/internal_assert.jsβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
require('../common');
55

66
const assert = require('internal/assert');
7-
assert(false);
7+
assert(2 + 2 > 5);

β€Žtest/parallel/test-cli-node-options-docs.jsβ€Ž

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,19 +87,19 @@ for (const [, envVar, config] of nodeOptionsCC.matchAll(addOptionRE)) {
8787
// CLI options
8888
if (!isV8Option && !hasTrueAsDefaultValue) {
8989
if (new RegExp(`###.*\`${envVar}[[=\\s\\b\`]`).test(cliText) === false) {
90-
assert(false, `Should have option ${envVar} documented`);
90+
assert.fail(`Should have option ${envVar} documented`);
9191
} else {
9292
manPagesOptions.delete(envVar.slice(1));
9393
}
9494
}
9595

9696
if (!hasTrueAsDefaultValue && new RegExp(`###.*\`--no${envVar.slice(1)}[[=\\s\\b\`]`).test(cliText) === true) {
97-
assert(false, `Should not have option --no${envVar.slice(1)} documented`);
97+
assert.fail(`Should not have option --no${envVar.slice(1)} documented`);
9898
}
9999

100100
if (!isV8Option && hasTrueAsDefaultValue) {
101101
if (new RegExp(`###.*\`--no${envVar.slice(1)}[[=\\s\\b\`]`).test(cliText) === false) {
102-
assert(false, `Should have option --no${envVar.slice(1)} documented`);
102+
assert.fail(`Should have option --no${envVar.slice(1)} documented`);
103103
} else {
104104
manPagesOptions.delete(`-no${envVar.slice(1)}`);
105105
}
@@ -108,21 +108,21 @@ for (const [, envVar, config] of nodeOptionsCC.matchAll(addOptionRE)) {
108108
// NODE_OPTIONS
109109
if (isInNodeOption && !hasTrueAsDefaultValue &&
110110
new RegExp(`\`${envVar}\``).test(nodeOptionsText) === false) {
111-
assert(false, `Should have option ${envVar} in NODE_OPTIONS documented`);
111+
assert.fail(`Should have option ${envVar} in NODE_OPTIONS documented`);
112112
}
113113

114114
if (isInNodeOption && hasTrueAsDefaultValue && new RegExp(`\`--no${envVar.slice(1)}\``).test(cliText) === false) {
115-
assert(false, `Should have option --no${envVar.slice(1)} in NODE_OPTIONS documented`);
115+
assert.fail(`Should have option --no${envVar.slice(1)} in NODE_OPTIONS documented`);
116116
}
117117

118118
if (!hasTrueAsDefaultValue && new RegExp(`\`--no${envVar.slice(1)}\``).test(cliText) === true) {
119-
assert(false, `Should not have option --no${envVar.slice(1)} in NODE_OPTIONS documented`);
119+
assert.fail(`Should not have option --no${envVar.slice(1)} in NODE_OPTIONS documented`);
120120
}
121121

122122
// V8 options
123123
if (isV8Option) {
124124
if (new RegExp(`###.*\`${envVar}[[=\\s\\b\`]`).test(v8OptionsText) === false) {
125-
assert(false, `Should have option ${envVar} in V8 options documented`);
125+
assert.fail(`Should have option ${envVar} in V8 options documented`);
126126
} else {
127127
manPagesOptions.delete(envVar.slice(1));
128128
}

β€Žtest/parallel/test-cluster-bind-twice.jsβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ if (!id) {
102102
}));
103103
}, 2));
104104
} else {
105-
assert(0); // Bad command line argument
105+
assert.fail('Bad command line argument');
106106
}
107107

108108
function startWorker() {

β€Žtest/parallel/test-cluster-eaddrinuse.jsβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,5 @@ if (id === 'undefined') {
5858
}));
5959
}));
6060
} else {
61-
assert(0); // Bad argument.
61+
assert.fail('Bad argument');
6262
}

β€Žtest/parallel/test-fastutf8stream-sync.jsβ€Ž

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,6 @@ assert.throws(() => {
181181
assert.ok(stream.write('hello world πŸ‘€\n'));
182182
assert.ok(stream.write('another line πŸ‘€\n'));
183183

184-
// Check internal buffer length (may not be available in Utf8Stream)
185-
// This is implementation-specific, so we just verify writes succeeded
186-
assert.ok(true, 'writes completed successfully');
187-
188184
stream.end();
189185
}
190186

@@ -200,7 +196,6 @@ assert.throws(() => {
200196
}
201197

202198
// Check internal buffer length (implementation-specific)
203-
assert.ok(true, 'writes completed successfully');
204199
readFile(dest, 'utf8', common.mustSucceed((data) => {
205200
assert.strictEqual(data, str);
206201
}));

β€Žtest/parallel/test-fs-glob.mjsβ€Ž

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,6 @@ describe('glob - with restricted directory', function() {
529529
for await (const match of asyncGlob('*', { cwd: restrictedDir })) {
530530
results.push(match);
531531
}
532-
assert.ok(true, 'glob completed without throwing on readdir error');
533532
} finally {
534533
try {
535534
chmodSync(restrictedDir, 0o755);

0 commit comments

Comments
Β (0)