Mock toLocaleString in Jest

2 minutes read
tech javascript

We had to use toLocaleString with a specific country-code. toLocaleString('de'). This works perfectly in all the browsers. However, not in jest tests.

Our Jest tests were running with --env=jsdom I got to know that jsdom and phantomJS aren't supporting multiple locale implementations.

PhantomJS support locale-specific.

So, the only solution I found is to mock these methods and test rest of the logic. Here is a sample mock behaviour.

import * as helpers from "../src/helpers";
describe("formatDate", () => {
  it("should invoke localString implementation to format date ", () => {
    const localStringMock = jest.fn();
    const mockDate = { toLocaleString: localStringMock };
    helpers.formatDate(mockDate);
    expect(localStringMock).toHaveBeenCalledWith("de-DE", {
      year: "numeric",
      month: "2-digit",
      day: "2-digit",
      hour: "2-digit",
      minute: "2-digit",
    });
  });
});

Note: This behaviour is applicable for toLocaleDateString() toLocaleTimeString()

profile-image

Prasanna is an AI/ML application engineer with a background in full-stack web development and extensive experience in architecting enterprise applications.