no-conditional-assertion
July 2, 2026 ยท View on GitHub
๐ Disallow assertions inside conditional code within a test.
๐ผ This rule is enabled in the following configs: โ
recommended, โ๏ธ unopinionated.
Assertions placed inside conditional blocks (if/else, switch, ternary, logical &&/||/??, loops) may never execute, which undermines the guarantee that a test actually verifies anything. Move assertions outside the conditional, or restructure the test so each path is covered by its own unconditional assertion.
Examples
import test from 'node:test';
import assert from 'node:assert';
// โ โ assertion may not run if x is falsy
test('foo', () => {
if (x) {
assert.ok(value);
}
});
// โ โ right-hand side of && is conditional
test('foo', () => {
ready && assert.ok(value);
});
// โ โ assertion inside a loop may run 0 or many times
test('foo', () => {
for (const item of items) {
assert.ok(item);
}
});
// โ
โ unconditional assertion
test('foo', () => {
assert.strictEqual(result, expected);
});
// โ
โ assertion in the condition test itself always runs
test('foo', () => {
if (assert.ok(value)) {
setup();
}
});
This rule is purely syntactic: it flags an assertion wrapped in a conditional anywhere between the assertion and the enclosing test or hook, including inside nested helper functions. So an assertion inside an if within a helper is reported even if that helper is always called.
This also applies to hooks, where a conditional assertion may silently never run:
import {beforeEach} from 'node:test';
import assert from 'node:assert';
// โ
beforeEach(() => {
if (x) {
assert.ok(value);
}
});