-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add runWithRealTimers & getSetTimeoutFn functions
* Add runWithRealTimers & getSetTimeoutFn functions BREAKING CHANGE: If you are using fake jest timers by default, but upgraded to v2.0 here, you might have to upgrade your tests. Sorry! :( Co-authored-by: Brais Piñeiro <[email protected]>
- Loading branch information
Showing
3 changed files
with
36 additions
and
5 deletions.
There are no files selected for viewing
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,30 @@ | ||
/* eslint-disable import/prefer-default-export */ | ||
/* eslint-env jest */ | ||
// Used to avoid using Jest's fake timers and Date.now mocks | ||
// See https://github.com/TheBrainFamily/wait-for-expect/issues/4 and | ||
// https://github.com/TheBrainFamily/wait-for-expect/issues/12 for more info | ||
const globalObj = typeof window === "undefined" ? global : window; | ||
|
||
// Currently this fn only supports jest timers, but it could support other test runners in the future. | ||
function runWithRealTimers(callback: () => any) { | ||
const usingJestFakeTimers = | ||
// eslint-disable-next-line no-underscore-dangle | ||
(globalObj.setTimeout as any)._isMockFunction && | ||
typeof jest !== "undefined"; | ||
|
||
if (usingJestFakeTimers) { | ||
jest.useRealTimers(); | ||
} | ||
|
||
const callbackReturnValue = callback(); | ||
|
||
if (usingJestFakeTimers) { | ||
jest.useFakeTimers(); | ||
} | ||
|
||
return callbackReturnValue; | ||
} | ||
|
||
export function getSetTimeoutFn() { | ||
return runWithRealTimers(() => globalObj.setTimeout); | ||
} |
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
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