date.toLocaleString('en-us', { dateStyle: 'long' }) not working in jest test but working in browser

Viewed 660

I don't know why the test not passes for this function,

export function timeStampToFormattedDate (timestamp) {
  if (!timestamp) return 'N/A'
  const date = new Date(timestamp)
  return date.toLocaleString('en-us', { dateStyle: 'long' })
}

Test

describe('timeStampToFormattedDate function', () => {
  it('returns formatted date string from last active user time-stamp', () => {
    const timeStamp = 1592810798024
    const formattedString = timeStampToFormattedDate(timeStamp)
    expect(formattedString).toEqual(`June 22, 2020`)
  })
})

I'm getting this error when I run the test,

timeStampToFormattedDate function › returns formatted date string from last active user time-stamp

expect(received).toEqual(expected) // deep equality

Expected: "June 22, 2020"
Received: "6/22/2020, 10:26:38 AM"

   8 |     const timeStamp = 1592810798024
   9 |     const formattedString = timeStampToFormattedDate(timeStamp)
> 10 |     expect(formattedString).toEqual(`June 22, 2020`)
     |                             ^
  11 |   })
  12 |
  13 |   it('returns "N/A" if last active timestamp is null', () => {

  at Object.toEqual (src/utilities/dateTimeUtil.test.js:10:29)

When I try the function by calling with the same parameter with the test in browser conosle, it's working as expected.

timeStampToFormattedDate(1592810798024)
"June 22, 2020"

So why it's failing in jest?

1 Answers
Related