Skip to content

Commit

Permalink
feat: configurable default timeout and interval (#18)
Browse files Browse the repository at this point in the history
* feat: configurable default timeout and interval

* chore: fix prettier
  • Loading branch information
mmkal authored and lgandecki committed May 16, 2019
1 parent 872b811 commit 7bf926a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,14 @@ waitForExpect takes 3 arguments, 2 optional.
*/
```

The defaults for `timeout` and `interval` can also be edited globally, e.g. in a jest setup file:
```javascript
import waitForExpect from 'wait-for-expect';

waitForExpect.defaults.timeout = 2000;
waitForExpect.defaults.interval = 10;
```

## Changelog
1.0.0 - 15 June 2018

Expand Down
11 changes: 8 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ declare global {
const { setTimeout, Date: { now } } =
typeof window !== "undefined" ? window : global;

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

/**
* Waits for the expectation to pass and returns a Promise
*
Expand All @@ -23,8 +28,8 @@ const { setTimeout, Date: { now } } =
*/
const waitForExpect = function waitForExpect(
expectation: () => void,
timeout = 4500,
interval = 50
timeout = defaults.timeout,
interval = defaults.interval
) {
const startTime = now();
return new Promise((resolve, reject) => {
Expand All @@ -49,4 +54,4 @@ const waitForExpect = function waitForExpect(
});
};

export default waitForExpect;
export default Object.assign(waitForExpect, { defaults });
22 changes: 22 additions & 0 deletions src/waitForExpect.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
import "./toBeInRangeMatcher";
import waitForExpect from "./index";

const originalDefaults = { ...waitForExpect.defaults };
beforeEach(() => {
Object.assign(waitForExpect.defaults, originalDefaults);
});

test("it waits for expectation to pass", async () => {
let numberToChange = 10;
// we are using random timeout here to simulate a real-time example
Expand Down Expand Up @@ -81,6 +86,23 @@ test("it reruns the expectation every x ms, as provided with the second argument
}
});

test("it reruns the expectation every x ms, as provided by the default timeout and interval", async () => {
const timeout = 600;
const interval = 150;
waitForExpect.defaults.timeout = timeout;
waitForExpect.defaults.interval = interval;
const mockExpectation = jest.fn();
mockExpectation.mockImplementation(() => expect(true).toEqual(false));
try {
await waitForExpect(mockExpectation);
throw Error("waitForExpect should have thrown");
} catch (e) {
// initial run + reruns
const expectedTimesToRun = 1 + Math.floor(timeout / interval);
expect(mockExpectation).toHaveBeenCalledTimes(expectedTimesToRun);
}
});

test("it works with promises", async () => {
let numberToChange = 10;
const randomTimeout = Math.floor(Math.random() * 300);
Expand Down

0 comments on commit 7bf926a

Please sign in to comment.