Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mocking Date.now() doesn't work on CircleCI #3341

Closed
chrishiste opened this issue Apr 21, 2017 · 8 comments
Closed

Mocking Date.now() doesn't work on CircleCI #3341

chrishiste opened this issue Apr 21, 2017 · 8 comments

Comments

@chrishiste
Copy link

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 ?

@thymikee
Copy link
Collaborator

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

@chrishiste
Copy link
Author

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

@thymikee
Copy link
Collaborator

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?

@chrishiste
Copy link
Author

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.

@chrishiste
Copy link
Author

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

@chrishiste
Copy link
Author

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

@thymikee
Copy link
Collaborator

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

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants