Jest: Mocking Date.now() doesn't work on CircleCI

Created on 21 Apr 2017  路  7Comments  路  Source: facebook/jest

Hello Jesters,

I'm having a problem on CircleCI while running my tests. I'm mocking "Date.now" to have the same date all the time from moment.js. It works perfectly on my local machine but the test fail on CircleCI. I'm mocking Date.now because my code is highly time dependant. Every test passes expect the ones using moment.js.

This is the code I use (Flow says it's alright) :

global.Date.now = jest.fn(() => 1490760656000);

I also tried using timekeeper : https://github.com/vesln/timekeeper

I thought of mocking moment but it seems like a lot of work and it will make my tests and data mocks really complicated to understand.

I saw we can set a timezone in the CircleCI config but it's not enough has I want a specific date during the execution of the tests. Maybe there is a way but I couldn't find it unfortunately.

I also looked for Jest fake timer system but it's only for timers not dates.

I use the latest version of Jest and run CircleCI on Node 6.10.
I run the test with "--runInBand".

Any idea about this ?

Needs More Info

Most helpful comment

OK finally found the bug! Jest was actually working and mocking Date.now() correctly but I'm on Australian timezone when I'm running my tests locally and the CI servers are on UTC. As my tests had snapshots, the code is time dependant and uses 'moment.js'. I suppose there is a timezone handling system in the moment.js code that checks what your timezone is. This means that my code was working well and the mock as well but the snapshot where different because the time they received was different. I managed to fix it by adding a timezone setting in my CI file. I should have tried it before but really I wasn't expecting something like that. Feel free to close the issue =D

All 7 comments

Is it happening only on Circle? What about other CIs?

@thymikee I just tried on Codeship it's the same problem

If you have some time, could you please setup a repo with a simple test case like this:

test('date', () => {
  Date.now = jest.fn(() => 1490760656000);
  const date1 = Date.now();
  const date2 = Date.now();
  expect(Date.now).toHaveBeenCalled();
  expect(date1).toEqual(date2);
});

and add Circle and Travis running this code on different Node versions?

Sure no problem do you want something like that https://github.com/chrishiste/jest-ci-testing ?
I've never used Travis before but give me a couple minutes I'll add it.

OK I tested, the test passed on Travis with node 5, 6, 7 and on Circle with node 6.10. Pretty weird..

OK finally found the bug! Jest was actually working and mocking Date.now() correctly but I'm on Australian timezone when I'm running my tests locally and the CI servers are on UTC. As my tests had snapshots, the code is time dependant and uses 'moment.js'. I suppose there is a timezone handling system in the moment.js code that checks what your timezone is. This means that my code was working well and the mock as well but the snapshot where different because the time they received was different. I managed to fix it by adding a timezone setting in my CI file. I should have tried it before but really I wasn't expecting something like that. Feel free to close the issue =D

Awesome, glad you figured out what's wrong :)

Was this page helpful?
0 / 5 - 0 ratings