Migrating from Vitest
If you are using the Rstack toolchain (Rsbuild / Rslib / Rspack, etc.), migrating to Rstest gives you a more consistent development experience.
Install dependencies
First, you need to install the Rstest dependency.
Next, update the test script in your package.json to use rstest instead of vitest. For example:
rstest does not have a --run flag. Running rstest already executes tests once and exits. If you want watch mode, use --watch:
Configuration migration
Update your Vitest configuration file (e.g., vite.config.ts or vitest.config.ts) to an rstest.config.ts file:
Test configuration
Rstest test configuration is basically the same as Vitest.
When migrating config, keep these changes in mind:
- Remove the
testfield and move its nested properties to the top level. - Rename keys when required (for example,
test.environment->testEnvironment).
You can view all available test configuration options through Test Configurations.
Build configuration
Rstest uses Rsbuild as the default test build tool instead of Vite. You can view all available build configuration options in Build Configurations.
In most projects, these are the key build-side changes:
- Use
source.defineinstead ofdefine. - Use
output.externalsinstead ofssr.external. - Use Rsbuild plugins instead of Vite plugins.
If you are using Rslib or Rsbuild, you can directly use the corresponding adapter:
- For Rslib projects (with
rslib.config.*), use@rstest/adapter-rslibwithextends: withRslibConfig()(see Rslib integration reference). - For Rsbuild projects (with
rsbuild.config.*), use@rstest/adapter-rsbuildwithextends: withRsbuildConfig()(see Rsbuild integration reference).
Replace test imports and APIs
For test APIs, migration is usually just two quick changes:
- Replace imports from
vitestwith imports from@rstest/core. - Replace
viorvitestutility APIs withrsequivalents.
For the full utility API list, see Rstest APIs.
Auto-mocking modules
In Vitest, calling vi.mock() with just the module path first attempts to load a manual mock from the corresponding __mocks__ directory. If no manual mock is found, it automatically mocks the module, replacing all its exports with empty mock functions.
Rstest handles this differently. Calling rs.mock() with only the module path will only look for a mock in the __mocks__ directory and will throw an error if one isn't found. To achieve auto-mocking, you must explicitly pass the { mock: true } option.
Mock async modules
When you need to mock a module's return value, Rstest does not support returning an async function.
As an alternative, Rstest provides synchronous importActual capability, allowing you to import the unmocked module implementation through static import statements: