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);
	}
});