0

I am trying to run Jest unit tests on function logic that is imported from a Svelte component. The program runs fine except when I try to import into Jest: I can console log the env variable in the line below ok.

The problem is that this line gives me an error when i try to run the unit test, I guess because its trying to import the file into the jest test. It runs fine when the program is actually running, but when the Jest test tries to import it, the context changes or ... something. Anyway, here's the line, from my file src/routes/signup/index.svelte:

// in the script tag of a svelte component
<script context="module">
const googleRecaptchaSiteKey =
    typeof import.meta.env.VITE_GOOGLE_RECAPTCHA_KEY === "string"
      ? import.meta.env.VITE_GOOGLE_RECAPTCHA_KEY
      : ""
export function foo() {
    // ...
}
</script>

This code must run in my Jest test because it gives the error when I run npm test, so here's how I am executing that code:

import { foo } from "../src/routes/signup/index.svelte"
// It must execute the whole component when I import from it?
describe("signup page logic", () => {
  test("ensure that the signup form button enablement conditions work properly", () => {
    const failureOne = foo()
  }
}

The error message itself:

    /home/rlm/Code/projName/src/routes/signup/index.svelte:446
    const googleRecaptchaSiteKey = typeof import.meta.env.VITE_GOOGLE_RECAPTCHA_KEY === "string"
                                                 ^^^^

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

    > 1 | import { updateEnabledSubmitSignup } from "../src/routes/signup/index.svelte"

Now since writing the above text, I have been adventuring for approx 27 minutes to discover a solution. What I have done is try to follow guides.

Per the instruction of Environment variables with SvelteKit I did:

in src/lib/variables.ts:

export const variables = {
  foo: import.meta.env.VITE_FOO,
  secondRecaptchaKey: import.meta.env.VITE_SECOND_RECAPTCHA_KEY,
}

And then I import it into the Svelte file: import { variables } from "../../lib/variables"

I run npm run dev and it console logs the value fine.

But then when I run npm test I get:

 src/lib/variables.ts:3:23 - error TS1343: The 'import.meta' meta-property is only allowed when the '--module' option is 'es2020', 'es2022', 'esnext', 'system', 'node12', or 'nodenext'.

    3   secondRecaptchaKey: import.meta.env.VITE_SECOND_RECAPTCHA_KEY,

TS1343: The 'import.meta' meta-property is only allowed when the '--module' option is 'es2020', 'esnext', or 'system'

Test suite failed to run import.meta.env.VITE_* does also but I tried to follow it and it fails even after installing vite-plugin-environment and babel-plugin-transform-import-meta and adding them to the babel plugins:

export const variables = { // logs with all values undefined
  foo: process.env.VITE_FOO,
  secondRecaptchaKey: process.env.VITE_SECOND_RECAPTCHA_KEY,
}

TS1343: The 'import.meta' meta-property is only allowed when the '--module' option is 'es2020', 'esnext', or 'system' also has advice that fails for me, or i have done it wrong. In my current state npm test logs the env variables as undefined.

edit: For anyone in the future who has this problem, I was able to go around the problem by doing this totally-good-enough workaround:

jest.mock("../src/lib/envVariables", () => ({
  envVariables: { foo: "bar", secondRecaptchaKey: "someMockValue" },
}))

I credit nstanard in this post for saving us from the hassle

plutownium
  • 1,220
  • 2
  • 9
  • 23
  • Not really answer, but as you're already using vite, i can highly recommend switching to vitest, it is very similar to jest, but getting it to work requires al lot less config. – Bob Fanger Feb 19 '23 at 01:15

0 Answers0