How Two Prompts Enable Cursor to Batch‑Generate Unit Tests
The article details a step‑by‑step workflow that uses two carefully crafted prompts with Cursor to automatically locate source files in a large monorepo, record tasks, iteratively generate Vitest unit tests, track progress, and handle failures, turning a 11 k‑line codebase into a semi‑automated test suite.
Using Cursor, the author generated about 11 k lines of unit‑test code for a monorepo containing over 300 packages and 2 million lines of source code. By providing a single prompt, Cursor can locate the package source directory, enumerate files to test, and produce test files without the author understanding the underlying logic.
Prompt parsing : Cursor supports multi‑file editing, repository indexing, and .cursorrules configuration. Two custom prompts are added: .cursor/rules/spec-for-ut.mdc to define test specifications and .cursor/rules/instruct-ut.mdc to drive iterative test generation until coverage and passing criteria are met.
Workflow steps :
Find the source directory based on the package name.
Scan the directory, list files that need tests, and record them in .cursor/task.md.
For each task, apply the instructions in .cursor/rules/instruct-ut.mdc to generate test code, run the tests, and update the task list.
If a test consistently fails with only a few cases, drop those cases to keep the overall suite green.
Log error cases in .cursor/lessons to avoid repeating mistakes.
Repeat until all files are covered, then exit.
The author provides the full content of the .cursor/rules/spec-for-ut.mdc file, which specifies the testing stack (Vitest, React Testing Library), naming conventions, file placement, and the arrange‑act‑assert pattern.
Challenges : Cursor sometimes stops after processing part of a large package, and complex monorepo structures (deep nesting, TS aliases) increase test difficulty. For complex components, generated tests may fail due to ESM/CommonJS mismatches or missing mocks.
Mitigation techniques :
Use Vitest/Jest's [only] filter to run only failing cases.
Click “Add to Composer” in the terminal UI to let the LLM continue fixing issues.
Mock all downstream module calls and environment variables, as LLMs struggle with proper mocking.
Final checklist for reliable operation:
Install Cursor and upgrade to a paid tier.
Enable Codebase Indexing.
Activate Cursor Agent Yolo mode to automatically run linters, type checks, and Vitest validation.
Add the two rule files to .cursor/rules.
Even with these measures, complex code still requires human intervention, and keeping source code simple remains the best way to reduce manual effort.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Full-Stack Cultivation Path
Focused on sharing practical tech content about TypeScript, Vue 3, front-end architecture, and source code analysis.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
