question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Vite compatible Jest preset

See original GitHub issue

Is 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:closed
  • Created 3 years ago
  • Reactions:12
  • Comments:17 (6 by maintainers)

github_iconTop GitHub Comments

46reactions
padcomcommented, Jan 12, 2021

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.

25reactions
yyx990803commented, Jan 8, 2021

On another thought, there could probably be an official Jest preset to make Jest vite-compatible.

Read more comments on GitHub >

github_iconTop Results From Across the Web

vite-jest - npm
A Jest transformer that enables first-class Vite integration. Latest version: 0.1.4, last published: a year ago. Start using vite-jest in ...
Read more >
Adding Jest with TypeScript Support to a Vite Application
Starting from the bottom, we have Babel preset typescript to allow Jest to read our typescript files. Babel preset React for our JSX....
Read more >
Getting Started - Jest
Jest supports TypeScript, via Babel. First, make sure you followed the instructions on using Babel above. Next, install the @babel/preset- ...
Read more >
Setup Jest with Vite (featuring SWC) - Blog by hung.dev
Integrating Jest to Vite · First, generate React Typescript project using Vite. · Then, install the main dependency jest : · Install react-testing- ......
Read more >
Add testing to Vite - DEV Community ‍ ‍
Making Vite, the brand new incredibly fast development server for Vue 3, ... By default, Jest doesn't understand Vue and Typescript files, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Hashnode Post

No results found