How to test custom global in jest

Viewed 77

I declare a new global variable in global.d.ts:

declare const foo: {
  env: Record<string, string>
}

Then I define it using webpack.Defineplugin:

new webpack.DefinePlugin({
      "foo.env": {
        NAME: JSON.stringify("loc"),
        VERSION: JSON.stringify(project.version)
      }
    }),

Lastly, I have a function that can pull these env variables so I can put them into a config object:

export function getEnvVar(name: string): string | never {
  const value: unknown = foo.env[name]

  if (value) {
    return String(value)
  } else {
    throw `Could not find env var ${name}`
  }
}

No I want to test getEnvVar using jest, but whenever I try to define the foo object in my test, it's unable to find it.

Any help would be greatly appreciated

0 Answers
Related