reverse-engineer-anything
MOmorluto/rea441k25,484 new stars in the last 7 days
Use when writing or fixing tests in apps/frontend. Covers Jest and React Testing Library conventions, targeted test runs, the coverage mandate, Zustand mocking, and common pitfalls.
Use this skill when writing or fixing tests in apps/frontend. Covers Jest + React Testing Library conventions, targeting rules, and common pitfalls in this codebase.
TRIGGER: any task involving test files in apps/frontend, or when asked to write/fix/run frontend tests.
Target: ≥ 95% Statements, Branches, Functions, Lines across apps/frontend. Every change must move coverage upward, never downward.
| Layer | Tool | When required |
|---|---|---|
| Unit | Jest | Every service, store, hook, utility, helper |
| Component | React Testing Library (RTL) | Every UI component — render + interaction + conditional rendering |
| Snapshot | Jest toMatchSnapshot / toMatchInlineSnapshot | Stable UI layouts — complement behavioural tests, never replace them |
| E2E | Playwright (e2e/) | Auth flows, booking, checkout, payment, any critical user journey |
All four layers must grow together. Do not add 20 RTL tests while leaving Playwright untouched for a critical flow, and vice versa.
# After every change, run coverage for the touched file(s):
pnpm --filter frontend run test -- --testPathPatterns="<YourFile>" --coverage --collectCoverageFrom="src/app/path/to/YourFile.tsx"
# Check the output — if Statements/Branches/Functions dropped vs what you started with, add tests before declaring done.
If adding a test for a previously-uncovered branch, note it in the COMMIT CHECKPOINT message (e.g. test(frontend): improve branch coverage for CompanionHistoryPage loading state).
Every new module, service, hook, store, utility, or component added to apps/frontend must ship with tests in the same batch. No exceptions.
| What you add | What you must also add |
|---|---|
| Service function / API call | Jest unit: success + all error branches (axios + non-axios) |
| Zustand store | Jest: every action, selector, guard, and edge case |
| Custom hook | renderHook covering all return values and state branches |
| Utility / lib function | Jest unit with full branch coverage |
| UI component | RTL render + at least one user interaction test |
| E2E-critical flow (auth, booking, checkout, payment) | Playwright test in e2e/ |
Coverage bar for any new file you author: Statements ≥ 90%, Branches ≥ 90%, Functions ≥ 90%.
Do not leave an existing file in a worse coverage state than you found it. If you touch a file, hold or improve its coverage.
Run all three every time you touch apps/frontend. Never skip any step.
# 1. Type check — run from apps/frontend/
npx tsc --noemit
# 2. Lint — run from repo root
pnpm --filter frontend run lint
# 3. Prefer targeted tests for the files you modified; full Jest runs are allowed if the user explicitly asks or if you are validating shared test infrastructure
pnpm --filter frontend run test -- --testPathPatterns="<ModifiedComponentName>"
# Examples
pnpm --filter frontend run test -- --testPathPatterns="CompanionCard"
pnpm --filter frontend run test -- --testPathPatterns="Availability"
pnpm --filter frontend run test -- --testPathPatterns="__tests__/features/billing"
Full suite is discouraged by default. Use targeted runs for normal development, but a full Jest run is allowed if the user explicitly asks, if you are triaging repo-wide breakage, or if you changed shared test infrastructure. Playwright and accessibility runs are allowed whenever they are relevant.
When modifying an existing file, always check whether a test file already exists for it (look in src/app/__tests__/ mirroring the source path). If it does, run it and fix any failures your change introduced before declaring the task done. A change is not complete if it breaks existing tests.
--testPathPatterns (plural) - the old singular form errors.src/app/__tests__/apps/frontend/jest.config.tssrc/app/jest.mocks/jest.spyOn(console, 'error') checks are active — DOM nesting warnings are treated as test failures.
<button> inside <button>.<span> not <button>:// jest.mocks or inline
jest.mock('react-icons/fa', () => ({
FaUser: () => <span data-testid="icon-user" />,
FaPlus: () => <span data-testid="icon-plus" />,
}));
// Always wrap async state updates
await act(async () => {
userEvent.click(button);
});
Reset store state between tests to avoid leakage:
beforeEach(() => {
useAuthStore.setState({ user: null, isAuthenticated: false });
});
Mocking stores that use .getState() outside React
When a hook calls useXxxStore.getState() directly (e.g. to read status without subscribing), jest.mock alone produces a plain function with no getState method and the test will throw TypeError: useXxxStore.getState is not a function.
Two patterns depending on how the store is mocked:
jest.mock('@/app/stores/xxxStore') (auto-mock) — attach getState in beforeEach:const mockGetState = jest.fn();
beforeEach(() => {
(useXxxStore as unknown as jest.Mock).mockImplementation((selector) => selector(mockState));
mockGetState.mockReturnValue(mockState);
(useXxxStore as unknown as jest.Mock & { getState: jest.Mock }).getState = mockGetState;
});
jest.mock('...', () => ({ useXxxStore: jest.fn() })) (explicit factory) — include getState in the factory and wire it in beforeEach:jest.mock('@/app/stores/xxxStore', () => ({
useXxxStore: Object.assign(jest.fn(), { getState: jest.fn() }),
}));
const mockGetState = jest.fn();
beforeEach(() => {
(useXxxStore as unknown as jest.Mock).mockImplementation((selector) => selector(mockState));
mockGetState.mockReturnValue(mockState);
(useXxxStore as unknown as jest.Mock & { getState: jest.Mock }).getState = mockGetState;
});
Always update mockGetState.mockReturnValue(mockState) whenever mockState changes mid-test so that getState() stays in sync with the selector mock.
Use jest.spyOn on axios or mock at the module level. Never make real HTTP calls in tests.
import axios from 'axios';
jest.spyOn(axios, 'get').mockResolvedValue({ data: mockData });
In order of preference:
getByRole — semantic, accessiblegetByLabelText — formsgetByText — when role isn't meaningfulgetByTestId — last resort, only when no other selector workssrc/app/__tests__/
features/ ← feature-level tests
components/ ← component unit tests
hooks/ ← custom hook tests
utils/ ← utility function tests
Test file naming: ComponentName.test.tsx mirrors the source file name.
screen.getByDisplayValue for controlled inputs — use getByRole('textbox') + check value.userEvent needs await in v14+ — always await userEvent.click(el).@/app/ui, make sure the mock is at module level, not inside describe.e2e/ and run separately — don't confuse them with Jest tests.--testPathPatterns matches multiple files unintentionally, be more specific with the path.require() is forbidden — use top-level imports insteadESLint rule @typescript-eslint/no-require-imports blocks require() inside test bodies. Never do:
// ❌ forbidden
const { fromFormRequestDTO } = require('@yosemite-crew/types');
Instead, import at the top of the file and cast:
// ✅ correct
import { fromFormRequestDTO } from '@yosemite-crew/types';
// ...
(fromFormRequestDTO as jest.Mock).mockImplementationOnce(() => {
throw new Error('invalid');
});
jest.resetAllMocks() wipes factory mock return valuesIf you use jest.resetAllMocks() in beforeEach, any mock initialized with .mockReturnValue() in a jest.mock() factory is reset to undefined. Re-initialize all mock return values inside beforeEach after resetAllMocks():
beforeEach(() => {
jest.resetAllMocks();
// Must re-set these — factory defaults are gone after resetAllMocks
(canTransitionAppointmentStatus as jest.Mock).mockReturnValue(true);
(useAuthStore.getState as jest.Mock).mockReturnValue({ user: mockUser, attributes: {} });
});
axios.isAxiosError mock — use jest.mock("axios", ...) not jest.spyOnjest.spyOn on axios.isAxiosError doesn't reliably work because the service imports axios at module load time. Use:
jest.mock('axios', () => ({
create: jest.fn(() => ({
interceptors: { request: { use: jest.fn() }, response: { use: jest.fn() } },
})),
isAxiosError: jest.fn(),
}));
// Then per test:
(axios.isAxiosError as jest.Mock).mockReturnValue(true);
// After test:
(axios.isAxiosError as jest.Mock).mockReset();
Object.definePropertyObject.assign(el, { scrollTop: 0 }) throws because scrollTop is a getter-only property on HTMLElement. Use:
Object.defineProperty(el, 'scrollTop', { value: 0, writable: true, configurable: true });
jest.resetModules()Services that maintain module-level singletons (e.g. connectionPromise, chatClient) can't easily test "connection in progress" state when jest.resetModules() resets the module between each test. Drop those test scenarios or use a single beforeAll import for that specific describe block.
performAppointmentAction requires a valid lead.id for accept actionWhen testing acceptAppointment or changeAppointmentStatus → UPCOMING, the appointment must have a non-empty lead.id or the service throws "Cannot accept appointment without a valid lead." Always include lead: { id: 'vet-1', name: 'Dr Vet' } in those test fixtures.
canTransitionAppointmentStatus from @/app/lib/appointmentsThis function is imported by appointmentService.ts from @/app/lib/appointments (not from a utils sub-path). Mock it as:
jest.mock('@/app/lib/appointments', () => ({
canTransitionAppointmentStatus: jest.fn(),
getInvalidAppointmentStatusTransitionMessage: jest.fn().mockReturnValue('Invalid transition'),
}));
And re-initialize in beforeEach after resetAllMocks():
const { canTransitionAppointmentStatus } = jest.requireMock('@/app/lib/appointments');
(canTransitionAppointmentStatus as jest.Mock).mockReturnValue(true);
resetAllMocks()useAuthStore and useTeamStore are imported by appointmentService. If your test file uses jest.resetAllMocks(), you must re-seed these in beforeEach:
const { useAuthStore } = jest.requireMock('@/app/stores/authStore');
(useAuthStore.getState as jest.Mock).mockReturnValue({
user: { getUsername: jest.fn().mockReturnValue('user-1') },
attributes: {},
});
getValidSession branch logic in authStoreisSessionFresh checks session.isValid() first, then reads payload.exp. Testing branches:
isValid: () => true and exp far in the future → returns cached sessionnull session → triggers refreshdecodePayload throws → falls back to session.isValid() return valueforceRefresh: true + null refreshed session → returns null without calling checkSessionMOmorluto/rea441k25,484 new stars in the last 7 days
MAmattpocock/skills318k9,301 new stars in the last 7 days
OBobra/superpowers314k4,048 new stars in the last 7 days
AFaffaan-m/ECC299k5,106 new stars in the last 7 days
AFaffaan-m/ECC299k5,106 new stars in the last 7 days
Audit accessibility on Web, iOS, and Android to meet WCAG 2.2, with keyboard navigation and screen-reader support.
AFaffaan-m/ECC299k5,106 new stars in the last 7 days