jest

A set of rules for writing Jest tests, where Jest is a JavaScript and TypeScript testing tool. It covers test structure, matchers, mocking, asynchronous tests, and test independence.

In plain words
What is it for?
Use it when creating or reviewing unit tests with `describe` and `it`, Arrange-Act-Assert setup, matchers, mocks, and asynchronous code.
Why use it?
It reduces unclear, fragile, or order-dependent tests by giving developers consistent ways to organize checks and compare results.

Cursor rule

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add rules/nedcodes-ok/cursor-doctor/jest
Clone the repo
git clone --depth 1 https://github.com/nedcodes-ok/cursor-doctor
Per session 978 This file is loaded in full into every session.
When invoked 978 The same file — it is already loaded in full.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00978 $0.00978
Opus 5 $0.00489 $0.00489
Sonnet 5 $0.00196 $0.00196
Haiku 4.5 $0.00098 $0.00098

Measured 2d ago against content hash 8ace8a931584, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

jest scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 2d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

pro-kit/templates/tools/jest.mdc · 48 lines

How it starts

The opening of the file, as written. The whole thing — 48 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Jest Cursor Rules

You are an expert in Jest testing (v29+). Follow these rules:

Structure

  • One test file per source file: user.service.tsuser.service.test.ts. Colocate tests next to source, not in a separate __tests__/ tree
  • describe() for grouping related behaviors. Nest describes for sub-behaviors: describe('UserService')describe('findById')
  • it() with descriptive names that read as sentences: it("returns null when user not found") — not it("test1") or it("should work")
  • Tests must be independent — no shared mutable state between test cases. If test B depends on test A running first, both tests are broken
  • Arrange-Act-Assert pattern in every test: set up data, call the thing, check the result. One logical assertion per test (multiple expect calls checking one behavior is fine)

Matchers

  • .toBe() for primitives and reference equality. .toEqual() for deep equality on objects/arrays — .toBe({a:1}) fails because it's a different object reference
  • .toMatchObject() for partial matching — don't over-specify with .toEqual() when you only care about 3 of 10 fields. Less brittle when the shape changes
  • .toThrow() requires wrapping in a function: expect(() => fn()).toThrow() not expect(fn()).toThrow() — this is the #1 gotcha
  • .toHaveBeenCalledWith(expect.objectContaining({...})) for partial argument matching on mocks
  • .toMatchInlineSnapshot() over .toMatchSnapshot() for small outputs — inline snapshots keep the expected value right in the test file, no separate .snap file to maintain

Mocking

  • jest.fn() for standalone mock functions. jest.spyOn(object, 'method') to wrap an existing method while keeping the original accessible via .mockRestore()
  • jest.mock("module") at the top level — Jest hoists it above imports automatically. Inside the mock factory, you can't reference variables from the outer scope (they're not initialized yet)
  • jest.mocked(fn) for TypeScript: wraps the function type so autocomplete and type-checking work on mock methods (.mockReturnValue, .mockResolvedValue)
  • jest.clearAllMocks() in beforeEach — resets call counts and return values. jest.restoreAllMocks() also undoes spyOn replacements. Pick one and be consistent
  • Mock at the boundary (HTTP client, database, file system), not internal functions — testing the wiring between your own functions gives you change-detector tests, not confidence

Read the full file on GitHub · 48 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 2d ago First seen · 48 lines · 978 tokens per session scan A 8ace8a931584

Subscribe to this mod's changes

jest is a cursor rule published in the GitHub repository nedcodes-ok/cursor-doctor (9 stars, last pushed 5mo ago), licensed MIT. It adds 978 tokens to every session, about $0.0049 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-31.