test-structure
The shape of a single test.
Agent checkAdvises by default
The rule
Agents read this text as written when they write and review code.
Write every test as arrange, act, assert. Arrange in one named setup factory; the test body calls it, acts and asserts.
import { expect, describe, test, jest } from '@jest/globals';
import { UserProfile } from '@/profiles/UserProfile';
import { AppSettings } from '@/settings/AppSettings';
import { getAvatarUrl } from '@/avatars/getAvatarUrl';
const mockGetAvatarFromProfile = jest.fn<(params: { profile: UserProfile }) => string | null>();
jest.mock('@/profiles/getAvatarFromProfile', () => ({
getAvatarFromProfile: (params: { profile: UserProfile }) => mockGetAvatarFromProfile(params),
}));
const setupAvatar = ({ profile = null }: { profile?: string | null } = {}) => {
mockGetAvatarFromProfile.mockReturnValue(profile);
const userProfile = new UserProfile({ profileData: { email: 'user@example.com' } });
const appSettings = new AppSettings({ defaultPreferences: {} });
return { userProfile, appSettings };
};
describe('getAvatarUrl', () => {
test('returns the profile avatar when one exists', () => {
const { userProfile, appSettings } = setupAvatar({ profile: 'p.png' });
const avatarUrl = getAvatarUrl({ userProfile, appSettings });
expect(avatarUrl).toBe('p.png');
});
});
- The setup factory takes the test's variants as parameters with defaults, wires the mocks, builds the fixtures, and returns the locals the test needs as
consts. One factory configures any number of mocks, and one call is the whole arrangement. - A test may override the one value it varies with a single line after the call, such as one
mockReturnValue. - For a class, the factory returns the constructor's collaborators, and the test's act constructs the instance.
- Act once per test: two acts make two tests. Use several
expects only when they assert one behaviour's result, and prefer oneexpectfor several properties of one result. - Assign each call in the act to a named
const; never nest one call in another. The act sits inside the assertion only for an error,expect(() => parse(bad)).toThrow()orawait expect(load()).rejects.toThrow(). A matcher may compose another, as intoEqual(expect.objectContaining(...)). - Separate arrange, act and assert with a blank line, and write no
// arrange,// actor// assertcomments.
Examples
The agent flags code like the incorrect example and accepts code like the correct one.
Incorrect
import { expect, describe, test } from '@jest/globals';import { getTotal } from './getTotal';describe('getTotal', () => {test('multiplies the quantity by the unit price', () => {// arrangeconst quantity = 2;const unitPrice = 50;// act and assert in one breath, with the call nested in the matcherexpect(getTotal({ quantity, unitPrice })).toBe(100);});});
Correct
import { expect, describe, test } from '@jest/globals';import { getTotal } from './getTotal';const setupOrder = ({ quantity = 2, unitPrice = 50 }: { quantity?: number; unitPrice?: number } = {}) => {return { quantity, unitPrice };};describe('getTotal', () => {test('multiplies the quantity by the unit price', () => {const { quantity, unitPrice } = setupOrder();const total = getTotal({ quantity, unitPrice });expect(total).toBe(100);});});
Configure
- Block
"blocking"Stops a run when a file the run changed breaks the rule. - AdviseDefault
"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.
{"standards-rule-settings": {"test-structure": "advisory"}}