I've got a function like
export function formatDate(date: string){
return new Intl.DateTimeFormat(navigator.language).format(new Date(date))
}
I'm attempting to write a unit test in vanilla jest (not using jsdom library), but as you can see I need to be able to mock window.navigator.language
.
I've so far tried,
test('UK date format', () => {
Object.defineProperty(window.navigator, 'language', {value: 'en-GB', configurable: true})
expect(window.navigator.language).toBe('en-GB')
})
but I cannot for the life of my understand how you're supposed to mock window.navigator in jest.
Ideally I'd like to be able to mock a new value for window.navigator.language on each test. So I could have a test for en-US
, fr
etc etc
Any help to understand how you're supposed to mock this would be greatly appreciated.