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

223
Views
How do I mock `.env` variables in Jest?

I am trying to mock something that imports @env so that I can test different combinations of the values. However, I don't want to change the existing method which reads from the @env to pass in variables.

// env.d.ts
declare module '@env' {
  export const APP_ROUTE_NAME: string;
}

Here's my attempt

import env from '@env';
jest.mock('@env');
describe("Determine initial routes", ()=> {
  it("defaults",()=>{
    // does not work because it is read only
    (env as jest.Mocked<typeof env>).APP_ROUTE_NAME = null
  })
})
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If you are testing you probably don't need to care about fake secrets being included in your code. So just set the environment variables manually. You could put this is the the a global setup file:

const isDev = process.env.NODE_ENV !== 'production'

module.exports = async () => {
  if (isDev) {
    process.env.API_KEY = 'xxx'
    process.env.OTHER_STUFF = 'xxx'
  }
}
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!