lightsoutAlpha

test-mock-prefix

How mocks are named.

Deterministic checkBlocks by default

The rule

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

Start the name of every mock declared at module scope with mock, such as mockGetProfile. Jest hoists jest.mock() calls to the top of the file, and only mock-prefixed variables are reachable inside the factory.

Examples

The check flags the incorrect code and passes the correct code.

Incorrect

subject.unit.test.ts
import { expect, describe, test, jest } from '@jest/globals';
const getProfile = jest.fn<() => string>();
describe('subject', () => {
test('reads the profile', () => {
getProfile.mockReturnValue('p.png');
expect(getProfile()).toBe('p.png');
});
});

Correct

subject.unit.test.ts
import { expect, describe, test, jest } from '@jest/globals';
const mockGetProfile = jest.fn<() => string>();
const setupProfile = () => {
const getGravatar = jest.fn<() => string>();
mockGetProfile.mockReturnValue('p.png');
getGravatar.mockReturnValue('g.png');
return { getGravatar };
};
describe('subject', () => {
test('reads the profile', () => {
const { getGravatar } = setupProfile();
expect(mockGetProfile() + getGravatar()).toBe('p.pngg.png');
});
});

Configure

  • Block"blocking"Stops a run when a file the run changed breaks the rule.
    Default
  • Advise"advisory"Reports it and hands it to the refactor agent. Never stops a run.
  • 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": {
"test-mock-prefix": "blocking"
}
}

Packs that select it