Skip to content

Commit

Permalink
fix: improve suggested message, can't await getBy (#655)
Browse files Browse the repository at this point in the history
  • Loading branch information
timdeschryver committed Jun 20, 2020
1 parent 4828687 commit 18b1623
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 24 deletions.
28 changes: 14 additions & 14 deletions src/__tests__/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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()

Expand All @@ -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
})
24 changes: 14 additions & 10 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -51,7 +55,7 @@ function getTimeFunctions() {
}
}

const { clearTimeoutFn, setImmediateFn, setTimeoutFn } = runWithRealTimers(
const {clearTimeoutFn, setImmediateFn, setTimeoutFn} = runWithRealTimers(
getTimeFunctions,
)

Expand All @@ -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
Expand Down

0 comments on commit 18b1623

Please sign in to comment.