Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

360
Views
Jest test fails when using window object environment variable?

My tests were running fine first, I am now using environmental variables based on the window object like:

{window._env_.REACT_APP_URL}

But when I run my test: it shows as TypeError: Cannot read property 'REACT_APP_URL' of undefined

Do I need to do something different in my test files to accept this type of variable, when I change to {process.env.REACT_APP_URL} everything works as expected?

Any idea's?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The _env_ property does not exist on the window object out of the box, hence TypeScript is complaining that you are assigning a value to an undefined property.

Best way to solve your issue is to reassign the window variable to a custom one that combines the current window types with your own custom EnvironmentVariables interface and use that in your tests instead:

interface EnvironmentVariables {
  '_env_': {
    'REACT_APP_URL': string
  }
}

const customWindow = window as Window & typeof globalThis & EnvironmentVariables;

// set the _env_ object with values
customWindow._env_ = {
  'REACT_APP_URL': 'url here'
}

// access REACT_APP_URL
customWindow._env_.REACT_APP_URL
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!