DEV Community

RubyMuibi
RubyMuibi

Posted on

Jest and Vite: Cannot use `import.meta` outside a module

This post describes a fix for a common issue with Jest and using import.meta to load environment variables.

The problem

In react, I kept running into an error when testing with jest. In my case, Jest could not recognize import.meta.env.

The error I got was:

Test suite failed to run

    Jest encountered an unexpected token

    Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.

    Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel
configuration.

    By default "node_modules" folder is ignored by transformers.

    Here's what you can do:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
     • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/configuration
    For information about custom transformations, see:
    https://jestjs.io/docs/code-transformation

    Details:
    SyntaxError: Cannot use 'import.meta' outside a module

Enter fullscreen mode Exit fullscreen mode

The reason

Jest, by default, uses CommonJS module system for handling JavaScript modules. CommonJS doesn't support import.meta because it's a feature introduced in ECMAScript modules (ESM).

Also import.meta.env is a feature provided by Vite for loading environment variables so it is not recognized by default in Jest or Node.js.

Therefore, when Jest encounters import.meta or import.meta.env in the code, it doesn't recognize it and throws an error.

The solution

After much trial and error, here's what worked:

1. Install dependencies

npm install --save-dev babel-jest jest-environment-jsdom @babel/preset-env @babel/preset-react babel-preset-vite
Enter fullscreen mode Exit fullscreen mode

2. Create a babel.config.json file

After installing the above dependencies, we need to configure Babel.

// babel.config.json

{
  "presets": [
    ["@babel/preset-env", { "targets": { "node": "current" } }],

    "@babel/preset-react",

    [
      "babel-preset-vite",
      {
        "env": true,
        "glob": false
      }
    ]
  ]
}
Enter fullscreen mode Exit fullscreen mode

3. Finally, configure your jest.config.js file

In your jest.config.js file, include this

// jest.config.js

export default {
  moduleNameMapper: {
    "^@/(.*)$": "<rootDir>/src/$1",
  },
  testEnvironment: "jest-environment-jsdom",
  transform: {
    "^.+\\.[t|j]sx?$": "babel-jest",
  },
};
Enter fullscreen mode Exit fullscreen mode

Conclusion

Jest throwing an error when import.meta or import.meta.env is used is due to its default use of the CommonJS module system and import.meta.env being a feature of vite. This issue can be resolved by configuring Jest and Babel correctly. With the above steps, we can successfully use import.meta and import.meta.env in our code and Jest tests.

Top comments (3)

Collapse
 
jwbw29 profile image
Justin Byrd

appreciate you!

Collapse
 
kamsiyonna_obi_0ee3d65fe6 profile image
Kamsiyonna Obi

Well done!

Collapse
 
ebitzu profile image
eBitzu

This is the kind of article where you say goodbye to a compiler you don't know with a one you don't want, without knowing it