Introduction¶
Writing missing tests is a common yet brain-draining task in daily development: you have to figure out whether the project uses Jest, Vitest, or another runner, trace branches and boundaries against the source code, and finally mock external dependencies. When narrating these requirements from scratch each time, agents tend to miss conventions or write test cases that are inconsistent with the repository’s style. Formalizing the process of “detect framework → analyze code → write mocks & assertions → run successfully” into a Skill ensures that adding tests and improving coverage follow the same reproducible workflow.
What is this¶
writing-tests is a Cursor Agent Skill listed in the awesome-cursor-skills repository, maintained by the open-source collection (author Spencer Pauly and other contributors). The Skill itself is an instruction file named SKILL.md that conforms to Cursor’s specifications. It enables the Agent to analyze existing code and write unit and integration tests with mocks, boundary cases, and assertions. It automatically identifies the testing framework in the project, points out uncovered paths, and is triggered when the user requests adding tests, increasing coverage, or writing tests for a specific module.
Cursor discovers and loads Skills from .cursor/skills/ (personal) or the project directory. Like other entries in the collection, this Skill is a ready-to-use workflow description intended to be copied and used, rather than a standalone npm package.
Core Functions¶
Based on the official SKILL.md, the capabilities can be summarized as follows:
- Detect the testing environment: Look for jest, vitest, mocha, playwright, cypress, etc., in
package.json; search for configuration files likevitest.config.ts,jest.config.ts,playwright.config.ts,.mocharc.*; and read existing test files to align with project habits. - Assist in setup if no runner exists: The examples primarily focus on Vitest + Testing Library, providing installation commands and
test/test:watchscripts forpackage.json. - Analyze the code under test: Map out external APIs (exported functions, classes, components, hooks), conditional and error branches, dependencies requiring mocking, and side effects such as file I/O, network requests, and DOM manipulation.
- Write test files according to conventions: Place them in the same directory as the source file or in
__tests__/, using naming conventions likeformat.ts→format.test.ts,Button.tsx→Button.test.tsx. - Structure test cases: Include happy paths, null values/boundaries, error throwing, async operations, etc.; use
vi.mockfor external modules and React data hooks; use Testing Library for rendering and interaction assertions on the component side. - Execute and fix: Run
npm test, and upon failure, distinguish between incorrect expectations and implementation issues before making corrections.
The official instructions also clarify “what to test / what not to test”: prioritize public APIs, error handling, boundaries, state, and async behavior; avoid testing private implementation details, third-party library internals, simple getters/setters, and pure type code. Test case names should describe the scenario, and async operations should use await or resolves / rejects.
Installation and Activation¶
- Obtain the Skill file from the official directory (the collection instructions suggest copying the
SKILL.mdto the Skill directory):- Source path: resources/writing-tests
- Place it in one of the locations Cursor can discover:
- User-level: A corresponding subdirectory under
.cursor/skills/(containing theSKILL.md) - Or within the current project repository’s
.cursor/skills/
- User-level: A corresponding subdirectory under
- Once saved, the Cursor Agent will discover it automatically. You can trigger it in the conversation by stating intents such as “add tests for a file,” “increase coverage,” or “write tests for a module” (consistent with the Skill’s
description).
The materials do not separately list installation differences for Codex CLI or Claude Code, so this will not be expanded upon here.
Typical Usage¶
The following steps are extracted from the official workflow to help you understand what the Agent actually does.
1. Confirm if a test stack already exists
# Check package.json for test runner
cat package.json | grep -E "jest|vitest|mocha|playwright|cypress"
Also, check configuration files and existing *.test.* styles.
2. If no runner exists, install Vitest (React scenario example)
npm install -D vitest @testing-library/react @testing-library/jest-dom
Add to package.json:
{ "test": "vitest run", "test:watch": "vitest" }
3. Structural example when generating tests (Vitest)
import { describe, it, expect, vi } from "vitest";
describe("functionName", () => {
// Happy path
it("returns formatted output for valid input", () => { ... });
// Edge cases
it("handles empty string", () => { ... });
it("handles null/undefined input", () => { ... });
// Error cases
it("throws on invalid argument", () => { ... });
// Boundary conditions
it("handles maximum length input", () => { ... });
});
4. Mock external dependencies
vi.mock("@/lib/db", () => ({
query: vi.fn().mockResolvedValue([{ id: 1, name: "test" }]),
}));
In React, you can mock data hooks:
vi.mock("@/hooks/useUser", () => ({
useUser: () => ({ user: { name: "Test" }, isLoading: false }),
}));
5. Component testing (Testing Library)
import { render, screen, fireEvent } from "@testing-library/react";
it("renders the button and handles click", () => {
const onClick = vi.fn();
render(<Button onClick={onClick}>Click me</Button>);
fireEvent.click(screen.getByRole("button", { name: "Click me" }));
expect(onClick).toHaveBeenCalledOnce();
});
6. Run
npm test
If the project originally uses test() instead of it(), the Skill requires the Agent to follow the existing style rather than forcing a unified syntax.
Applicable Scenarios and Notes¶
Suitable for: Existing business code that needs supplementary unit/integration tests; desire to increase coverage with cases covering null values, boundaries, and error paths; modules dependent on databases, APIs, or hooks that require stable mocking; React components requiring behavior-level assertions rather than a pile of snapshots.
Notes:
- The Skill examples are biased towards Node/React + Vitest, but the first step detects Jest, Mocha, Playwright, Cypress, etc.; applicability to non-JS tech stacks depends on whether the Agent can find equivalent testing conventions in the same repository. The official text focuses on the JavaScript/TypeScript ecosystem.
- “Integration tests” appear alongside unit tests in the description. Specific boundaries should still obey the project’s existing layering (e.g., whether to use real HTTP). The Skill emphasizes not hitting real APIs or databases on the unit testing side.
- The same collection includes other entries like
adding-e2e-tests,parallel-test-fixing, andgrinding-until-pass. End-to-end testing or batch fixing of failing tests can be used in combination; this article only describeswriting-testsitself.
Conclusion¶
Test generation is one of the high-frequency scenarios in AI-assisted programming. Writing the steps of framework detection, path analysis, mocking, and assertion into a Skill reduces the cost of repeatedly explaining specifications every time tests need to be added. For the official Skill and instructions, see: writing-tests (awesome-cursor-skills).