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

324
Visualizações
How to set timezone in jest tests

In my application i use a specific timezone (UK). To run the tests i use "test": "TZ=UTC jest. In my code i created some function that uses Intl api.

const Date = (d, opt = {}) => {

  return new Intl.DateTimeFormat(
    'en-GB',
    {
      ...opt,
      timeZone: 'Europe/London'
    }
  ).format(new Date(d))
}

The issue is next: when i run the test with npm run test the results are different, if i run a single test locally. For example if a run a separate test in my react application and i expect the next date 08:00 the code works, but if i run the code using npm run test the code does not give the expected value, instead gives 10:00. The issue appears when i set do this in my test:

test('It should return time', () => {
  const n = new Date()
  n.setHours(10)
  n.setMinutes(00)

  const r = getD(n)
  expect(r).toEqual('10:30') // expect 08:00
})
I understand that the results are different because when i run the tests using npm run test it uses UTC, and when i run them locally the tests uses the correct date. But how to avoid this issue and to get every time the correct time?
Note: UK time is behind with 2 hours in my situation.
PS: i don't use momentjs

about 4 years ago · Juan Pablo Isaza
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