Rstest is designed to be Jest-compatible, making migration from Jest projects straightforward. Here's how to migrate your Jest project to Rstest:
First, you need to install Rstest as a development dependency.
Next, update the test script in your package.json to use rstest instead of jest. For example:
Update your jest config file (e.g., jest.config.js or jest.config.ts) to a rstest.config.ts file.
Here are some common Jest configurations and their Rstest equivalents:
| Jest Configuration | Rstest Equivalent |
|---|---|
testRegex | include |
testMatch | include |
testPathIgnorePatterns | exclude |
transformIgnorePatterns | output.externals、source.exclude |
displayName | name |
rootDir | root |
setupFilesAfterEnv | setupFiles |
verbose | verbose-reporter |
injectGlobals | globals |
moduleNameMapper | resolve.alias |
maxWorkers | pool.maxWorkers |
collectCoverage | coverage.enabled |
coverageDirectory | coverage.reportsDirectory |
coverageProvider | coverage.provider |
coveragePathIgnorePatterns | coverage.exclude |
coverageThreshold | coverage.thresholds |
For more details, please refer to the Configuration section.
Rstest does not mount the test APIs (e.g., describe, expect, it, test) to the global object by default, which is different from Jest.
If you want to continue using the global test APIs, you can enable the globals option in your rstest.config.ts file:
Rstest uses swc for code transformation by default, which is different from Jest's babel-jest. Most of the time, you don't need to change anything. And you can configure your swc options through tools.swc.
However, if you have custom Babel configurations or use specific Babel plugins/presets, you can add Rsbuild's Babel Plugin:
Your existing Jest test files should work with minimal changes since Rstest provides Jest-compatible APIs. Simply update your imports from Jest to Rstest:
Rstest provides a rstest API that you can use to access Rstest's utilities, such as rstest.fn() and rstest.mock(). Just like Jest's jest.fn() and jest.mock(). More utilities can be found in the Rstest APIs.
The done callback is not supported in Rstest. Instead, you can return a Promise or use async/await for asynchronous tests.
If you need to handle errors, you can modify it as follows:
The return functions of the beforeEach and beforeAll hooks in Rstest are used to perform cleaning work after testing.
If you used jest.setTimeout() to set the timeout for a test, you can use rstest.setConfig() instead.