Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

225
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda