Skip to content

fix: improve suggested message, can't await getBy #655

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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