rstest-best-practices
rstackjs/agent-skills
Set up, write, and review Rstest tests with best practices for environments, mocks, snapshots, coverage, and CI.
What is rstest-best-practices?
Rstest is a Rust testing framework. This skill guides you through configuring Rstest projects, writing maintainable tests, managing mocks and snapshots, and integrating with CI pipelines. Use it when setting up a new Rstest project, reviewing test code, or optimizing test configuration and coverage.
- Configure Rstest with appropriate environments (node, happy-dom, jsdom, or Browser Mode) and setup files
- Write focused, single-behavior tests with proper async/await patterns and avoid common pitfalls like .only commits
- Mock external boundaries using rs.fn(), rs.spyOn(), and rs.mock() with correct module-system matching
- Manage snapshots with inline or file-based strategies and deterministic serializers for unstable data
- Enable and configure coverage reporting with appropriate providers and thresholds
- Integrate Rstest into CI with sharding, blob reports, and machine-readable output formats
How to install rstest-best-practices
npx skills add https://github.com/rstackjs/agent-skills --skill rstest-best-practices- Rstest installed and basic familiarity with Rust testing concepts
- Node.js and npm/yarn/pnpm for managing @rstest/core and related packages
- Understanding of your project's module mode (ESM or CJS) and build configuration
How to use rstest-best-practices
- 1.Inspect your existing package manager, module mode, build configuration, and test layout
- 2.Install @rstest/core and add a package-local test script and config file matching your module mode
- 3.Configure test discovery, environments, and setup files explicitly using defineConfig
- 4.Write focused tests with proper async/await patterns and avoid .only in commits
- 5.Set up mocks using rs.fn(), rs.spyOn(), or rs.mock() for external boundaries only
- 6.Enable coverage with an appropriate provider and set thresholds after reviewing coverage.include
- 7.Verify test discovery with rstest list and run the full suite plus your project's build command
- 8.In CI, use sharding with blob reports and rstest merge-reports for distributed test runs
Use cases
- Set up a new Rstest project with correct discovery, environment, and config file matching the package's module mode
- Review existing test code for best practices in mocking, async patterns, and snapshot management
- Configure coverage thresholds and reporters for both local development and CI artifacts
- Debug test failures using verbose reporting, DEBUG environment variables, and the Rstest VS Code extension
- Migrate or optimize an existing Rstest suite to use multiple test projects, proper isolation, and CI sharding
- Rust developers writing or maintaining test suites
- QA engineers setting up test infrastructure and CI integration
- Tech leads reviewing test code quality and coverage strategies
- DevOps engineers configuring Rstest in CI/CD pipelines
rstest-best-practices FAQ
Prefer explicit imports from @rstest/core over globals: true for better clarity and maintainability.
Use happy-dom or jsdom for simulated DOM tests, Browser Mode for real-browser behavior, and node for server/SSR tests. Install the selected environment package explicitly.
Choose the module-mocking API that matches your project's module system, verify the exact request string, and register hoisted/static mocks before the module under test is evaluated.
Use inline snapshots for short output and file snapshots for large structured output. Keep snapshots small and deterministic, and review every update.
Start with a focused repro and --reporter=verbose, use DEBUG=rstest to inspect configuration, and use breakpoints through the Rstest VS Code extension for runtime failures.
Full instructions (SKILL.md)
Source of truth, from rstackjs/agent-skills.
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
passWithNoTestsunless 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
defineConfigfrom@rstest/coreand a config file extension compatible with the package's module mode. - Prefer explicit imports from
@rstest/coreoverglobals: true. - Use
setupFilesfor shared matchers and cleanup; useplugins,resolve, orsourcefor ordinary build integration and low-leveltoolsonly when necessary. - Set
includeexplicitly when the package's test layout is non-standard; do not assume the default discovery pattern. - Use
nodefor server/SSR tests,happy-domorjsdomfor 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
.skipor.todointentionally 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...overtry/catchpatterns that can miss assertions. - Use
includeSourceonly for small utilities, guard tests withimport.meta.rstest, and define it asfalsein production builds.
Mocking
- Use
rs.fn()for functions,rs.spyOn()for object methods, andrs.mock()factories for modules; mock external boundaries rather than the subject under test. - Choose
clearMocks,resetMocks, orrestoreMocksaccording 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
--coverageorcoverage.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.includedeliberately before adding thresholds; choose reporters for humans and CI artifacts separately.
Running and CI
- Use
rstestfor a single run;rstest runis an optional explicit equivalent. Userstest --watchorrstest watchonly for local development. - Use
rstest listto verify discovery, positional filters or-tfor focused runs,-ufor intentional snapshot updates, and-cfor a non-default config. - In CI, never use watch mode. Use sharding with blob reports and
rstest merge-reportswhen 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--printConsoleTracefor noisy or unclear console output. - Use
DEBUG=rstestand inspectdist/.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-debuggingfor systematic startup, build, runtime, logging, memory, or performance diagnosis.
Documentation
- For the latest Rstest docs, read https://rstest.rs/llms.txt
Related skills
More from rstackjs/agent-skills and the wider catalog.

rsbuild-best-practices
Configure, review, and optimize Rsbuild applications with build, dev, and bundle best practices.

rslib-best-practices
Rslib best practices for config, CLI workflow, output, declaration files, dependency handling, build optimization and toolchain integration. Use when writing, reviewing, or troubleshooting Rslib projects.

file-manager
Agent skill from rtgs2017/nagaagent.
code-simplifier
Simplify Rust code in RTK by applying idiomatic patterns without changing behavior.
design-patterns
Rust design patterns for RTK CLI filter modules: Newtype, Builder, RAII, Trait Objects, State Machine.
rtk-tdd
Enforces Red-Green-Refactor TDD discipline for Rust with idiomatic test patterns and automated workflow.