Skip to content

Commit

Permalink
feat: Add runWithRealTimers & getSetTimeoutFn functions
Browse files Browse the repository at this point in the history
* 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
lgandecki and brapifra authored Sep 24, 2019
1 parent a6f0b72 commit dd272db
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
30 changes: 30 additions & 0 deletions src/helpers.ts
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);
}
7 changes: 3 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { getSetTimeoutFn } from "./helpers";

const defaults = {
timeout: 4500,
interval: 50
Expand All @@ -16,10 +18,7 @@ const waitForExpect = function waitForExpect(
timeout = defaults.timeout,
interval = defaults.interval
) {
// 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 { setTimeout } = typeof window !== "undefined" ? window : global;
const setTimeout = getSetTimeoutFn();

// eslint-disable-next-line no-param-reassign
if (interval < 1) interval = 1;
Expand Down
4 changes: 3 additions & 1 deletion src/withFakeTimers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ beforeEach(() => {
jest.useRealTimers();
});

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

jest.useFakeTimers();

await importedWaitForExpect(() => {
expect(numberToChange).toEqual(100);
});
Expand Down

0 comments on commit dd272db

Please sign in to comment.