|
| 1 | +# Migrating from jest |
| 2 | + |
| 3 | +Rstest is designed to be Jest-compatible, making migration from Jest projects straightforward. Here's how to migrate your Jest project to Rstest: |
| 4 | + |
| 5 | +## Installation and setup |
| 6 | + |
| 7 | +First, you need to install Rstest as a development dependency. |
| 8 | + |
| 9 | +import { PackageManagerTabs } from '@theme'; |
| 10 | + |
| 11 | +<PackageManagerTabs command="add @rstest/core -D" /> |
| 12 | + |
| 13 | +Next, update the test script in your `package.json` to use [rstest](/guide/basic/cli) instead of `jest`. For example: |
| 14 | + |
| 15 | +```diff |
| 16 | +"scripts": { |
| 17 | +- "test": "jest" |
| 18 | ++ "test": "rstest" |
| 19 | +} |
| 20 | +``` |
| 21 | + |
| 22 | +## Configuration migration |
| 23 | + |
| 24 | +Update your jest config file (e.g., `jest.config.js` or `jest.config.ts`) to a `rstest.config.ts` file. |
| 25 | + |
| 26 | +```ts title='rstest.config.ts' |
| 27 | +import { defineConfig } from '@rstest/core'; |
| 28 | + |
| 29 | +export default defineConfig({ |
| 30 | + globals: true, |
| 31 | +}); |
| 32 | +``` |
| 33 | + |
| 34 | +Here are some common Jest configurations and their Rstest equivalents: |
| 35 | + |
| 36 | +| Jest Configuration | Rstest Equivalent | |
| 37 | +| ---------------------------- | -------------------- | |
| 38 | +| `testRegex` | `include` | |
| 39 | +| `testMatch` | `include` | |
| 40 | +| `testPathIgnorePatterns` | `exclude` | |
| 41 | +| `injectGlobals` | `globals` | |
| 42 | +| `moduleNameMapper` | `resolve.alias` | |
| 43 | +| `collectCoverage` | `coverage.enabled` | |
| 44 | +| `coverageDirectory` | `coverage.directory` | |
| 45 | +| `coverageProvider` | `coverage.provider` | |
| 46 | +| `coveragePathIgnorePatterns` | `coverage.exclude` | |
| 47 | + |
| 48 | +For more details, please refer to the [Configuration](/config) section. |
| 49 | + |
| 50 | +### Inject globals |
| 51 | + |
| 52 | +Rstest does not mount the test APIs (e.g., `describe`, `expect`, `it`, `test`) to the global object by default, which is different from Jest. |
| 53 | + |
| 54 | +If you want to continue using the global test APIs, you can enable the `globals` option in your `rstest.config.ts` file: |
| 55 | + |
| 56 | +```ts title='rstest.config.ts' |
| 57 | +import { defineConfig } from '@rstest/core'; |
| 58 | + |
| 59 | +export default defineConfig({ |
| 60 | + globals: true, |
| 61 | +}); |
| 62 | +``` |
| 63 | + |
| 64 | +### Code transformation |
| 65 | + |
| 66 | +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. |
| 67 | + |
| 68 | +However, if you have custom Babel configurations or use specific Babel plugins/presets, you can add [Rsbuild's Babel Plugin](https://rsbuild.rs/plugins/list/plugin-babel): |
| 69 | + |
| 70 | +```ts title='rstest.config.ts' |
| 71 | +import { pluginBabel } from '@rsbuild/plugin-babel'; |
| 72 | +import { defineConfig } from '@rstest/core'; |
| 73 | + |
| 74 | +export default defineConfig({ |
| 75 | + plugins: [pluginBabel()], |
| 76 | +}); |
| 77 | +``` |
| 78 | + |
| 79 | +## Update test API |
| 80 | + |
| 81 | +Your existing Jest test files should work with minimal changes since Rstest provides Jest-compatible APIs. Simply update your imports from Jest to Rstest: |
| 82 | + |
| 83 | +```diff |
| 84 | +- import { describe, expect, it, test } from '@jest/globals'; |
| 85 | ++ import { describe, expect, it, test } from '@rstest/core'; |
| 86 | +``` |
| 87 | + |
| 88 | +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](/api/). |
| 89 | + |
| 90 | +```diff |
| 91 | +- const fn = jest.fn(); |
| 92 | ++ const fn = rstest.fn(); |
| 93 | + |
| 94 | +fn.mockResolvedValue('foo'); |
| 95 | +``` |
0 commit comments