lightsoutAlpha

what-to-mock

What to mock, and with which tool.

Agent checkAdvises by default

The rule

Agents read this text as written when they write and review code.

Construct the subject under test directly, and stub only the boundaries you do not own: the network, the filesystem, other modules' services. Never mock what you own and could simply build.

  • Use jest.spyOn for one method on an object you already hold, such as an injected service or repository; the rest stays real.
  • Use jest.mock for a standalone function exported from another module.
  • Never mock a module that only exports plain constants; import the real one, since mocking it blocks coverage and adds no isolation. Mock one only when it has import-time side effects, or when the test needs a different value, and then prefer jest.replaceProperty or injection.

Examples

The agent flags code like the incorrect example and accepts code like the correct one.

Incorrect

src/orders/placeOrder.unit.test.ts
import { expect, describe, test, jest } from '@jest/globals';
import { placeOrder } from './placeOrder';
// The repository is an object the test already holds — replacing its whole
// module hides every other method the unit may touch.
const mockSave = jest.fn<(order: { id: string }) => void>();
jest.mock('./orderRepository', () => ({
orderRepository: { save: (order: { id: string }) => mockSave(order) },
}));
describe('placeOrder', () => {
test('saves the order it was given', () => {
placeOrder({ order: { id: 'a' } });
expect(mockSave).toHaveBeenCalledWith({ id: 'a' });
});
});

Correct

src/orders/placeOrder.unit.test.ts
import { expect, describe, test, jest } from '@jest/globals';
import { placeOrder } from './placeOrder';
const setupOrder = () => {
const repository = { save: () => undefined };
const saveSpy = jest.spyOn(repository, 'save');
return { repository, saveSpy };
};
describe('placeOrder', () => {
test('saves the order it was given', () => {
const { repository, saveSpy } = setupOrder();
placeOrder({ repository, order: { id: 'a' } });
expect(saveSpy).toHaveBeenCalledWith({ id: 'a' });
});
});

Configure

  • Block"blocking"Stops a run when a file the run changed breaks the rule.
  • Advise"advisory"Reports it and hands it to the refactor agent. Never stops a run.
    Default
  • Off"off"Not checked. Use it when your own linter already enforces the rule.

Add this to your lightsout.config.json, then change the value.

lightsout.config.json
{
"standards-rule-settings": {
"what-to-mock": "advisory"
}
}

Packs that select it