Skip to content

Commit dd272db

Browse files
lgandeckibrapifra
andauthored
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 <brapifra@gmail.com>
1 parent a6f0b72 commit dd272db

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

src/helpers.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/* eslint-disable import/prefer-default-export */
2+
/* eslint-env jest */
3+
// Used to avoid using Jest's fake timers and Date.now mocks
4+
// See https://github.com/TheBrainFamily/wait-for-expect/issues/4 and
5+
// https://github.com/TheBrainFamily/wait-for-expect/issues/12 for more info
6+
const globalObj = typeof window === "undefined" ? global : window;
7+
8+
// Currently this fn only supports jest timers, but it could support other test runners in the future.
9+
function runWithRealTimers(callback: () => any) {
10+
const usingJestFakeTimers =
11+
// eslint-disable-next-line no-underscore-dangle
12+
(globalObj.setTimeout as any)._isMockFunction &&
13+
typeof jest !== "undefined";
14+
15+
if (usingJestFakeTimers) {
16+
jest.useRealTimers();
17+
}
18+
19+
const callbackReturnValue = callback();
20+
21+
if (usingJestFakeTimers) {
22+
jest.useFakeTimers();
23+
}
24+
25+
return callbackReturnValue;
26+
}
27+
28+
export function getSetTimeoutFn() {
29+
return runWithRealTimers(() => globalObj.setTimeout);
30+
}

src/index.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { getSetTimeoutFn } from "./helpers";
2+
13
const defaults = {
24
timeout: 4500,
35
interval: 50
@@ -16,10 +18,7 @@ const waitForExpect = function waitForExpect(
1618
timeout = defaults.timeout,
1719
interval = defaults.interval
1820
) {
19-
// Used to avoid using Jest's fake timers and Date.now mocks
20-
// See https://github.com/TheBrainFamily/wait-for-expect/issues/4 and
21-
// https://github.com/TheBrainFamily/wait-for-expect/issues/12 for more info
22-
const { setTimeout } = typeof window !== "undefined" ? window : global;
21+
const setTimeout = getSetTimeoutFn();
2322

2423
// eslint-disable-next-line no-param-reassign
2524
if (interval < 1) interval = 1;

src/withFakeTimers.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ beforeEach(() => {
1212
jest.useRealTimers();
1313
});
1414

15-
test("it works with real timers even if they were set to fake before importing the module", async () => {
15+
test("it always uses real timers even if they were set to fake before importing the module", async () => {
1616
jest.useFakeTimers();
1717
/* eslint-disable global-require */
1818
const importedWaitForExpect = require("./index");
@@ -27,6 +27,8 @@ test("it works with real timers even if they were set to fake before importing t
2727
numberToChange = 100;
2828
}, randomTimeout);
2929

30+
jest.useFakeTimers();
31+
3032
await importedWaitForExpect(() => {
3133
expect(numberToChange).toEqual(100);
3234
});

0 commit comments

Comments
 (0)