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 or installhappy-domorjsdomfor a simulated DOM. Use Browser Mode for component tests that execute in a real browser. For end-to-end tests that drive a running app from a Node worker, use Rstest's Playwright integration. - 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. - Match the mock to how the code loads the dependency. Use
rs.mock()for an ESM import andrs.mockRequire()for a CommonJSrequire(). For a partial ESM mock, import the real exports withimport * as actual from './api' with { rstest: 'importActual' }and spreadactualin the synchronous factory. - 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.
- If thresholds apply to the whole source tree, set
coverage.includeto its source globs and check that an untested source file appears in the report. By default, coverage includes only tested files.
Running and CI
- Use
rstestfor a single run;rstest runis an optional explicit equivalent. Userstest --watchorrstest watchonly for local development. - Use
rstest list --filesOnlyfor file discovery andrstest listfor test names. Use positional filters or-tfor focused runs,-ufor intentional snapshot updates, and-cfor a non-default config. - For sharded CI, run each shard with the blob reporter, collect the blobs in
.rstest-reports/, and runrstest merge-reportsfrom the project root with the desired reporters (such as JUnit). - If passing-test logs are too noisy, consider
silent: 'passed-only'; disable it temporarily when diagnosing setup or runtime output.
Worker pool performance
- In Rstest v0.12.0 and later, try a VM pool when many test files spend much of their time starting workers or compiling the same dependencies. Each file gets a fresh VM context in a reused worker. Setup files still run for each test file.
- Start with a CLI trial on a passing suite:
rstest run --pool.type vmForks --bail 1for child processes orrstest run --pool.type vmThreads --bail 1for worker threads. - If the trial fails or tests become flaky, remove the trial flags and rerun the same tests with the original pool. Consult VM pool behavior when investigating a VM-only failure.
- If the trial passes, drop the trial's
--bail 1and compare repeated full-suite runs with the same worker count and cache conditions. Setpool.typein the root config only if the suite stays green and the speedup is repeatable within the project's memory budget.
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