From 7bf926a9b347ff297bdc3e4099fb123d85935392 Mon Sep 17 00:00:00 2001 From: mmkal Date: Thu, 16 May 2019 03:59:40 -0400 Subject: [PATCH] feat: configurable default timeout and interval (#18) * feat: configurable default timeout and interval * chore: fix prettier --- README.md | 8 ++++++++ src/index.ts | 11 ++++++++--- src/waitForExpect.spec.ts | 22 ++++++++++++++++++++++ 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 99db3cb..462ac06 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/index.ts b/src/index.ts index d0f3a2a..e9d6c2f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 * @@ -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) => { @@ -49,4 +54,4 @@ const waitForExpect = function waitForExpect( }); }; -export default waitForExpect; +export default Object.assign(waitForExpect, { defaults }); diff --git a/src/waitForExpect.spec.ts b/src/waitForExpect.spec.ts index 40ff40a..3fc3d30 100644 --- a/src/waitForExpect.spec.ts +++ b/src/waitForExpect.spec.ts @@ -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 @@ -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);