Vite compatible Jest preset
See original GitHub issueIs your feature request related to a problem? Please describe.
I’m migrating a large codebase to Vue 3 + Vite. The project contains tests I’d like to reuse. But due to the limited ESM support Jest throws an error every time import.meta.env
is used in components, vuex actions, api helpers. Apart from that experience is pretty smooth.
For example (in tests):
timeout: import.meta.env.VITE_API_CONFIG_TIMEOUT
^^^^
SyntaxError: Cannot use 'import.meta' outside a module
Describe the solution you’d like
I’d like to use process.env
until ESM support in Jest is complete. Allowing environment variables with VITE_
prefix. As far as I can see in config.ts, the choice is made in favour of import.meta
. Would you consider adding a config flag that will allow env customization?
Describe alternatives you’ve considered
Snowpack, because similar issue has already been addressed https://github.com/snowpackjs/create-snowpack-app/issues/120
jest.config.js
module.exports = {
coverageDirectory: 'coverage',
moduleFileExtensions: ['vue', 'js', 'json'],
moduleNameMapper: {
'^/@/(.*)$': '<rootDir>/src/$1',
},
transform: {
'^.+\\.vue$': 'vue-jest',
'^.+\\js$': 'babel-jest',
},
}
babel.config.js
module.exports = {
env: {
test: {
presets: [
[
'@babel/preset-env',
{
targets: {
node: 'current',
},
},
],
],
},
},
}
P.S. I’m happy to provide a reproduction if needed
Issue Analytics
- State:
- Created 3 years ago
- Reactions:12
- Comments:17 (6 by maintainers)
Top GitHub Comments
Honestly, the only thing stopping me from widely adopting Vite as the build tool is the lack of templates with unit tests and end to end tests. I’ve been trying to get something done myself but apparently I don’t know the area good enough to accomplish that. I suppose I am not alone in it.
Having the option to scaffold a project with unit tests using either mocha+chai or jest and e2e tests with cypress or some selenium-based library would make it far easier to make Vite the choice for a new project.
On another thought, there could probably be an official Jest preset to make Jest vite-compatible.