-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
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 ? |
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 :) |
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. |
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) :
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 ?
The text was updated successfully, but these errors were encountered: