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

325
Vistas
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 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