Skip to content

swallowed-assertion ​

An assertion inside a try whose catch neither rethrows, fails the test, nor looks at the error. Reported in test files. On by default, as an error.

Why: a failed assertion throws. Here the catch takes it, and the test passes with the assertion failed. Guide: Testing.

ts
import assert from "node:assert/strict";
import { test } from "node:test";

function total(prices: readonly number[]): number {
  return prices.reduce((sum, price) => sum + price, 0);
}

test("total adds the prices", () => {
  try {
    assert.equal(total([3, 4]), 7);
  } catch {
    console.info("total: not checked");
  }
});

✅ Accepted: no try around the assertion; a throw is checked with the form made for it

✓ Accepted0 diagnostics
ts
import assert from "node:assert/strict";
import { test } from "node:test";

function total(prices: readonly number[]): number {
  return prices.reduce((sum, price) => sum + price, 0);
}

test("totals and broken settings", () => {
  assert.equal(total([3, 4]), 7);
  assert.throws(() => JSON.parse("{ port: 80"), SyntaxError);
});

A catch that rethrows (also only some errors: if (!(e instanceof Timeout)) throw e), that fails the test, or that keeps the error to assert on later (errors.push(e), then assert.deepEqual(errors, [])) is left alone. Logging the error is not looking at it.

What the rule can't see ​

  • A helper that asserts, called in the try: only assertions written there are found.
  • A finally that returns, which drops the failure too.

MIT OR Apache-2.0 · Docs by Gemini 4 Neon Extra