Skip to content
Sarj Code Standards

no-tautological-expect

Disallow supported assertions fixed by literal setup or immutable self identity.

Why

An assertion fixed by literal setup or one immutable binding's identity cannot distinguish a wrong result from the intended behavior.

Fix

Assert on a value produced by the behavior under test against an independently specified expectation, or remove only the redundant assertion while preserving coverage.

Examples

Before — flagged An uncaptured literal assertion remains redundant
src/assertion.test.ts
import { expect } from "vitest";
expect(true).toBeTruthy();
After — preferred A throwing matcher observes the inner assertion contract
src/assertion.test.ts
import { expect } from "vitest";
expect(() => expect(true).toBeTruthy()).not.toThrow();
Before — flagged A returned value's identity with itself cannot reject a wrong result
src/service.test.ts
import { expect } from "vitest";
const result = serviceStatus();
expect(result).toBe(result);
After — preferred An independent expected state can reject a wrong result
src/service.test.ts
import { expect } from "vitest";
const result = serviceStatus();
expect(result.status).toBe("ready");
Before — flagged Do not compare identical literals
src/add.test.ts
it("works", () => {
expect(true).toBe(true);
});
After — preferred Assert on a produced value
src/add.test.ts
it("adds", () => {
expect(add(1, 1)).toBe(2);
});
Before — flagged A diagnostic string does not make a literal comparison observe behavior
src/service.test.ts
import { expect } from "vitest";
expect(true, "service ready").toBe(true);
After — preferred A diagnostic message can describe an observed result
src/service.test.ts
import { expect } from "vitest";
expect(serviceReady(), "service ready").toBe(true);
Before — flagged Negating a fixed matcher still does not observe the application
src/service.test.ts
import { expect as verify } from "vitest";
it("works", () => {
const ready = false;
verify(ready).not.toBeTruthy();
});
After — preferred A negated assertion can observe a runtime result
src/service.test.ts
import { expect as verify } from "vitest";
it("works", () => {
verify(serviceStatus()).not.toBeNull();
});
Before — flagged A const setup assertion never observes the application
src/service.test.ts
it("works", () => {
const status = "ok";
expect(status).toBe("ok");
});
After — preferred A runtime scalar result remains a valid observation
src/service.test.ts
it("works", () => {
const status = serviceStatus();
expect(status).toBe("ok");
});
Before — flagged Adding a different matcher preserves the built-in proof
src/assertion.test.ts
import { expect } from "vitest";
expect.extend({ toBeReady: custom });
expect(true).toBe(true);
After — preferred A replaced matcher can reject a literal comparison
src/assertion.test.ts
import { expect } from "vitest";
expect.extend({ toBe: () => ({ pass: false, message: () => "custom contract" }) });
expect(true).toBe(true);