Skip to content

Commit 79df8b2

Browse files
committed
lib,test: enforce use of assert.fail via a lint rule
PR-URL: #61004 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: René <[email protected]>
1 parent 029ebac commit 79df8b2

16 files changed

+33
-28
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-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);

test/parallel/test-http.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ const server = http.Server(common.mustCall((req, res) => {
4646
assert.strictEqual(req.headers.cookie, 'abc=123; def=456; ghi=789');
4747
break;
4848
default:
49-
assert(false, `Unexpected request for ${req.url}`);
49+
assert.fail(`Unexpected request for ${req.url}`);
5050
}
5151

5252
if (expectedRequests.length === 0)

0 commit comments

Comments
 (0)