Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions doc/api/assert.md
Original file line number Diff line number Diff line change
Expand Up @@ -1682,6 +1682,8 @@ If no arguments are passed in at all `message` will be set to the string:
Be aware that in the `repl` the error message will be different to the one
thrown in a file! See below for further details.

<!-- eslint-skip -->

```mjs
import assert from 'node:assert/strict';

Expand Down Expand Up @@ -1717,6 +1719,8 @@ assert.ok(0);
// assert.ok(0)
```

<!-- eslint-skip -->

```cjs
const assert = require('node:assert/strict');

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

// Using `assert()` works the same:
assert(0);
assert(2 + 2 > 5);
// AssertionError: The expression evaluated to a falsy value:
//
// assert(0)
// assert(2 + 2 > 5)
```

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

// Using `assert()` works the same:
assert(0);
assert(2 + 2 > 5);
// AssertionError: The expression evaluated to a falsy value:
//
// assert(0)
// assert(2 + 2 > 5)
```

## `assert.rejects(asyncFn[, error][, message])`
Expand Down
14 changes: 7 additions & 7 deletions doc/api/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -1786,7 +1786,7 @@ This function creates a hook that runs before executing a suite.
describe('tests', async () => {
before(() => console.log('about to run some test'));
it('is a subtest', () => {
assert.ok('some relevant assertion here');
// Some relevant assertions here
});
});
```
Expand Down Expand Up @@ -1816,7 +1816,7 @@ This function creates a hook that runs after executing a suite.
describe('tests', async () => {
after(() => console.log('finished running tests'));
it('is a subtest', () => {
assert.ok('some relevant assertion here');
// Some relevant assertion here
});
});
```
Expand Down Expand Up @@ -1849,7 +1849,7 @@ This function creates a hook that runs before each test in the current suite.
describe('tests', async () => {
beforeEach(() => console.log('about to run a test'));
it('is a subtest', () => {
assert.ok('some relevant assertion here');
// Some relevant assertion here
});
});
```
Expand Down Expand Up @@ -1880,7 +1880,7 @@ The `afterEach()` hook is run even if the test fails.
describe('tests', async () => {
afterEach(() => console.log('finished running a test'));
it('is a subtest', () => {
assert.ok('some relevant assertion here');
// Some relevant assertion here
});
});
```
Expand Down Expand Up @@ -3472,7 +3472,7 @@ test('top level test', async (t) => {
await t.test(
'This is a subtest',
(t) => {
assert.ok('some relevant assertion here');
// Some relevant assertion here
},
);
});
Expand Down Expand Up @@ -3503,7 +3503,7 @@ finishes.
```js
test('top level test', async (t) => {
t.after((t) => t.diagnostic(`finished running ${t.name}`));
assert.ok('some relevant assertion here');
// Some relevant assertion here
});
```

Expand Down Expand Up @@ -3535,7 +3535,7 @@ test('top level test', async (t) => {
await t.test(
'This is a subtest',
(t) => {
assert.ok('some relevant assertion here');
// Some relevant assertion here
},
);
});
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/crypto/cipher.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ function getDecoder(decoder, encoding) {
if (normalizedEncoding === undefined) {
throw new ERR_UNKNOWN_ENCODING(encoding);
}
assert(false, 'Cannot change encoding');
assert.fail('Cannot change encoding');
}
return decoder;
}
Expand Down
2 changes: 1 addition & 1 deletion test/abort/test-abort-fatal-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ exec(...cmdline, common.mustCall((err, stdout, stderr) => {
if (!err) {
console.log(stdout);
console.log(stderr);
assert(false, 'this test should fail');
assert.fail('this test should fail');
}

assert(common.nodeProcessAborted(err.code, err.signal));
Expand Down
2 changes: 1 addition & 1 deletion test/message/internal_assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
require('../common');

const assert = require('internal/assert');
assert(false);
assert(2 + 2 > 5);
2 changes: 1 addition & 1 deletion test/parallel/test-cluster-bind-twice.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ if (!id) {
}));
}, 2));
} else {
assert(0); // Bad command line argument
assert.fail('Bad command line argument');
}

function startWorker() {
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-cluster-eaddrinuse.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,5 @@ if (id === 'undefined') {
}));
}));
} else {
assert(0); // Bad argument.
assert.fail('Bad argument');
}
5 changes: 0 additions & 5 deletions test/parallel/test-fastutf8stream-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,6 @@ assert.throws(() => {
assert.ok(stream.write('hello world 👀\n'));
assert.ok(stream.write('another line 👀\n'));

// Check internal buffer length (may not be available in Utf8Stream)
// This is implementation-specific, so we just verify writes succeeded
assert.ok(true, 'writes completed successfully');

stream.end();
}

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

// Check internal buffer length (implementation-specific)
assert.ok(true, 'writes completed successfully');
readFile(dest, 'utf8', common.mustSucceed((data) => {
assert.strictEqual(data, str);
}));
Expand Down
1 change: 0 additions & 1 deletion test/parallel/test-fs-glob.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,6 @@ describe('glob - with restricted directory', function() {
for await (const match of asyncGlob('*', { cwd: restrictedDir })) {
results.push(match);
}
assert.ok(true, 'glob completed without throwing on readdir error');
} finally {
try {
chmodSync(restrictedDir, 0o755);
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-http.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const server = http.Server(common.mustCall((req, res) => {
assert.strictEqual(req.headers.cookie, 'abc=123; def=456; ghi=789');
break;
default:
assert(false, `Unexpected request for ${req.url}`);
assert.fail(`Unexpected request for ${req.url}`);
}

if (expectedRequests.length === 0)
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-inspector-network-http2.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ const handleStream = common.mustCallAtLeast((stream, headers) => {
}));
break;
default:
assert(false, `Unexpected path: ${path}`);
assert.fail(`Unexpected path: ${path}`);
}
});

Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-stream-duplex-readable-writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const assert = require('assert');
duplex.on('end', common.mustNotCall());
async function run() {
for await (const chunk of duplex) {
assert(false, chunk);
assert.fail(chunk);
}
}
run().then(common.mustCall());
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-vm-module-basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const util = require('util');
const m = new SourceTextModule('while (true) {}');
await m.link(common.mustNotCall());
await m.evaluate({ timeout: 500 })
.then(() => assert(false), () => {});
.then(() => assert.fail(), () => {});
})().then(common.mustCall());

// Check the generated identifier for each module
Expand Down
2 changes: 1 addition & 1 deletion test/pummel/test-fs-watch-file-slow.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ fs.watchFile(FILENAME, { interval: TIMEOUT - 250 }, common.mustCall((curr, prev)
fs.unwatchFile(FILENAME);
break;
default:
assert(0);
assert.fail();
}
}, 4));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const server = https.createServer({
res.end('bundled ca works');
break;
default:
assert(false, `Unexpected path: ${path}`);
assert.fail(`Unexpected path: ${path}`);
}
}, 1));

Expand Down
7 changes: 7 additions & 0 deletions tools/eslint/eslint.config_utils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ export const noRestrictedSyntaxCommonAll = [
selector: "CallExpression[callee.property.name='substr']",
message: 'Use String.prototype.slice() or String.prototype.substring() instead of String.prototype.substr()',
},
{
selector: `CallExpression:matches(${[
'[callee.name="assert"]',
'[callee.object.name="assert"][callee.property.name="ok"]',
]})[arguments.0.type="Literal"]`,
message: 'Do not use a literal for the first argument of assert(), use assert.fail() instead or remove the call',
},
];

export const noRestrictedSyntaxCommonLib = [
Expand Down
Loading