Jest
jestTesting36 lines
Unit and component testing. Adapts its configuration to the platform you selected.
Current — as published in v1.3.2. Written by hand for Jest, not generated.
The rule
Queries
- Find elements the way a user does:
getByText,getByLabelText,getByRole. getByTestIdis a last resort. Reaching for it first produces tests that pass while the screen is unusable by anyone relying on a screen reader.- Anything async uses
findBy*, which retries.getBy*does not, and a fixedsetTimeoutto compensate is a flake waiting to happen.
Mocking
- Mock native modules and third-party SDKs in
jest.setup.js. - Where this project wraps an SDK in a service, mock the service.
- Never mock your own modules to make a test pass — that couples the test to the current file layout and it will break on a no-op refactor.
- Reset mocks in
beforeEach. A test that only passes in isolation is broken.
Structure
ts
it('surfaces a retryable error when the token has expired', async () => {
// arrange — the least setup that makes the scenario true
// act — one interaction
// expect — what a caller or user observes
});One reason to fail per test, and a name that says which.
Do not
- Do not commit
it.onlyorit.skip. - Do not add snapshots nobody reads.
- Do not test library behaviour — test this project's use of it.
- Do not chase a coverage percentage; cover the paths that can be wrong.
As each tool receives it
The same guidance, in the shape each editor reads. Cursor and Claude Code are written separately by hand; the rest are derived from the Cursor rule.
.cursor/rules/jest.mdc
---
description: Jest and React Native Testing Library conventions
globs: ["**/*.test.ts", "**/*.test.tsx", "**/__tests__/**", "jest.setup.js"]
alwaysApply: false
---
# Jest
## Queries
- Find elements the way a user does: `getByText`, `getByLabelText`, `getByRole`.
- `getByTestId` is a last resort. Reaching for it first produces tests that pass
while the screen is unusable by anyone relying on a screen reader.
- Anything async uses `findBy*`, which retries. `getBy*` does not, and a fixed
`setTimeout` to compensate is a flake waiting to happen.
## Mocking
- Mock native modules and third-party SDKs in `jest.setup.js`.
- Where this project wraps an SDK in a service, mock the **service**.
- Never mock your own modules to make a test pass — that couples the test to
the current file layout and it will break on a no-op refactor.
- Reset mocks in `beforeEach`. A test that only passes in isolation is broken.
## Structure
```ts
it('surfaces a retryable error when the token has expired', async () => {
// arrange — the least setup that makes the scenario true
// act — one interaction
// expect — what a caller or user observes
});
```
One reason to fail per test, and a name that says which.
## Do not
- Do not commit `it.only` or `it.skip`.
- Do not add snapshots nobody reads.
- Do not test library behaviour — test this project's use of it.
- Do not chase a coverage percentage; cover the paths that can be wrong.
What else this module writes
Selecting Jest contributes more than a rule file — all of it merged with every other module you pick, with conflicts resolved rather than duplicated.
Dependencies
Depends on the rest of the stack — this module installs different packages depending on what else you select, so there is no single list to show.
Folders
src/test/factories/
Put this in your repo
Adds Jest to a project this tool generated, without starting over. Files you have hand-edited are left alone.