最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Mock a date in jest not working in different environment - Stack Overflow

programmeradmin1浏览0评论

So I trying to Mock a date in my test, this is what I did :

const mockDate = new Date('2018-01-01');
const backupDate = Date;

beforeEach(() => {
  (global.Date as any) = jest.fn(() => mockDate);
})

afterEach(() => {
  (global.Date as any) = backupDate;
  jest.clearAllMocks();
});



const backupDate = Date;
(global.Date as any) = jest.fn(() => mockDate);
expect(myModule).toMatchSnapshot();
(global.Date as any) = jest.fn(() => backupDate);

So this test in my local works fine and it's match with my snapshots :

exports[`should match with date`] = `
[MockFunction] {
  "calls": Array [
    Array [
      Object {
           "myDate" : "Mon Jan 01 2018 01:00:00 GMT+0100 (Central European Standard Time)"
}]]}

but in production environment I getting this instead which cause failing the test : Mon Jan 01 2018 01:00:00 GMT+0100 (CET)

Any idea what is wrong?

So I trying to Mock a date in my test, this is what I did :

const mockDate = new Date('2018-01-01');
const backupDate = Date;

beforeEach(() => {
  (global.Date as any) = jest.fn(() => mockDate);
})

afterEach(() => {
  (global.Date as any) = backupDate;
  jest.clearAllMocks();
});



const backupDate = Date;
(global.Date as any) = jest.fn(() => mockDate);
expect(myModule).toMatchSnapshot();
(global.Date as any) = jest.fn(() => backupDate);

So this test in my local works fine and it's match with my snapshots :

exports[`should match with date`] = `
[MockFunction] {
  "calls": Array [
    Array [
      Object {
           "myDate" : "Mon Jan 01 2018 01:00:00 GMT+0100 (Central European Standard Time)"
}]]}

but in production environment I getting this instead which cause failing the test : Mon Jan 01 2018 01:00:00 GMT+0100 (CET)

Any idea what is wrong?

Share Improve this question edited Nov 29, 2018 at 22:40 skyboyer 23.8k7 gold badges62 silver badges71 bronze badges asked Nov 29, 2018 at 17:06 Emad DehnaviEmad Dehnavi 3,4515 gold badges23 silver badges47 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

You should use jest.spyOn works for locking time:

let dateNowSpy;

beforeAll(() => {
    // Lock Time
    dateNowSpy = jest.spyOn(Date, 'now').mockImplementation(() => 1487076708000);
});

afterAll(() => {
    // Unlock Time
    dateNowSpy.mockRestore();
});

For Date & time test on Jest, I wrote a module named jest-date-mock which will make Date & Time test simply and controllable.

import { advanceBy, advanceTo, clear } from 'jest-date-mock';

test('usage', () => {
  advanceTo(new Date(2018, 5, 27, 0, 0, 0)); // reset to date time.

  const now = Date.now();

  advanceBy(3000); // advance time 3 seconds
  expect(+new Date() - now).toBe(3000);

  advanceBy(-1000); // advance time -1 second
  expect(+new Date() - now).toBe(2000);

  clear();
  Date.now(); // will got current timestamp
}); 
发布评论

评论列表(0)

  1. 暂无评论