Jest: Date.now() does not work properly

Created on 6 Aug 2015  Â·  7Comments  Â·  Source: facebook/jest

Try this code. It will prints same datetime every time. Version 0.4.17.

describe('Date.now()', () => {
  it('should print different timestamp', () => {
    let i = 0
    let interval = setInterval(() => {
      if(i > 5) {
        clearInterval(interval)
      }

      console.log(Date.now())
      i++
    }, 1000)

    jest.runAllTimers()
  })
})

It this expected behaviour?

Most helpful comment

Yes, you need to mock Date.now with your expected return value, like this: Date.now = jest.genMockFunction().mockReturnValue(0);

All 7 comments

Yes, you need to mock Date.now with your expected return value, like this: Date.now = jest.genMockFunction().mockReturnValue(0);

@cpojer Just an additional question, will be Date.now reseted for all other tests?

Yes.

@cpojer I'm pretty sure this does not reset Date.now for the next test. If we replace Date.now like this it will bleed into tests that follow. We have the following test:

it('stubs date', () => {
  Date.now = jest.genMockFunction().mockReturnValue(0);
  console.log(Date.now);
  jest.resetAllMocks();
});

it('does not stub date', () => {
  console.log(Date.now);
});

And the output we see is:

$ npm run jest -s spec/js/blah_spec.js 
 PASS  spec/js/blah_spec.js
  ✓ stubs date (4ms)
  ✓ does not stub date (1ms)

Test Suites: 1 passed, 1 total
Tests:       2 passed, 2 total
Snapshots:   0 total
Time:        1.117s
Ran all test suites matching "spec/js/blah_spec.js".
  console.log spec/js/blah_spec.js:3
    { [Function: mockConstructor]
      _isMockFunction: true,
      getMockImplementation: [Function],
      mock: [Getter/Setter],
      mockClear: [Function],
      mockReset: [Function],
      mockReturnValueOnce: [Function],
      mockReturnValue: [Function],
      mockImplementationOnce: [Function],
      mockImpl: [Function],
      mockImplementation: [Function],
      mockReturnThis: [Function] }

  console.log spec/js/blah_spec.js:7
    { [Function: mockConstructor]
      _isMockFunction: true,
      getMockImplementation: [Function],
      mock: [Getter/Setter],
      mockClear: [Function],
      mockReset: [Function],
      mockReturnValueOnce: [Function],
      mockReturnValue: [Function],
      mockImplementationOnce: [Function],
      mockImpl: [Function],
      mockImplementation: [Function],
      mockReturnThis: [Function] }

@mockdeep you forgot to restore now method

  it('stubs date', () => {
    const _now = Date.now;  // save
    // looks like this api is deprecated, but it works too
    // Date.now = jest.genMockFunction().mockReturnValue(0);
    Date.now = jest.fn(() => 0);
    try {
      expect(Date.now()).toEqual(0);
    } finally {
      Date.now = _now;  // restore original now meth. on object
    }
  });

  it('does not stub date', () => {
    expect(Date.now()).not.toEqual(0);
  });

@akaRem I didn't forget, I was responding to @cpojer's comment above. It does not happen automatically, so like you say, you have to manually restore the stub. It would be nice if Jest had a way to stub a property and automatically restore it.

@mockdeep ah, ok, sorry.

Was this page helpful?
0 / 5 - 0 ratings