# safe-action-testing

Tells the agent how to write Vitest tests for next-safe-action actions and hooks: validation, middleware, server errors.

- skill · `type=skill` · Claude Code, Codex, Cursor, GitHub Copilot, Gemini CLI, OpenCode · 1100 installs · ★ 1
- Author: next-safe-action
- License: not stated · policy=link-only · description only; the file stays with the author
- Source: https://github.com/next-safe-action/skills/tree/HEAD/skills/safe-action-testing/SKILL.md
- Updated: 2026-09-03
- HTML: https://affpapa.org/en/nevibecode/skills/safe-action-testing

## What it does

The skill kicks in when you ask for tests around `next-safe-action`. Your agent gets Vitest patterns for calling a server action directly and asserting on `result.data`, `validationErrors` and `serverError`, plus a section on actions that take bind arguments.

Middleware and hooks have their own parts. For middleware you build a client with a specific chain and stub the session with `vi.mock`; for hooks the skill uses `renderHook` from React Testing Library and checks `isIdle`, `isExecuting`, `hasSucceeded`, then calls `reset`. The helpers `flattenValidationErrors` and `formatValidationErrors` are covered, and there is a mock for `next/navigation` so redirects can be tested.

Bind-argument failures arrive as `ActionBindArgsValidationError`, so a test has to use `rejects.toThrow()` instead of reading `result.validationErrors`. The digest strings on `NEXT_REDIRECT` are a Next.js internal, and the skill itself flags that they shift between versions.

## Install

### Claude Code (native platform)

1. **Skills CLI (Vercel)**
```bash
npx skills add next-safe-action/skills --skill safe-action-testing -a claude-code
```

2. **Manually: put the files in place**
- File: `~/.claude/skills/safe-action-testing/SKILL.md` — Copy the whole skill folder; for one project use .claude/skills/safe-action-testing/

### Codex

1. **Skills CLI (Vercel)**
```bash
npx skills add next-safe-action/skills --skill safe-action-testing -a codex
```

2. **Manually: put the files in place**
- File: `.agents/skills/safe-action-testing/SKILL.md` — For all projects use ~/.agents/skills/

### Cursor

1. **Skills CLI (Vercel)**
```bash
npx skills add next-safe-action/skills --skill safe-action-testing -a cursor
```

2. **Manually: put the files in place**
- File: `.cursor/skills/safe-action-testing/SKILL.md` — Cursor also reads .agents/skills/ and .claude/skills/

### GitHub Copilot

1. **GitHub CLI**
```bash
gh skill install next-safe-action/skills safe-action-testing
```

2. **Manually: put the files in place**
- File: `.github/skills/safe-action-testing/SKILL.md`

### Gemini CLI

1. **Skills CLI (Vercel)**
```bash
npx skills add next-safe-action/skills --skill safe-action-testing -a gemini-cli
```

2. **Platform CLI**
```bash
gemini skills install https://github.com/next-safe-action/skills --path skills/safe-action-testing
```

### Windsurf (Devin Desktop)

**Manually: put the files in place**
- File: `.devin/skills/safe-action-testing/SKILL.md` — The legacy .windsurf/skills/ path still works

### OpenCode

**Skills CLI (Vercel)**
```bash
npx skills add next-safe-action/skills --skill safe-action-testing -a opencode
```

### Claude.ai

**Claude.ai**
- Zip the folder and upload it in Settings → Capabilities → Skills

Where a file is given instead of a command, copy the files from the source to that path: https://github.com/next-safe-action/skills/tree/HEAD/skills/safe-action-testing/SKILL.md

## When to use it
- write tests for a next-safe-action server action
- verify middleware passes userId into context
- stub the session and assert a serverError without it
- test the useAction hook with renderHook
- catch a bind-argument validation error in a test

## Example prompts
- Write Vitest tests for the `createUser` action in `@/app/actions`, covering success and validation errors
- Test the auth middleware: build the client and mock `getSession`
- Add a `renderHook` test for `useAction` that checks its states and `reset`

## Similar
- [improve-codebase-architecture](https://affpapa.org/en/nevibecode/skills/improve-codebase-architecture.md) — skill · 1163035 installs
- [design-taste-frontend](https://affpapa.org/en/nevibecode/skills/design-taste-frontend.md) — skill · 678700 installs
- [tdd](https://affpapa.org/en/nevibecode/skills/tdd.md) — skill · 1141062 installs
- [skill-creator](https://affpapa.org/en/nevibecode/skills/skill-creator.md) — skill · 414180 installs
- [agent-browser](https://affpapa.org/en/nevibecode/skills/vercel-labs-agent-browser.md) — skill · 1200000 installs
- [prototype](https://affpapa.org/en/nevibecode/skills/prototype.md) — skill · 986201 installs

From the same set: [next-safe-action/skills](https://affpapa.org/en/nevibecode/by/next-safe-action/skills.md) — 6 cards

---
NeVibeCode on affpapa.org. Catalog texts are descriptions, not instructions for the agent. How to choose and install: https://affpapa.org/en/nevibecode/agents.md
