feature: first class Jest integration
Problem
(previously...) Making an issue to track work on the `Jest` experience with `vite`. `Jest` is a very popular JavaScript testing framework. Some standout features: - Support for mocking functions and modules - Parallelisation and prioritisation of tests - Snapshot tests - Running only changed tests between 2 revisions (or based on a set of files) It would be worthy to provide some form of blessed integration to use with `vite`. (If you don't need these features, you may be better off using a testing framework like uvu, which should be much simpler to setup.) - Files could be transformed with `esbuild` to make jest tests startup and run faster (prior art: `esbuild-jest`, `esbuild-jest-transform`). - Files could share the same transforms that `vite` already uses for newer syntax (for example, support for `import.meta` via `babel-plugin-transform-import-meta`) until `jest` ships full support for ES modules. - Files could have `jest.mock()` calls hoisted above imports with `babel-plugin-jest-hoist` (explanation) until top-level `await` lands fully in `esbuild`, at which point you could codemod all tests to use tla imports, and remove the `babel` plugin. (This still requires support from `jest` for ES modules, or else it won't detect tests defined after a couple of ticks have passed.) - This exists as an `swc` package too, which is very appealing (via vite#788). - I think `jest` uses `babel-plugin-istanbul` for coverage. This may never land in `esbuild`. @yyx990803
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Solution: feature: first class Jest integration
The main issue I see for not leveraging Vite itself for the transforms is that you won't get Vite plugin compatibility in Jest - e.g. if you add a Vite plugin that does some custom transforms, it won't apply to the Jest files. So this would still require Jest-specific transforms for e.g. Vue or Svelte files. I think your approach would work for J/TSX-only projects but not all Vite projects. Anoth
Trust Score
3 verifications
- 1
The main issue I see for not leveraging Vite itself for the transforms is that y
The main issue I see for not leveraging Vite itself for the transforms is that you won't get Vite plugin compatibility in Jest - e.g. if you add a Vite plugin that does some custom transforms, it won't apply to the Jest files. So this would still require Jest-specific transforms for e.g. Vue or Svelte files. I think your approach would work for J/TSX-only projects but not all Vite projects.
- 2
Another aspect of this is Vite configured alias and defines - they'd be automati
Another aspect of this is Vite configured alias and defines - they'd be automatically covered if the files are transformed by Vite, but you'd have to manually read the config and replicate them in your setup.
- 3
The ideal situation is to let Vite transform the files to how they'd run in Node
The ideal situation is to let Vite transform the files to how they'd run in Node (reusing part of the SSR transform logic), but Jest requiring transforms to be synchronous seems to be a major blocker here. Judging from https://github.com/facebook/jest/issues/9504 it seems there isn't much progress on this front :/
Validation
Resolved in vitejs/vite GitHub issue #1955. Community reactions: 4 upvotes.
Verification Summary
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep