I have a ReactJs project where also i use jest for testing. To display the time according UK timezone, i created this function:
const getDateTZ = dt => {
const d = new Date(dt)
const o = {
hour: '2-digit',
minute: '2-digit',
timeZone: 'Europe/London'
}
return d.toLocaleTimeString('en-GB', o)
}
console.log(getDateTZ(new Date()))
This function work fine and running it i get the right time. To test this function with jest i add this:
test('It should return time', () => {
const today = new Date()
today.setHours(11)
today.setMinutes(30)
const r = getDateTZ(today)
expect(r).toEqual('09:30')
})
"test": "TZ=UTC jest the test above fails and output this when i do npm run test:
Expected: "09:30"
Received: "11:30"
So when i run all the tests my test fails because i receive 11:30 instead of 09:30 even if the test works if i run it separately in the test file.
Question: Why i get this issue and how to get rid of it?