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
})
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?