Skip to content

Commit 7bf926a

Browse files
mmkallgandecki
authored andcommitted
feat: configurable default timeout and interval (#18)
* feat: configurable default timeout and interval * chore: fix prettier
1 parent 872b811 commit 7bf926a

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,14 @@ waitForExpect takes 3 arguments, 2 optional.
161161
*/
162162
```
163163

164+
The defaults for `timeout` and `interval` can also be edited globally, e.g. in a jest setup file:
165+
```javascript
166+
import waitForExpect from 'wait-for-expect';
167+
168+
waitForExpect.defaults.timeout = 2000;
169+
waitForExpect.defaults.interval = 10;
170+
```
171+
164172
## Changelog
165173
1.0.0 - 15 June 2018
166174

src/index.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ declare global {
1313
const { setTimeout, Date: { now } } =
1414
typeof window !== "undefined" ? window : global;
1515

16+
const defaults = {
17+
timeout: 4500,
18+
interval: 50
19+
};
20+
1621
/**
1722
* Waits for the expectation to pass and returns a Promise
1823
*
@@ -23,8 +28,8 @@ const { setTimeout, Date: { now } } =
2328
*/
2429
const waitForExpect = function waitForExpect(
2530
expectation: () => void,
26-
timeout = 4500,
27-
interval = 50
31+
timeout = defaults.timeout,
32+
interval = defaults.interval
2833
) {
2934
const startTime = now();
3035
return new Promise((resolve, reject) => {
@@ -49,4 +54,4 @@ const waitForExpect = function waitForExpect(
4954
});
5055
};
5156

52-
export default waitForExpect;
57+
export default Object.assign(waitForExpect, { defaults });

src/waitForExpect.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
import "./toBeInRangeMatcher";
33
import waitForExpect from "./index";
44

5+
const originalDefaults = { ...waitForExpect.defaults };
6+
beforeEach(() => {
7+
Object.assign(waitForExpect.defaults, originalDefaults);
8+
});
9+
510
test("it waits for expectation to pass", async () => {
611
let numberToChange = 10;
712
// we are using random timeout here to simulate a real-time example
@@ -81,6 +86,23 @@ test("it reruns the expectation every x ms, as provided with the second argument
8186
}
8287
});
8388

89+
test("it reruns the expectation every x ms, as provided by the default timeout and interval", async () => {
90+
const timeout = 600;
91+
const interval = 150;
92+
waitForExpect.defaults.timeout = timeout;
93+
waitForExpect.defaults.interval = interval;
94+
const mockExpectation = jest.fn();
95+
mockExpectation.mockImplementation(() => expect(true).toEqual(false));
96+
try {
97+
await waitForExpect(mockExpectation);
98+
throw Error("waitForExpect should have thrown");
99+
} catch (e) {
100+
// initial run + reruns
101+
const expectedTimesToRun = 1 + Math.floor(timeout / interval);
102+
expect(mockExpectation).toHaveBeenCalledTimes(expectedTimesToRun);
103+
}
104+
});
105+
84106
test("it works with promises", async () => {
85107
let numberToChange = 10;
86108
const randomTimeout = Math.floor(Math.random() * 300);

0 commit comments

Comments
 (0)