-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
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
feature: first class Jest integration #1955
Comments
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. 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. 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 jestjs/jest#9504 it seems there isn't much progress on this front :/ |
Yeah this has been running around my mind as well. One idea I had, because it's all so fast, is to just do a build (without minification and concatenation) and have jest operate on that. 🤔 |
The PR for jest's async transform support jestjs/jest#9889 |
Ok I think I have ideas for hackzz. I'm going to try some stuff out, as and when I have time in the evenings. Will get back to this thread this weekend. |
@threepointone I've been playing with this concept using That repo is a repro for a leak issue so it is stripped of anything non-essential, but the same pattern could be used in jest/ava/uvu. |
Just for anyone looking for something for right now, we wrote a (probably naive) Babel plugin that handles We have discussed extracting this into a stand alone package and publishing it if there is any interest in others using it. |
Was needed for enabling our Orgs OSS contributing to a Vite project since a majority of our group is familiar with Jest it seemed worth the extra effort! It is now successfully running Jest + React Testing Library, locally and in GH actions |
We ended up cleaning it up a bit and publishing it to NPM (source code). |
@yyx990803 async code transformations are implemented by jestjs/jest#11191 and it was released in jest 27.0.0-next.5 |
I'm trying to write a vite jest preset recently. Now I got a blocker, that I need jest to allow async resolver to allow vite to do module resolving. see jestjs/jest#11226 (comment) and jestjs/jest#9505 |
FYI I've just implemented a super hacky vite-jest transformer with ViteDevServer. The implementation A working example: 2 main hacks used in the implementation:
And I have to patch Vite (https://github.com/sodatea/vite-jest/blob/main/examples/vue-app-type-module/patch-vite.js) to rename |
For those of us unfamiliar with Vite, why is it necessary to spin up the |
Yeah, it's for simplicity and consistency. Setting up Jest using But like Evan has said earlier, to support other custom transformations, such as That's a lot of overcomplication. If we can spin up a |
What's the status of this? Any updates? Vite is looking really great so far but the lack of first-class jest support is the only thing that's giving us pause on adopting it. I am interested in helping but I need a little bit of direction |
@moljac024 Blocked by this issue: jestjs/jest#9505 |
Is there anything I can do to push this forward? |
Woohoo: Jest 27 shipped and includes async transforms! |
Does this mean this can move forward? |
Is anyone working on a fix that we can follow? |
sodatea commented in Vite Land that jestjs/jest#9505 is still unresolved |
I don't really know what I'm doing, but I really want jest support, so hold my beer: jestjs/jest#11540. (It's just a start, and has a long way to go before being done, but at least it's a start?) |
@sodatea, after jest supports async resolvers, there will still be a few other issues that you mentioned in #1955 (comment), closing the server after tests (which seems fine to do in a reporter, I think), and renaming vite files from |
Continuing #1955 (comment)
Well I think at this point it's time to move this code to the package and setup tests |
I tried my solution on react-ts and found that it is far from working state.
need to figure out how to deal with it |
Just want to provide the jest config I'm using for people don't know how to run This way do not support
|
Was porting our react snowpack project to vitejs, this was the biggest pain to get done, mainly because all react+ts examples are ignoring issue with import.meta. Which is really annoying In snowpack this was solved, with create-snowpack-app which mimics cra setup. They use jest.config from package @snowpack/app-scripts-react . there is nothing specific to snowpack there, so it can be easily used for vitejs or any other esbuild build tooling.
and tests are running, love the project btw :). |
Is there any roadmap on implementing this feature? Right now, it's preventing a lot of people adopting or migrating to Vite. This is the case for React and TS, Vue, etc. The |
@lloydjatkinson as I understand it, jestjs/jest#9505 is one blocker on a more seamless vite integration with jest. I've got a PR opened in jest to add async resolver support, and the jest maintainers have said they plan to merge it, but it's been slow going so far. |
https://twitter.com/haoqunjiang/status/1468053053651632128 So as of I need to find a Windows machine to test and fix the file path issue later. Just make sure you have understood the inherent limitation of this approach before trying it out: https://github.com/sodatea/vite-jest/tree/main/packages/vite-jest#limitations-and-differences-with-commonjs-tests |
For a quick fix for |
Regarding
e.g.
|
While we wait for movement from jest to provide better support for projects like vite, I've created an example repo which shows how to use some of the best parts of jest (like its This approach has been working well for me so far, with 143 tests (including many react component tests) across 46 test suites running in three headless browsers (chromium, firefox, and safari) in ~35 seconds. |
In the meantime, you might want to keep track of https://vitest.dev/ |
For those following this thread, https://github.com/vitest-dev/vitest just opened. |
I think Vitest solves this issue? |
not really, but it is an interesting alternative indeed :) |
Maybe I can share my experience blending vite with jest. I hope everyone can find it helpful or at least insightful. The only thing that worked for me was vite-jest, which is still a work in progress., as mentioned by @sodatea. But the approach works on macOS. Here's how to configure your project to run the package.json {
// your configuration...
"type": "module",
"scripts": {
// your scripts...
"test:unit": "vite-jest --no-cache",
},
"devDependencies": {
// your dev dependencies...
"@vue/test-utils": "^2.0.0-rc.18",
"jest": "^27.4.7",
"jest-environment-jsdom": "^27.4.6",
"vite-jest": "^0.1.4",
}
}
jest.config.js export default {
preset: 'vite-jest',
testMatch: [
'**/tests/unit/**/*.spec.?(m)js?(x)',
'**/__tests__/*.?(m)js?(x)',
],
testEnvironment: 'jest-environment-jsdom',
} vite.config.js import { fileURLToPath } from 'url'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [vue()],
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url)),
},
},
}) One thing to notice, and pretty important, is that your app will work on I hope it helps those who want to work with |
I might be able to share some of my experiences with Vite + React + Jest. At the moment there is no perfect built-in solution indeed. And I'm hesitate to use a tool that is still considered WIP for our production project (e.g. So what I did is basically write a jest config from scratch with a minimum 3rd party helper, in my case, this is the good old The complete config file is straightforward. module.exports = {
preset: 'ts-jest',
testEnvironment: 'jsdom',
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
moduleNameMapper: {
'^~/(.*)$': '<rootDir>/src/$1',
'\\.(css|less|scss|sass)$': 'identity-obj-proxy',
'\\.svg$': '<rootDir>/__mocks__/svg-mock.js',
},
transform: {
'^.+\\.(t|j)sx?$': ['ts-jest'],
},
setupFilesAfterEnv: ['./src/setupTests.ts'],
}; To solve the stylesheet importing issue, add module mapper and proxy them to
To solve SVG importing issue, add a mock file mapper
then in the mapper do whatever fits your SVG transformation rules. In my case I love the
To solve the customized tsconfig
To resolve I put all
Then in Jest I just mock the config file via
|
EDIT: I quoted the wrong one, I meant shouldn't |
No, it should be an array. See https://jestjs.io/docs/configuration#setupfilesafterenv-array |
Sorry I commented on the wrong one, I meant
|
I think we can close this issue now that vite-jest is available. Check the last comment from @sodatea here. I don't see further steps to be taken in Vite Core. Please create more focused issues in the proper repository (vite, jest, or vite-jest) moving forward. As a note, Vitest is also now available as an alternative to Jest. |
I didn’t like the idea of having to spin up the vite server for testing. I’ve currently got this working with es-build so I guess I’ll just post my config for posterity’s sake.
The mock for |
(previously...)
Making an issue to track work on the
Jest
experience withvite
.Jest
is a very popular JavaScript testing framework. Some standout features: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.)
esbuild
to make jest tests startup and run faster (prior art:esbuild-jest
,esbuild-jest-transform
).vite
already uses for newer syntax (for example, support forimport.meta
viababel-plugin-transform-import-meta
) untiljest
ships full support for ES modules.jest.mock()
calls hoisted above imports withbabel-plugin-jest-hoist
(explanation) until top-levelawait
lands fully inesbuild
, at which point you could codemod all tests to use tla imports, and remove thebabel
plugin. (This still requires support fromjest
for ES modules, or else it won't detect tests defined after a couple of ticks have passed.)swc
package too, which is very appealing (via vite#788).jest
usesbabel-plugin-istanbul
for coverage. This may never land inesbuild
.@yyx990803 mentioned spinning up a
ViteDevServer
and usingvite.transformRequest()
as the transform. One problem here is that thejest
transform is expected to be synchronous.Jest
's likely going to fix this as part of the ES module work, tracking here. (Further, it's not clear to me what the devx would be? Should we run a vite server in a seaparate process/terminal tab? Or should we introduce avite test
command that wrapsjest
?Let me propose a first step: in my own repo I'm going to do the bare minimum:
esbuild-jest
(possibly modified) +babel-plugin-transform-import-meta
+babel-plugin-jest-hoist
+babel-plugin-istanbul
. Anything I'm missing? What are thevite
specific bits to inject into the environment? Discuss!The text was updated successfully, but these errors were encountered: