Skip to content

Commit

Permalink
feat: use real Date.now to avoid using potentially mocked one (#12)
Browse files Browse the repository at this point in the history
* (#12) use real Date.now to avoid using potentially mocked one

* (#12) make tsc happy with Date on Window interface and Date jest mock
  • Loading branch information
valignatev authored and lgandecki committed Nov 16, 2018
1 parent 7bc02ea commit 0f11a85
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 7 deletions.
21 changes: 16 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
// Used to avoid using Jest's fake timers.
// See https://github.com/TheBrainFamily/wait-for-expect/issues/4 for more info
const { setTimeout } = typeof window !== "undefined" ? window : global;
// Augment Window interface with the Date declaratoin,
// because typescript does not expose it for now.
// Check https://github.com/Microsoft/TypeScript/issues/19816 for more info
declare global {
/* eslint-disable-next-line no-undef */
interface Window {
Date: typeof Date;
}
}
// 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, Date: { now } } =
typeof window !== "undefined" ? window : global;

/**
* Waits for the expectation to pass and returns a Promise
Expand All @@ -15,10 +26,10 @@ const waitForExpect = function waitForExpect(
timeout = 4500,
interval = 50
) {
const startTime = Date.now();
const startTime = now();
return new Promise((resolve, reject) => {
const rejectOrRerun = (error: Error) => {
if (Date.now() - startTime >= timeout) {
if (now() - startTime >= timeout) {
reject(error);
return;
}
Expand Down
61 changes: 59 additions & 2 deletions src/withFakeTimers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@
import "./toBeInRangeMatcher";
import waitForExpect from "./index";

// this is a copy of "it waits for expectation to pass" modified to use jestFakeTimers
// This breakes when we remove the const { setTimeout } = typeof window !== "undefined" ? window : global;
// this is a copy of "it waits for expectation to pass" modified to use jestFakeTimers and two ways of Date.now mocking
// This breakes when we remove the const { setTimeout, Date: { now } } = typeof window !== "undefined" ? window : global;
// line from the index.ts

beforeEach(() => {
jest.restoreAllMocks();
jest.useRealTimers();
});

test("it works even if the timers are overwritten by jest", async () => {
jest.useFakeTimers();
let numberToChange = 10;
Expand All @@ -22,3 +27,55 @@ test("it works even if the timers are overwritten by jest", async () => {
expect(numberToChange).toEqual(100);
});
});

// Date.now might be mocked with two main ways:
// via mocking whole Date, or by mocking just Date.now
// hence two test cases covered both ways
test("it works even if the Date was mocked", async () => {
/* eslint-disable no-global-assign */
// @ts-ignore: Cannot reassign to const Date
Date = jest.fn(() => ({
now() {
return 1482363367071;
}
}));
/* eslint-enable */
let numberToChange = 10;

setTimeout(() => {
numberToChange = 100;
}, 100);
let expectFailingMessage;
try {
await waitForExpect(() => {
expect(numberToChange).toEqual(101);
}, 1000);
} catch (e) {
expectFailingMessage = e.message;
}
expect(expectFailingMessage).toMatch("Expected value to equal:");
expect(expectFailingMessage).toMatch("101");
expect(expectFailingMessage).toMatch("Received:");
expect(expectFailingMessage).toMatch("100");
});

test("it works even if the Date.now was mocked", async () => {
Date.now = jest.fn(() => 1482363367071);
let numberToChange = 10;

setTimeout(() => {
numberToChange = 100;
}, 100);
let expectFailingMessage;
try {
await waitForExpect(() => {
expect(numberToChange).toEqual(101);
}, 1000);
} catch (e) {
expectFailingMessage = e.message;
}
expect(expectFailingMessage).toMatch("Expected value to equal:");
expect(expectFailingMessage).toMatch("101");
expect(expectFailingMessage).toMatch("Received:");
expect(expectFailingMessage).toMatch("100");
});

0 comments on commit 0f11a85

Please sign in to comment.