forked from jestjs/jest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Always recreate mocks in FakeTimers.useFakeTimers() (jestjs#2109)
* Always recreate mocks in FakeTimers.useFakeTimers() If the flag resetMocks or jest.resetAllMocks is used in the test, all the timer tests following that will fail with no way of recovering. jest.resetAllMocks resets everything, including the one created by fakeTimers. There is also no public methods that we can use to reinstantiate a new FakeTimer object to mitigate that. Given both mock and fake timers are part of the jest, they should be working together out-of-the box without extra code gluing them. We change the useFakeTimers method to always create new mocks so it would not be reverted. * Cleanup FakeTimers handling of `process.nextTick`.
- Loading branch information
Showing
6 changed files
with
115 additions
and
69 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
integration_tests/__tests__/timer_after_resetAllMocks-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
* @emails oncall+jsinfra | ||
*/ | ||
'use strict'; | ||
|
||
const runJest = require('../runJest'); | ||
const skipOnWindows = require('skipOnWindows'); | ||
|
||
skipOnWindows.suite(); | ||
|
||
test('run timers after resetAllMocks test', () => { | ||
const result = runJest('timer_after_resetAllMocks'); | ||
expect(result.status).toBe(0); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/** | ||
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
module.exports = () => {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"jest": { | ||
"testEnvironment": "node", | ||
"resetMocks": false | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
integration_tests/timer_after_resetAllMocks/timer_and_mock.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
describe('timers', () => { | ||
it('should work before calling resetAllMocks', () => { | ||
jest.useFakeTimers(); | ||
const f = jest.fn(); | ||
setImmediate(() => f()); | ||
jest.runAllImmediates(); | ||
expect(f.mock.calls.length).toBe(1); | ||
}); | ||
|
||
it('should not break after calling resetAllMocks', () => { | ||
jest.resetAllMocks(); | ||
jest.useFakeTimers(); | ||
const f = jest.fn(); | ||
setImmediate(() => f()); | ||
jest.runAllImmediates(); | ||
expect(f.mock.calls.length).toBe(1); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.