[SOLVED] How to mock window.navigator.language in jest 27


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

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.


You can mock window.navigator.language using Jest as follows:

let windowSpy: jest.SpyInstance;;

beforeEach(() => {
    // Spy on the read-only window function which
    // returns a reference to the current window.
    windowSpy = jest.spyOn(window, 'window', 'get');

// Clean-up the spy after every test
afterEach(() => windowSpy.mockRestore());

const setMockLanguage = (language: string) =>
    // Set how you want window.navigator.language to behave
    windowSpy.mockImplementation(() => ({
        navigator: {

test('UK date format', () => {


test('US date format', () => {


I’ve included some comments in the code to help.

Answered By – Ben Smith

Answer Checked By – Katrina (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *