Skip to content

Instantly share code, notes, and snippets.

@matthewblewitt
Last active July 23, 2020 14:36
Show Gist options
  • Save matthewblewitt/07c23749945d7f9f209714e040e4d644 to your computer and use it in GitHub Desktop.
Save matthewblewitt/07c23749945d7f9f209714e040e4d644 to your computer and use it in GitHub Desktop.
Jest mock cheet sheet

dates

describe("mock global date", ()=> {
  beforeEach(async () => {
    global.Date.now = jest.fn(() => new Date("2020-01-01T12:00:00Z").getTime());
  });
  afterAll(() => {
    global.Date.now = RealDate;
  });
});
describe("mock date methods", ()=> {
  jest
    .spyOn(Date.prototype, "toISOString")
    .mockReturnValue("2020-01-01T12:00:00Z");
});

localStorage

jest.spyOn(Storage.prototype, "getItem").mockReturnValue("getItem");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment