Skip to content
Back to skills

Write Tests

ASecurity

Add test coverage to existing code with correct mock patterns. Use when adding tests to untested modules, writing regression tests for bugs, or user asks to test a specific file. Handles mockReset:true, vi.hoisted(), forwarding pattern, and test app builder utilities.

  • 225 stars
  • 0 votes
  • 0 copies
  • 2 views
  • Added September 4, 2026
testingtypescriptbashapi

Works with

  • api

Security analysis

A100/100

Pro scans all 13 files and shows the line behind each finding

Scanned September 4, 2026

npx -y skills add NeverSight/skills_feed --skill write-tests --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Write Tests?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Write Tests
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/neversight-write-tests/badge)](https://www.skillsdirectory.com/skills/neversight-write-tests)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: write-tests
description: >-
  Add test coverage to existing code with correct mock patterns. Use when adding
  tests to untested modules, writing regression tests for bugs, or user asks to
  test a specific file. Handles mockReset:true, vi.hoisted(), forwarding
  pattern, and test app builder utilities.
---

# Write Tests for Existing Code

## Before Writing, Ask Yourself

- **Module type?** Route handler, repository, plugin, utility, or service — each has a different mock strategy
- **Blast radius?** Does this module have side effects (DB writes, API calls) that need isolation?
- **Nearest test file?** Find the closest `*.test.ts` and match its structure exactly

## Mock Strategy by Module Type

| Module Type      | Strategy                                               |
| ---------------- | ------------------------------------------------------ |
| Route handler    | Test app builder + session simulation + `app.inject()` |
| Repository       | Mock DB connection + counter-based `execute`           |
| Framework plugin | Real framework instance + selective dependency mocks   |
| Pure utility     | No mocks — test inputs/outputs directly                |
| Service w/ DI    | Mock injected deps via forwarding pattern              |

## Mock Setup (mockReset: true)

If your test runner uses `mockReset: true`, most examples from the internet will **silently fail**.

```typescript
const { mockFn } = vi.hoisted(() => ({
  mockFn: vi.fn(),
}));

vi.mock("./dependency", () => ({
  dependency: (...args: unknown[]) => mockFn(...args),
}));

beforeEach(() => {
  // MUST reconfigure here — mockReset clears return values between tests
  mockFn.mockResolvedValue(defaultResult);
});
```

For complex TDZ cases (multiple interdependent mocks), use the **globalThis registry pattern**.

## NEVER

- **NEVER chain `mockResolvedValueOnce`** — `mockReset` clears the chain between tests. Use counter-based `mockImplementation` instead.
- **NEVER define mock variables at module scope then reference in `vi.mock()` factories** — hoisting creates a temporal dead zone. Use `vi.hoisted()` or globalThis.
- **NEVER `vi.importActual()` for modules with side effects** — use selective re-exports.
- **NEVER test implementation details** (private state, internal call order) — test behavior through the public API.
- **NEVER copy mock patterns from other projects** — check YOUR test runner config first.
- **NEVER modify source code** — this skill writes tests only.

## Metacognitive Rule

**If >3 tests fail on first run**: STOP. The root cause is almost certainly a mock wiring issue affecting all tests, not individual test logic errors. Re-examine the mock setup strategy holistically before fixing tests one by one.

## Run

```bash
npx vitest run <test-file> --reporter=verbose
```

## Arguments

- `$ARGUMENTS`: Path to the source file or module to cover
  - Example: `/write-tests src/routes/admin/settings.ts`
  - If empty, ask the user which file needs test coverage

Files in this skill

  • SKILL.md2.9 KB
  • description_ar.txt559 B
  • description_cn.txt282 B
  • description_de.txt386 B
  • description_en.txt269 B
  • description_es.txt353 B
  • description_fr.txt402 B
  • description_it.txt337 B
  • description_ja.txt468 B
  • description_ko.txt400 B
  • description_ru.txt656 B
  • description_tw.txt285 B
  • stats.json66 B

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in testing →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.