Skip to content

Conversation

@ksolanki7
Copy link
Contributor

@ksolanki7 ksolanki7 commented Feb 6, 2025

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

  • Replaces Jest with Vite + Vitest
  • Installed HappyDom (Again performance improvement)
  • As Vitest and Jest have similar APIs, they are not the same, the following changes are made to support Vitest.
    • jest.fn -> vi.fn, jest.mock -> vi.mock
  • Avoid capturing RTL internals in DOM snapshot, for more details reference this PR context

Note:
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.

@ksolanki7 ksolanki7 linked an issue Feb 6, 2025 that may be closed by this pull request
es6: true,
node: true,
amd: true,
jest: true,
Copy link
Contributor Author

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",
Copy link
Contributor Author

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"],
Copy link
Contributor Author

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(),
}
})
Copy link
Contributor Author

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(),
Copy link
Contributor Author

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

@ksolanki7 ksolanki7 requested a review from kurtdoherty February 6, 2025 02:11
Copy link
Contributor

@kurtdoherty kurtdoherty left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice one 👍

Copy link
Contributor

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.

@ksolanki7 ksolanki7 merged commit fede1c8 into master Feb 10, 2025
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Replace Jest with Vitest

3 participants