Skip to content

ddd-ts-kit/test-no-mocks

Architecture id: test/no-mocks. Default severity: warn in recommended, error in strict.

Applies to: test files of a context (test/**, *.spec.ts, *.test.ts).

A mock asserts calls, not outcomes, and breaks on every refactoring. A fake is a working in-memory implementation of the port, held to the same contract suite as the real adapter.

  • vi.mock, vi.doMock, vi.fn, vi.spyOn, vi.mocked and their jest twins
  • an import of sinon, ts-mockito, jest-mock, jest-mock-extended, vitest-mock-extended, testdouble, ts-auto-mock, typemoq, moq.ts, @golevelup/ts-jest, @golevelup/ts-vitest

Incorrect:

const save = vi.fn();
vi.mock('@/contexts/billing/infrastructure/persistence/drizzle-invoice.repository');

Correct:

const invoices = new FakeInvoiceRepository();
await handler.execute(command);
expect(await invoices.findById(id)).toBeDefined();

vi.useFakeTimers and vi.setSystemTime are not mocks of a collaborator and are allowed, although FixedClock is the usual way.

None.

rules."test/no-mocks" in ddd-ts-kit.config.json set to off silences the rule, and // ddd-ts-kit-disable-next-line test/no-mocks -- <reason> silences one line, exactly as for ddd-ts-kit check.

  • Tests outside a context (test/e2e) are not judged.

ddd-ts-kit check reports the same id with regular expressions. This rule is the authority.