Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(jest): compatibility with jest@28 #37

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
12.22.12
2 changes: 1 addition & 1 deletion circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: 2
jobs:
build:
docker:
- image: circleci/node:8.10.0
- image: circleci/node:12
steps:
- checkout
- run:
Expand Down
16,967 changes: 8,510 additions & 8,457 deletions package-lock.json

Large diffs are not rendered by default.

25 changes: 11 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,33 +33,33 @@
},
"homepage": "https://github.com/TheBrainFamily/wait-for-expect#readme",
"devDependencies": {
"@babel/cli": "^7.0.0-beta.42",
"@babel/core": "^7.0.0-beta.42",
"@babel/plugin-proposal-class-properties": "^7.0.0-beta.42",
"@babel/plugin-proposal-object-rest-spread": "^7.0.0-beta.42",
"@babel/preset-env": "^7.0.0-beta.42",
"@babel/preset-typescript": "^7.0.0-beta.42",
"@babel/cli": "^7.21.5",
"@babel/core": "^7.0.0",
"@babel/plugin-proposal-class-properties": "^7.0.0",
"@babel/plugin-proposal-object-rest-spread": "^7.0.0",
"@babel/preset-env": "^7.0.0",
"@babel/preset-typescript": "^7.0.0",
"@types/eslint": "^4.16.0",
"@types/eslint-plugin-prettier": "^2.2.0",
"@types/jest": "^22.2.2",
"@types/node": "^9.6.1",
"@types/jest": "^27.0.0",
"@types/node": "^12.0.0",
"@types/prettier": "^1.10.0",
"@types/typescript": "^2.0.0",
"@typescript-eslint/eslint-plugin": "^2.5.0",
"@typescript-eslint/parser": "^2.5.0",
"babel-core": "^7.0.0-0",
"babel-jest": "^22.4.3",
"babel-plugin-add-module-exports": "^1.0.0",
"babel-plugin-add-module-exports": "^1.0.4",
"eslint": "^6.5.1",
"eslint-config-airbnb-base": "^14.0.0",
"eslint-config-prettier": "^2.9.0",
"eslint-plugin-import": "^2.18.2",
"eslint-plugin-prettier": "^2.6.0",
"jest": "^22.4.3",
"jest": "^28.0.0",
"jest-serializer-ansi": "^1.0.3",
"prettier": "^1.11.1",
"semantic-release": "^15.12.0",
"typescript": "~3.2.4"
"typescript": "^4.0.0"
},
"jest": {
"snapshotSerializers": [
Expand All @@ -75,9 +75,6 @@
"js",
"jsx"
],
"transform": {
"^.+\\.(js|jsx|ts|tsx)$": "babel-jest"
},
"testRegex": "/src/.*\\.spec\\.(js|ts|tsx)$",
"testURL": "http://localhost/"
}
Expand Down
16 changes: 6 additions & 10 deletions src/__snapshots__/waitForExpect.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`it fails properly with jest error message when it times out without expectation passing 1`] = `
"expect(received).toEqual(expected)
"expect(received).toEqual(expected) // deep equality

Expected value to equal:
2000
Received:
200"
Expected: 2000
Received: 200"
`;

exports[`it fails when the change didn't happen fast enough, based on the waitForExpect timeout 1`] = `
"expect(received).toEqual(expected)
"expect(received).toEqual(expected) // deep equality

Expected value to equal:
3000
Received:
300"
Expected: 3000
Received: 300"
`;
8 changes: 5 additions & 3 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ 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";
typeof jest !== "undefined" &&
// @ts-ignore
setTimeout.clock != null &&
// @ts-ignore
typeof setTimeout.clock.Date === "function";

if (usingJestFakeTimers) {
jest.useRealTimers();
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const waitForExpect = function waitForExpect(
if (interval < 1) interval = 1;
const maxTries = Math.ceil(timeout / interval);
let tries = 0;
return new Promise((resolve, reject) => {
return new Promise<void>((resolve, reject) => {
const rejectOrRerun = (error: Error) => {
if (tries > maxTries) {
reject(error);
Expand All @@ -40,7 +40,7 @@ const waitForExpect = function waitForExpect(
.then(() => resolve())
.catch(rejectOrRerun);
} catch (error) {
rejectOrRerun(error);
rejectOrRerun(error as Error);
}
}
setTimeout(runExpectation, 0);
Expand Down
12 changes: 5 additions & 7 deletions src/waitForExpect.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,22 @@ test("it waits for expectation to pass", async () => {

test(
"it fails properly with jest error message when it times out without expectation passing",
async done => {
async () => {
const numberNotToChange = 200;
try {
await waitForExpect(() => {
expect(numberNotToChange).toEqual(2000);
}, 300);
} catch (e) {
expect(e.message).toMatchSnapshot();
done();
expect((e as Error).message).toMatchSnapshot();
}
},
1000
);

test(
"it fails when the change didn't happen fast enough, based on the waitForExpect timeout",
async done => {
async () => {
let numberToChangeTooLate = 300;
const timeToPassForTheChangeToHappen = 1000;

Expand All @@ -53,8 +52,7 @@ test(
expect(numberToChangeTooLate).toEqual(3000);
}, timeToPassForTheChangeToHappen - 200);
} catch (e) {
expect(e.message).toMatchSnapshot();
done();
expect((e as Error).message).toMatchSnapshot();
}
},
1500
Expand Down Expand Up @@ -112,7 +110,7 @@ test("it works with promises", async () => {
}, randomTimeout);

const sleep = (ms: number) =>
new Promise(resolve => setTimeout(() => resolve(), ms));
new Promise<void>(resolve => setTimeout(() => resolve(), ms));

await waitForExpect(async () => {
await sleep(10);
Expand Down
8 changes: 4 additions & 4 deletions src/withFakeTimers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ test("it works even if the Date was mocked", async () => {
expect(numberToChange).toEqual(101);
}, 1000);
} catch (e) {
expectFailingMessage = e.message;
expectFailingMessage = (e as Error).message;
}
expect(expectFailingMessage).toMatch("Expected value to equal:");
expect(expectFailingMessage).toMatch(/toEqual/);
expect(expectFailingMessage).toMatch("101");
expect(expectFailingMessage).toMatch("Received:");
expect(expectFailingMessage).toMatch("100");
Expand All @@ -78,9 +78,9 @@ test("it works even if the Date.now was mocked", async () => {
expect(numberToChange).toEqual(101);
}, 1000);
} catch (e) {
expectFailingMessage = e.message;
expectFailingMessage = (e as Error).message;
}
expect(expectFailingMessage).toMatch("Expected value to equal:");
expect(expectFailingMessage).toMatch(/toEqual/);
expect(expectFailingMessage).toMatch("101");
expect(expectFailingMessage).toMatch("Received:");
expect(expectFailingMessage).toMatch("100");
Expand Down