Skip to main content
skill CX Codex

rstest-best-practices — Claude Code skill for Rstest tests

Rules for setting up and writing Rstest tests: environments, mocks, snapshots, coverage and CI. Useful when a package moves to Rstest or its tests have drifted apart.

Install into Claude Code · Skills CLI (Vercel) All methods and platforms ↓
npx skills add rstackjs/agent-skills --skill rstest-best-practices -a claude-code
Open on GitHub

What rstest-best-practices does

The skill kicks in when you set up Rstest, write new tests, or review someone else's. The agent first checks the package manager, module mode, build configuration and test layout, then edits the config. After that it adds @rstest/core, a package-local test script, and a config file whose extension matches the package's module mode.

Most of the content is configuration and environments. defineConfig from @rstest/core, explicit imports instead of globals: true, setupFiles for shared matchers and cleanup. Server tests get node, simulated DOM gets happy-dom or jsdom, real browser behaviour gets Browser Mode, and the environment package has to be installed explicitly. Mocking has its own section: rs.fn(), rs.spyOn(), rs.mock(), plus a warning that a module mock does not always remove the real dependency from the Rspack build graph — aliases or narrowly scoped externals may be needed.

There is a debugging section too. Start with a focused repro and --reporter=verbose, reach for --printConsoleTrace when console output is noisy, and use DEBUG=rstest with dist/.rstest-temp/.rsbuild/ to inspect the final Rstest, Rsbuild and Rspack configuration. Watch mode is off-limits in CI; for distributed runs the skill points to sharding with blob reports and rstest merge-reports. It will not pick a coverage provider for you: choose one supported by the installed Rstest version and add its package explicitly.

What it helps with

  • set up an Rstest config in a package with a non-standard test layout
  • pick an environment for server, DOM or real-browser tests
  • separate module mocks from dependencies that still reach the build
  • wire coverage with the right provider and CI-ready reporters
  • trace why a test fails at startup or at runtime

Example prompts for the agent

  • Set up Rstest in this package: check the package manager and module mode, add a config and a test script
  • Review my Rstest tests and flag module mocks that still pull the real dependency into the build
  • Help me debug a failing test, starting with a focused repro and the verbose reporter

How to install rstest-best-practices

1Skills CLI (Vercel)

npx skills add rstackjs/agent-skills --skill rstest-best-practices -a claude-code

2Manually: put the files in place

~/.claude/skills/rstest-best-practices/SKILL.md

Copy the whole skill folder; for one project use .claude/skills/rstest-best-practices/

Step by step, with commands for each platform: How to install skills in Claude Code: five ways

Questions about rstest-best-practices

How do I install rstest-best-practices in Claude?

Run npx skills add rstackjs/agent-skills --skill rstest-best-practices -a claude-code. The skill lands in ~/.claude/skills/rstest-best-practices/SKILL.md.

What does rstest-best-practices do?

It gives the agent rules for Rstest: configuring the config file and environments, writing tests, mocking, snapshots, coverage, and running the suite in CI.

Does it need anything installed first?

The environment package is chosen explicitly and installed separately: happy-dom, jsdom or Browser Mode. The coverage provider is also added as its own package, matched to the installed Rstest version.

Source text

SKILL.md · MIT
---
name: rstest-best-practices
description: Set up, write, or review Rstest tests and configuration, including environments, mocks, snapshots, coverage, and CI.
---

# Rstest best practices

Apply these rules when setting up, writing, or reviewing Rstest projects.

## Workflow

- Inspect the existing package manager, dependency policy, module mode, build configuration, test layout, and framework conventions before editing.
- Add `@rstest/core`, a package-local test script, and a config file whose extension matches the package's module mode.
- Reuse existing build configuration only when doing so lowers maintenance cost and remains compatible. An adapter is optional; use an independent Rstest config when adapter integration is incompatible or disproportionately costly, and explicitly reproduce required aliases, plugins, defines, environments, and externals.
- Configure test discovery, environment, and setup files intentionally. Do not enable `passWithNoTests` unless an empty test set is an expected and documented state.
- Verify test discovery with `rstest list`, then run focused tests, the full suite, and the project's existing build or type-check command.

## Configuration and environments

- Use `defineConfig` from `@rstest/core` and a config file extension compatible with the package's module mode.
- Prefer explicit imports from `@rstest/core` over `globals: true`.
- Use `setupFiles` for shared matchers and cleanup; use `plugins`, `resolve`, or `source` for ordinary build integration and low-level `tools` only when necessary.
- Set `include` explicitly when the package's test layout is non-standard; do not assume the default discovery pattern.
- Use `node` for server/SSR tests, `happy-dom` or `jsdom` for simulated DOM tests, and Browser Mode for real-browser behavior; install the selected environment package explicitly.
- For React or Vue component tests, register the matching build plugin, use the corresponding Testing Library, and centralize matcher extension and cleanup in a setup file.
- Use multiple test projects only when environments or configurations genuinely differ; keep global options such as reporters, coverage, pool, isolation, and bail at the root.

