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

326
Views
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
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!