-
Notifications
You must be signed in to change notification settings - Fork 1
task: #20 Replace Jest with Vitest #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
| es6: true, | ||
| node: true, | ||
| amd: true, | ||
| jest: true, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed Jest related configs
| "@testing-library/jest-dom": "^6.4.2", | ||
| "@testing-library/react": "^15.0.2", | ||
| "@testing-library/user-event": "^14.5.2", | ||
| "@testing-library/jest-dom": "^6.6.3", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Vitest doesn't natively include all the matchers that Jest provides, particularly those related to the DOM like toBeInTheDocument(), toHaveClass, toHaveAttribute and etc. So we are using @testing-library/jest-dom to take advantage of Jest matchers.
| "outDir": "dist", | ||
| "strict": true, | ||
| "target": "esnext", | ||
| "typeRoots": ["./node_modules/@types"], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No requirement for typeRoots here as by default Typescript automatically searches for type definitions inside node_modules/@types, we should only be configuring typeRoots if we want to specify any specific directory.
| }, | ||
| get: vi.fn(), | ||
| } | ||
| }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Jest overwrites axios completely, which works in Jest's ecosystem while Vitest requires preserving the original module and explicitly mocking only the needed parts.
- In Jest,
jest.mock('axios', () => ({ get: jest.fn() }))replaces the entire axios module with an object containing only a get method as a mock. - In Vitest,
vi.mock('axios', async (importOriginal) => { ... })ensures that the original axios module is imported before modifying it.
| redirect: jest.fn(), | ||
| vi.mock('react-router', async () => ({ | ||
| ...(await vi.importActual('react-router')), | ||
| redirect: vi.fn(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Migrating from Jest to Vitest, we need to replace requireActual with importActual.
Ref: https://vitest.dev/guide/migration.html#importing-the-original-of-a-mocked-package
kurtdoherty
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice one 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note: We've documented our TS configs here. No need to follow suit in this PR, but I think this is another thing we could standardise on via a separate repo in future.
Background
For better test performance, Vitest is much faster than Jest, Vitest is built on Vite, which makes it significantly faster than Jest. It uses esbuild (a super-fast bundler) instead of Babel or Webpack for transforming code. It performs parallel test execution and worker threads which improves test performance.
Jest was designed for CommonJS, while Vite and Vitest are ESM first tools.
This PR includes
jest.fn->vi.fn,jest.mock->vi.mockNote:
As part of the changes, I have updated all the test cases in one go, so most changes will be snapshot updates that can be ignored. To ease things up for the reviewer, I have left inline PR comments to facilitate any relevant changes.