## Test writing

- Prefer small tests that verify one public behavior; use `.skip` or `.todo` intentionally and never commit `.only`.
- Keep test APIs and test files in the same module system as the project; do not mix ESM/CJS assumptions without verifying the runtime and bundler behavior.
- Prefer `await expect(promise).resolves...` and `.rejects...` over `try/catch` patterns that can miss assertions.
- Use `includeSource` only for small utilities, guard tests with `import.meta.rstest`, and define it as `false` in production builds.

## Mocking

- Use `rs.fn()` for functions, `rs.spyOn()` for object methods, and `rs.mock()` factories for modules; mock external boundaries rather than the subject under test.
- Choose `clearMocks`, `resetMocks`, or `restoreMocks` according to whether calls, implementations, or original methods must be restored between tests.
- Choose the module-mocking API that matches the module system; verify the exact request string and register hoisted/static mocks before the module under test is evaluated.
- Module mocks replace runtime behavior but do not necessarily remove the real dependency from the Rspack build graph. Use aliases or narrowly scoped externals only when the build still traverses an unwanted or unavailable dependency, and verify the runtime externalization format.

## Snapshots and coverage

- Keep snapshots small and deterministic: use inline snapshots for short output, file snapshots for large structured output, and serializers for paths or unstable data. Review every update.
- Enable coverage with `--coverage` or `coverage.enabled`.
- Choose a coverage provider supported by the installed Rstest version and add its package explicitly; do not copy a V8 or Istanbul provider choice without checking project needs.
- Set `coverage.include` deliberately before adding thresholds; choose reporters for humans and CI artifacts separately.

## Running and CI

- Use `rstest` for a single run; `rstest run` is an optional explicit equivalent. Use `rstest --watch` or `rstest watch` only for local development.
- Use `rstest list` to verify discovery, positional filters or `-t` for focused runs, `-u` for intentional snapshot updates, and `-c` for a non-default config.
- In CI, never use watch mode. Use sharding with blob reports and `rstest merge-reports` when distributing tests; use JUnit when the CI system requires machine-readable results.
- If passing-test logs are too noisy, consider `silent: 'passed-only'`; disable it temporarily when diagnosing setup or runtime output.

## First-line debugging

- Start with a focused repro and `--reporter=verbose`; use `--printConsoleTrace` for noisy or unclear console output.
- Use `DEBUG=rstest` and inspect `dist/.rstest-temp/.rsbuild/` to verify final Rstest/Rsbuild/Rspack configuration and generated build output.
- Use breakpoints through the Rstest VS Code extension or a JavaScript Debug Terminal for runtime failures.
- Use `rstest-debugging` for systematic startup, build, runtime, logging, memory, or performance diagnosis.

## Documentation

- For the latest Rstest docs, read https://rstest.rs/llms.txt

© rstackjs · MIT · rstackjs/agent-skills

Similar skills

Everything for Testing and QA →

From the same set9

rsbuild-best-practices Provides rules for configuring, reviewing, and troubleshooting Rsbuild applications. Helps agents inspect builds, handle assets, and profile performance. CX Codex +1 2,700installs rslib-best-practices Provides guidelines for building JavaScript and TypeScript libraries with Rslib. Useful when configuring outputs, declaration files, and dependency bundling. CX Codex +1 1,000installs rspress-best-practices A skill that gives the agent rules for Rspress v2 sites: config, navigation, MDX, assets, search and deployment. Useful once the docs exist and the agent edits them. CX Codex +1 538installs rspack-v2-upgrade Walks a Rspack 1.x project through the v2 upgrade: dependencies, config, build checks. Useful when a major bump is too fiddly to do by hand. CX Codex +1 515installs rspress-description-generator A skill that fills in the description frontmatter field on Rspress pages that lack one. Useful when the docs site has grown and meta descriptions are missing in places. CX Codex +1 498installs rsbuild-v2-upgrade Walks a Rsbuild 1.x project through the v2 migration: dependencies, config, breaking options. Useful when a major bump would otherwise eat an evening. CX Codex +1 476installs migrate-to-rstest Migrates Jest, Vitest, or Playwright Test suites to Rstest while keeping test discovery, assertions, mocks, snapshots, and coverage intact. For moving a runner without rewriting scenarios. CX Codex +1 420installs rspress-custom-theme A skill that walks your agent through Rspress v2 theme changes, from CSS variables up to replacing built-in components. CX Codex +1 408installs

Project sponsors

Companies that keep this analytics open
Gold sponsor

Impressions, clicks and code copies — open statistics. We keep the numbers public for the same reason NeBlask itself is public. Become a sponsor