Skip to content

Vite compatible Jest preset #1149

@softbeehive

Description

@softbeehive

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
FredKSchott/create-snowpack-app#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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions