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

support for min consecutive passes #28

Open
wants to merge 1 commit 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
33 changes: 27 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ import { getSetTimeoutFn } from "./helpers";

const defaults = {
timeout: 4500,
interval: 50
interval: 50,
minConsecutivePasses: 1
};

const consecutivePassesDefaultError = new Error(
"Test have not passed the min number of consecutive required runs, might be a flaky test"
);

/**
* Waits for the expectation to pass and returns a Promise
*
Expand All @@ -16,16 +21,25 @@ const defaults = {
const waitForExpect = function waitForExpect(
expectation: () => void | Promise<void>,
timeout = defaults.timeout,
interval = defaults.interval
interval = defaults.interval,
minConsecutivePasses = defaults.minConsecutivePasses
) {
const setTimeout = getSetTimeoutFn();

// eslint-disable-next-line no-param-reassign
if (interval < 1) interval = 1;
const maxTries = Math.ceil(timeout / interval);
let tries = 0;
let consecutivePasses = 0;
let lastError = consecutivePassesDefaultError;
return new Promise((resolve, reject) => {
const rejectOrRerun = (error: Error) => {
const getRejectOrRerun = (resetConsecutivePasses: boolean) => (
error: Error
) => {
if (resetConsecutivePasses) {
consecutivePasses = 0;
lastError = error;
}
if (tries > maxTries) {
reject(error);
return;
Expand All @@ -37,10 +51,17 @@ const waitForExpect = function waitForExpect(
tries += 1;
try {
Promise.resolve(expectation())
.then(() => resolve())
.catch(rejectOrRerun);
.then(() => {
consecutivePasses += 1;
if (consecutivePasses === minConsecutivePasses) {
resolve();
return;
}
getRejectOrRerun(false)(lastError);
})
.catch(getRejectOrRerun(true));
} catch (error) {
rejectOrRerun(error);
getRejectOrRerun(true)(error);
}
}
setTimeout(runExpectation, 0);
Expand Down
19 changes: 19 additions & 0 deletions src/waitForExpect.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,22 @@ test("it works with a zero interval", async () => {
0
);
});

test("it does not pass flaky tests", async () => {
waitForExpect.defaults.minConsecutivePasses = 3;
try {
let counter = 0;
await waitForExpect(() => {
// a flaky test that passes if retried enough times
counter += 1;
if (counter % 10 === 0) {
expect(true).toEqual(true);
} else {
expect(true).toEqual(false);
}
});
throw Error("waitForExpect should have thrown");
} catch ({ message }) {
expect(message).not.toEqual("waitForExpect should have thrown");
}
});