Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

226
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda