rstest-best-practices
Rstest best practices for config, CLI workflow, test writing, mocking, snapshot testing, DOM testing, coverage, multi-project setup, CI integration, performance and debugging. Use when writing, reviewing, or troubleshooting Rstest test projects.
How do I install this agent skill?
npx skills add https://github.com/rstackjs/agent-skills --skill rstest-best-practicesIs this agent skill safe to install?
- Gen Agent Trust Hubpass
This skill provides a comprehensive guide for using Rstest, a testing framework. It contains best practices for configuration, CLI usage, and CI integration. No security risks were identified.
- Socketpass
No alerts
- Snykpass
Risk: LOW · No issues
- Runlayerpass
1 file scanned · No issues
- ZeroLeakspass
Score: 93/100 · 2 sections analyzed
What does this agent skill do?
Rstest Best Practices
Apply these rules when writing or reviewing Rstest test projects.
Configuration
- Use
rstest.config.tsanddefineConfigfrom@rstest/core - Prefer explicit imports
import { test, expect, describe } from '@rstest/core'overglobals: true - For Rsbuild projects, use
@rstest/adapter-rsbuildwithextends: withRsbuildConfig()to reuse build config - For Rslib projects, use
@rstest/adapter-rslibwithextends: withRslibConfig()to reuse build config - Use
setupFilesfor shared test setup (e.g., custom matchers, cleanup hooks) - When using Rsbuild plugins (e.g.,
@rsbuild/plugin-react), add them via thepluginsfield - For deep-level or advanced build configuration needs, use
tools.rspackortools.bundlerChain
CLI
- Use
rstestorrstest runto run tests (rundisables watch mode, suitable for CI) - Use
rstest --watchorrstest watchfor local development with file watching - Use
rstest listto list all test files and test names - Use
rstest -uto update snapshots - Use
--reporter=verbosewhen debugging test failures for detailed output - Use
--config(-c) to specify a custom config file path
Test writing
- Import test APIs from
@rstest/core:test,describe,expect,beforeEach,afterEach, etc. - Use
testoritfor test cases; usedescribefor grouping related tests - Use
.onlyto focus on specific tests during development, but never commit.onlyto the codebase - Use
.skipor.todoto mark incomplete or temporarily skipped tests - Prefer small, focused test cases that test a single behavior
- For async error paths, prefer
await expect(fn()).rejects.toThrow(ErrorClass)(or.rejects.toMatchObject({ ... })) overtry/catchwithexpect.failor.catch(e => e)patterns — the matcher form fails clearly if the promise unexpectedly resolves, keeps the assertion in one chain, and avoids forgetting to assert the throw at all - For async happy paths, use
await expect(fn()).resolves.toEqual(...)for the same reason - Use
includeSourcefor in-source testing of small utility functions (Rust-styleimport.meta.rstest) - For in-source tests, wrap test code in
if (import.meta.rstest) { ... }and defineimport.meta.rstestasfalsein production build config
Test environment
- Use
testEnvironment: 'node'(default) for Node.js / server-side code - Use
testEnvironment: 'jsdom'ortestEnvironment: 'happy-dom'for DOM / browser API testing - Install
jsdomorhappy-domas a dev dependency when using DOM environments - Prefer
happy-domfor faster DOM testing; usejsdomwhen better browser API compatibility is needed - For real browser testing, use
@rstest/browserwith Playwright - Use inline project configs to run different test environments within one project (e.g.,
nodeandjsdomprojects)
React / Vue testing
- For React: use
@rsbuild/plugin-reactplugin and@testing-library/reactfor component testing - For Vue: use
@rsbuild/plugin-vueplugin and@testing-library/vuefor component testing - Create a
rstest.setup.tswithexpect.extend(jestDomMatchers)andafterEach(() => cleanup())for Testing Library - Add the setup file to
setupFilesin config - For SSR testing, use
testEnvironment: 'node'and test withreact-dom/serveror framework-specific SSR APIs
Mocking
- Use
rs.mock('./module')to mock modules - Use
rs.fn()to create mock functions - Use
rs.spyOn(object, 'method')to spy on methods - Prefer
clearMocks,resetMocks, orrestoreMocksconfig options to automatically clean up mocks between tests - Use factory functions in
rs.mock('./module', () => ({ ... }))to provide mock implementations
Snapshot testing
- Use
toMatchSnapshot()for general snapshot testing - Use
toMatchInlineSnapshot()for small, readable inline snapshots - Use
toMatchFileSnapshot()for large or structured outputs (e.g., HTML, generated code) - Keep snapshots concise — only include relevant data, avoid timestamps and session IDs
- Use
expect.addSnapshotSerializer()to mask paths or sensitive data in snapshots - Use
path-serializerto normalize file paths across platforms - Review snapshot changes carefully in code review
Coverage
- Enable coverage with
--coverageCLI flag orcoverage.enabled: truein config - Install
@rstest/coverage-istanbulfor the Istanbul coverage provider - Use
coverage.includeto specify source files for coverage (e.g.,['src/**/*.{js,ts,tsx}']) - Use
coverage.thresholdsto enforce minimum coverage requirements - Use
coverage.reportersto generate reports in different formats (e.g.,text,lcov,html)
Multi-project testing
- Use
projectsfield in root config to define multiple test projects - For monorepos, use glob patterns like
'packages/*'to auto-discover sub-projects - Use
defineProjecthelper in sub-project configs - Extract shared config and use
mergeRstestConfigto compose project configs - Global options (
reporters,pool,isolate,coverage,bail) must be set at the root level, not in projects
CI integration
- Use
rstest run(notrstest watch) in CI - Use
--shardfor parallel test execution across CI machines (e.g.,--shard 1/3) - Use
--reporter=blobwithrstest merge-reportsto combine sharded results - Use
--reporter=junitwithoutputPathfor CI report integration - The
github-actionsreporter is auto-enabled in GitHub Actions for inline error annotations - Use
--bailto stop early on first failure when appropriate
Performance
- Disable
isolate(--no-isolate) when tests have no side effects for faster execution via module cache reuse - Use
pool.maxWorkersto control parallelism based on available resources - Keep test build fast by avoiding unnecessary Rspack plugins in test config
- Use test filtering (
rstest <pattern>or-t <name>) to run only relevant tests during development - Leverage watch mode's incremental re-runs for fast local feedback
Debugging
- Run with
DEBUG=rstestto enable debug mode, which writes final configs and build outputs to disk - Read generated files in
dist/.rstest-temp/.rsbuild/to confirm final Rstest/Rsbuild/Rspack config - Use VS Code's JavaScript Debug Terminal to run
rstestwith breakpoints - Use
--reporter=verbosefor detailed per-test output - Use
--printConsoleTraceto trace console calls to their source - Add VS Code launch config for debugging specific test files with
@rstest/core/bin/rstest.js
Profiling
- Use Rsdoctor with
RSDOCTOR=true rstest runto analyze test build performance - Use
samplyfor native profiling of both main and worker processes - Use Node.js
--heap-proffor memory profiling
Toolchain integration
- Use the official VS Code extension (
rstack.rstest) for in-editor test running and debugging - For Rslib libraries, use
@rstest/adapter-rslibfor config reuse - For Rsbuild apps, use
@rstest/adapter-rsbuildfor config reuse - Use
process.env.RSTESTto detect test environment and apply test-specific config
Documentation
- For the latest Rstest docs, read https://rstest.rs/llms.txt
How can the creator link this skill?
Add the canonical catalog link to the repository README so users can inspect current installs and available audits. The publishing guide covers the complete discovery path.
<a href="https://skillzs.dev/skills/rstackjs/agent-skills/rstest-best-practices">View rstest-best-practices on skillZs</a>