diff --git a/src/__tests__/helpers.js b/src/__tests__/helpers.js index 49eb2b24..1bc85837 100644 --- a/src/__tests__/helpers.js +++ b/src/__tests__/helpers.js @@ -18,7 +18,7 @@ describe('window retrieval throws when given something other than a node', () => expect(() => getWindowFromNode(new Promise(jest.fn())), ).toThrowErrorMatchingInlineSnapshot( - `"It looks like you passed a Promise object instead of a DOM node. Did you do something like \`fireEvent.click(screen.findBy...\` when you meant to do \`fireEvent.click(await screen.getBy...\`?"`, + `"It looks like you passed a Promise object instead of a DOM node. Did you do something like \`fireEvent.click(screen.findBy...\` when you meant to use a \`getBy\` query \`fireEvent.click(screen.getBy...\`, or await the findBy query \`fireEvent.click(await screen.findBy...\`?"`, ) }) test('unknown as node', () => { @@ -61,8 +61,8 @@ test('should always use realTimers before using callback when timers are faked w runWithRealTimers(() => { expect(originalSetTimeout).toEqual(globalObj.setTimeout) }) - expect(globalObj.setTimeout._isMockFunction).toBe(true); - expect(globalObj.setTimeout.clock).toBeUndefined(); + expect(globalObj.setTimeout._isMockFunction).toBe(true) + expect(globalObj.setTimeout.clock).toBeUndefined() jest.useRealTimers() @@ -71,24 +71,24 @@ test('should always use realTimers before using callback when timers are faked w runWithRealTimers(() => { expect(originalSetTimeout).toEqual(globalObj.setTimeout) }) - expect(globalObj.setTimeout._isMockFunction).toBeUndefined(); - expect(globalObj.setTimeout.clock).toBeDefined(); + expect(globalObj.setTimeout._isMockFunction).toBeUndefined() + expect(globalObj.setTimeout.clock).toBeDefined() }) test('should not use realTimers when timers are not faked with useFakeTimers', () => { - const originalSetTimeout = globalObj.setTimeout; - + const originalSetTimeout = globalObj.setTimeout + // useFakeTimers is not used, timers are faked in some other way - const fakedSetTimeout = (callback) => { - callback(); - }; - fakedSetTimeout.clock = jest.fn(); + const fakedSetTimeout = callback => { + callback() + } + fakedSetTimeout.clock = jest.fn() - globalObj.setTimeout = fakedSetTimeout; + globalObj.setTimeout = fakedSetTimeout runWithRealTimers(() => { expect(fakedSetTimeout).toEqual(globalObj.setTimeout) }) - globalObj.setTimeout = originalSetTimeout; -}); + globalObj.setTimeout = originalSetTimeout +}) diff --git a/src/helpers.js b/src/helpers.js index 1a891d0a..feab0aa6 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -2,26 +2,30 @@ 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) { - const usingJestAndTimers = typeof jest !== 'undefined' && typeof globalObj.setTimeout !== 'undefined'; - const usingLegacyJestFakeTimers = usingJestAndTimers && typeof globalObj.setTimeout._isMockFunction !== 'undefined' && globalObj.setTimeout._isMockFunction; + const usingJestAndTimers = + typeof jest !== 'undefined' && typeof globalObj.setTimeout !== 'undefined' + const usingLegacyJestFakeTimers = + usingJestAndTimers && + typeof globalObj.setTimeout._isMockFunction !== 'undefined' && + globalObj.setTimeout._isMockFunction - let usingModernJestFakeTimers = false; + let usingModernJestFakeTimers = false if ( usingJestAndTimers && - typeof globalObj.setTimeout.clock !== "undefined" && - typeof jest.getRealSystemTime !== "undefined" + typeof globalObj.setTimeout.clock !== 'undefined' && + typeof jest.getRealSystemTime !== 'undefined' ) { try { // jest.getRealSystemTime is only supported for Jest's `modern` fake timers and otherwise throws - jest.getRealSystemTime(); - usingModernJestFakeTimers = true; + jest.getRealSystemTime() + usingModernJestFakeTimers = true } catch { // not using Jest's modern fake timers } } const usingJestFakeTimers = - usingLegacyJestFakeTimers || usingModernJestFakeTimers; + usingLegacyJestFakeTimers || usingModernJestFakeTimers if (usingJestFakeTimers) { jest.useRealTimers() @@ -51,7 +55,7 @@ function getTimeFunctions() { } } -const { clearTimeoutFn, setImmediateFn, setTimeoutFn } = runWithRealTimers( +const {clearTimeoutFn, setImmediateFn, setTimeoutFn} = runWithRealTimers( getTimeFunctions, ) @@ -74,7 +78,7 @@ function getWindowFromNode(node) { return node.window } else if (node.then instanceof Function) { throw new Error( - `It looks like you passed a Promise object instead of a DOM node. Did you do something like \`fireEvent.click(screen.findBy...\` when you meant to do \`fireEvent.click(await screen.getBy...\`?`, + `It looks like you passed a Promise object instead of a DOM node. Did you do something like \`fireEvent.click(screen.findBy...\` when you meant to use a \`getBy\` query \`fireEvent.click(screen.getBy...\`, or await the findBy query \`fireEvent.click(await screen.findBy...\`?`, ) } else { // The user passed something unusual to a calling function