Skip to content
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

Usage with import.meta.env #60

Open
harrygr opened this issue Nov 9, 2021 · 1 comment
Open

Usage with import.meta.env #60

harrygr opened this issue Nov 9, 2021 · 1 comment

Comments

@harrygr
Copy link

harrygr commented Nov 9, 2021

I'm using this package in my Jest setup in a Vite project.

With es modules env variables are accessed through import.meta.env. When I run my Jest tests, any that import a module that involves accessing an env variable results in the following error:

TypeError: Cannot read property 'VITE_MY_ENV_VAR' of undefined

Suggesting that import.meta.env is undefined; although import.meta looks to be defined or the error would say so.

Any idea how to solve this?

@Joshuabaker2
Copy link

For any future people coming here, I solved this by grabbing the changes at #56, patching my local version of esbuild-jest to match, and then doing:

"jest": {
    "transform": {
      "^.+\\.(js|ts)x?$": [
        "esbuild-jest",
        {
          "define": {
            "import.meta.env": "{}"
          }
        }
      ]
    }
}

In my package.json

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants