[READ-ONLY] Mirror of https://github.com/vitest-dev/vitest. Next generation testing framework powered by Vite. vitest.dev
test testing-tools vite
12

Configure Feed

Select the types of activity you want to include in your feed.

docs(migration): use headers instead of bold text (#3927)

authored by

Kanad Gupta and committed by
GitHub
(Aug 14, 2023, 3:42 PM +0200) 867dbf60 351399f2

+10 -10
+10 -10
docs/guide/migration.md
··· 8 8 9 9 Vitest has been designed with a Jest compatible API, in order to make the migration from Jest as simple as possible. Despite those efforts, you may still run into the following differences: 10 10 11 - **Globals as a Default** 11 + ### Globals as a Default 12 12 13 13 Jest has their [globals API](https://jestjs.io/docs/api) enabled by default. Vitest does not. You can either enable globals via [the `globals` configuration setting](/config/#globals) or update your code to use imports from the `vitest` module instead. 14 14 15 15 If you decide to keep globals disabled, be aware that common libraries like [`testing-library`](https://testing-library.com/) will not run auto DOM [cleanup](https://testing-library.com/docs/svelte-testing-library/api/#cleanup). 16 16 17 - **Module mocks** 17 + ### Module mocks 18 18 19 19 When mocking a module in Jest, the factory argument's return value is the default export. In Vitest, the factory argument has to return an object with each export explicitly defined. For example, the following `jest.mock` would have to be updated as follows: 20 20 ··· 27 27 28 28 For more details please refer to the [`vi.mock` api section](/api/vi#vi-mock). 29 29 30 - **Auto-Mocking Behaviour** 30 + ### Auto-Mocking Behaviour 31 31 32 32 Unlike Jest, mocked modules in `<root>/__mocks__` are not loaded unless `vi.mock()` is called. If you need them to be mocked in every test, like in Jest, you can mock them inside [`setupFiles`](/config/#setupfiles). 33 33 34 - **Importing the original of a mocked package** 34 + ### Importing the original of a mocked package 35 35 36 36 If you are only partially mocking a package, you might have previously used Jest's function `requireActual`. In Vitest, you should replace these calls with `vi.importActual`. 37 37 ··· 40 40 + const { cloneDeep } = await vi.importActual('lodash/cloneDeep') 41 41 ``` 42 42 43 - **Envs** 43 + ### Envs 44 44 45 45 Just like Jest, Vitest sets `NODE_ENV` to `test`, if it wasn't set before. Vitest also has a counterpart for `JEST_WORKER_ID` called `VITEST_POOL_ID` (always less than or equal to `maxThreads`), so if you rely on it, don't forget to rename it. Vitest also exposes `VITEST_WORKER_ID` which is a unique ID of a running worker - this number is not affected by `maxThreads`, and will increase with each created worker. 46 46 47 47 If you want to modify the envs, you will use [replaceProperty API](https://jestjs.io/docs/jest-object#jestreplacepropertyobject-propertykey-value) in Jest, you can use [vi.stubEnv](https://vitest.dev/api/vi.html#vi-stubenv) to do it also in Vitest. 48 48 49 - **Done Callback** 49 + ### Done Callback 50 50 51 51 From Vitest v0.10.0, the callback style of declaring tests is deprecated. You can rewrite them to use `async`/`await` functions, or use Promise to mimic the callback style. 52 52 ··· 59 59 + })) 60 60 ``` 61 61 62 - **Hooks** 62 + ### Hooks 63 63 64 64 `beforeAll`/`beforeEach` hooks may return [teardown function](/api/#setup-and-teardown) in Vitest. Because of that you may need to rewrite your hooks declarations, if they return something other than `undefined` or `null`: 65 65 ··· 68 68 + beforeEach(() => { setActivePinia(createTestingPinia()) }) 69 69 ``` 70 70 71 - **Types** 71 + ### Types 72 72 73 73 Vitest doesn't expose a lot of types on `Vi` namespace, it exists mainly for compatibility with matchers, so you might need to import types directly from `vitest` instead of relying on `Vi` namespace: 74 74 ··· 80 80 81 81 Also, Vitest has `Args` type as a first argument instead of `Returns`, as you can see in diff. 82 82 83 - **Timers** 83 + ### Timers 84 84 85 85 Vitest doesn't support Jest's legacy timers. 86 86 87 - **Vue Snapshots** 87 + ### Vue Snapshots 88 88 89 89 This is not a Jest-specific feature, but if you previously were using Jest with vue-cli preset, you will need to install [`jest-serializer-vue`](https://github.com/eddyerburgh/jest-serializer-vue) package, and use it inside [setupFiles](/config/#setupfiles): 90 90