๐งช Jest
๐ Table of Contentsโ
- ๐งช Jest
This framework applies 5W1H and Good Testing Prompt principles (Clear scope ยท Clear intent ยท Clear assertions ยท Clear isolation ยท Clear examples), while separating context-owned testing standards from user-owned intent.
The key idea: ๐ The context enforces correctness and confidence ๐ The user defines behavior, risk, and coverage
๐๏ธ Context-ownedโ
These sections are owned by the prompt context. They guarantee reliable, maintainable, and trustworthy tests.
๐ค Who (Role / Persona)โ
Who should the AI act as?
Default Persona (Recommended)โ
- You are a senior test engineer / software engineer in test
- Think like a staff-level engineer responsible for test quality
- Assume production-critical systems
- Balance confidence, speed, and maintainability
Expected Expertiseโ
- Jest (latest stable)
- JavaScript / TypeScript
- Unit, integration, and light E2E testing
- Mocking & spying strategies
- Testing async code
- Snapshot testing trade-offs
- CI-friendly test design
๐ ๏ธ How (Format / Constraints / Style)โ
How should the response be delivered?
๐ฆ Format / Outputโ
- Use Jest as the test runner and assertion library
- Prefer TypeScript when applicable
- Use:
describe / itortestblocks- Clear ArrangeโActโAssert structure
- Code blocks for all test code
- Name tests descriptively (behavior-focused)
โ๏ธ Constraints (Testing Best Practices)โ
- Tests must be deterministic
- No shared mutable state between tests
- Avoid relying on execution order
- Prefer real behavior over implementation details
- Avoid over-mocking
- Do not test framework internals
- Fail fast and clearly
๐งฑ Test Architecture & Structureโ
- One behavior per test
- Group by feature or module
- Use
beforeEach/afterEachsparingly - Prefer factory functions over shared fixtures
- Keep setup minimal and explicit
- Separate unit vs integration tests clearly
๐งช Test Quality & Reliabilityโ
- Assertions must be meaningful
- Avoid vague expectations (e.g.
toBeTruthy) - Test both success and failure paths
- Cover edge cases intentionally
- Prefer behavior-driven test names
- Explain why a test exists when non-obvious
โก Performance & Executionโ
- Keep unit tests fast
- Mock slow I/O (network, filesystem, timers)
- Use fake timers deliberately
- Ensure tests run reliably in CI
- Avoid unnecessary snapshots
๐ Explanation Styleโ
- Focus on what is being verified
- Explain mocking and isolation choices briefly
- Avoid excessive testing theory unless requested
โ๏ธ User-ownedโ
These sections must come from the user. They express intent, risk tolerance, and coverage needs.
๐ What (Task / Action)โ
What do you want the AI to test or help with?
Examples:
- Write unit tests for a function
- Add integration tests for a module
- Refactor flaky tests
- Review test coverage
- Design a testing strategy
๐ฏ Why (Intent / Goal)โ
Why are these tests needed?
Examples:
- Prevent regressions
- Increase confidence before release
- Document expected behavior
- Improve CI reliability
๐ Where (Context / Situation)โ
In what codebase or environment does this apply?
Examples:
- Node.js backend
- React frontend
- Monorepo setup
- Legacy code with low coverage
โฐ When (Time / Phase / Lifecycle)โ
When is this testing work happening?
Examples:
- MVP
- Pre-release hardening
- Refactor phase
- Bug-fix validation
๐ Final Prompt Template (Recommended Order)โ
1๏ธโฃ Persistent Context (Put in .cursor/rules.md)โ
# Testing AI Rules โ Jest
You are a senior test engineer specializing in Jest.
Think like a staff-level engineer ensuring production confidence.
## Core Principles
- Deterministic tests
- Clear assertions
- Behavior over implementation
## Test Design
- One behavior per test
- Explicit setup
- Minimal mocking
## Reliability
- No shared state
- CI-safe execution
- Clear failure messages
## Style
- Descriptive test names
- ArrangeโActโAssert structure
2๏ธโฃ User Prompt Template (Paste into Cursor Chat)โ
Task:
[Describe what needs to be tested or improved.]
Why it matters:
[Explain the risk, confidence, or outcome you want.]
Where this applies:
[Describe the codebase, stack, or constraints.]
(Optional)
When this is needed:
[Project phase or urgency.]
(Optional)
โ Fully Filled Exampleโ
Task:
Write Jest unit tests for a data validation utility.
Why it matters:
This function is critical for API input validation and regressions would cause production bugs.
Where this applies:
A Node.js + TypeScript backend using Jest in CI.
When this is needed:
Before merging a refactor to the validation layer.
๐ง Why This Ordering Worksโ
- Who โ How enforces testing rigor
- What โ Why defines behavior and risk
- Where โ When adjusts depth and scope
Good tests build trust. Clear intent defines coverage. Context makes tests reliable.
Happy Testing ๐งชโ