Skip to content

Commit 18b1623

Browse files
fix: improve suggested message, can't await getBy (#655)
1 parent 4828687 commit 18b1623

File tree

2 files changed

+28
-24
lines changed

2 files changed

+28
-24
lines changed

src/__tests__/helpers.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ describe('window retrieval throws when given something other than a node', () =>
1818
expect(() =>
1919
getWindowFromNode(new Promise(jest.fn())),
2020
).toThrowErrorMatchingInlineSnapshot(
21-
`"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...\`?"`,
21+
`"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...\`?"`,
2222
)
2323
})
2424
test('unknown as node', () => {
@@ -61,8 +61,8 @@ test('should always use realTimers before using callback when timers are faked w
6161
runWithRealTimers(() => {
6262
expect(originalSetTimeout).toEqual(globalObj.setTimeout)
6363
})
64-
expect(globalObj.setTimeout._isMockFunction).toBe(true);
65-
expect(globalObj.setTimeout.clock).toBeUndefined();
64+
expect(globalObj.setTimeout._isMockFunction).toBe(true)
65+
expect(globalObj.setTimeout.clock).toBeUndefined()
6666

6767
jest.useRealTimers()
6868

@@ -71,24 +71,24 @@ test('should always use realTimers before using callback when timers are faked w
7171
runWithRealTimers(() => {
7272
expect(originalSetTimeout).toEqual(globalObj.setTimeout)
7373
})
74-
expect(globalObj.setTimeout._isMockFunction).toBeUndefined();
75-
expect(globalObj.setTimeout.clock).toBeDefined();
74+
expect(globalObj.setTimeout._isMockFunction).toBeUndefined()
75+
expect(globalObj.setTimeout.clock).toBeDefined()
7676
})
7777

7878
test('should not use realTimers when timers are not faked with useFakeTimers', () => {
79-
const originalSetTimeout = globalObj.setTimeout;
80-
79+
const originalSetTimeout = globalObj.setTimeout
80+
8181
// useFakeTimers is not used, timers are faked in some other way
82-
const fakedSetTimeout = (callback) => {
83-
callback();
84-
};
85-
fakedSetTimeout.clock = jest.fn();
82+
const fakedSetTimeout = callback => {
83+
callback()
84+
}
85+
fakedSetTimeout.clock = jest.fn()
8686

87-
globalObj.setTimeout = fakedSetTimeout;
87+
globalObj.setTimeout = fakedSetTimeout
8888

8989
runWithRealTimers(() => {
9090
expect(fakedSetTimeout).toEqual(globalObj.setTimeout)
9191
})
9292

93-
globalObj.setTimeout = originalSetTimeout;
94-
});
93+
globalObj.setTimeout = originalSetTimeout
94+
})

src/helpers.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,30 @@ const globalObj = typeof window === 'undefined' ? global : window
22

33
// Currently this fn only supports jest timers, but it could support other test runners in the future.
44
function runWithRealTimers(callback) {
5-
const usingJestAndTimers = typeof jest !== 'undefined' && typeof globalObj.setTimeout !== 'undefined';
6-
const usingLegacyJestFakeTimers = usingJestAndTimers && typeof globalObj.setTimeout._isMockFunction !== 'undefined' && globalObj.setTimeout._isMockFunction;
5+
const usingJestAndTimers =
6+
typeof jest !== 'undefined' && typeof globalObj.setTimeout !== 'undefined'
7+
const usingLegacyJestFakeTimers =
8+
usingJestAndTimers &&
9+
typeof globalObj.setTimeout._isMockFunction !== 'undefined' &&
10+
globalObj.setTimeout._isMockFunction
711

8-
let usingModernJestFakeTimers = false;
12+
let usingModernJestFakeTimers = false
913
if (
1014
usingJestAndTimers &&
11-
typeof globalObj.setTimeout.clock !== "undefined" &&
12-
typeof jest.getRealSystemTime !== "undefined"
15+
typeof globalObj.setTimeout.clock !== 'undefined' &&
16+
typeof jest.getRealSystemTime !== 'undefined'
1317
) {
1418
try {
1519
// jest.getRealSystemTime is only supported for Jest's `modern` fake timers and otherwise throws
16-
jest.getRealSystemTime();
17-
usingModernJestFakeTimers = true;
20+
jest.getRealSystemTime()
21+
usingModernJestFakeTimers = true
1822
} catch {
1923
// not using Jest's modern fake timers
2024
}
2125
}
2226

2327
const usingJestFakeTimers =
24-
usingLegacyJestFakeTimers || usingModernJestFakeTimers;
28+
usingLegacyJestFakeTimers || usingModernJestFakeTimers
2529

2630
if (usingJestFakeTimers) {
2731
jest.useRealTimers()
@@ -51,7 +55,7 @@ function getTimeFunctions() {
5155
}
5256
}
5357

54-
const { clearTimeoutFn, setImmediateFn, setTimeoutFn } = runWithRealTimers(
58+
const {clearTimeoutFn, setImmediateFn, setTimeoutFn} = runWithRealTimers(
5559
getTimeFunctions,
5660
)
5761

@@ -74,7 +78,7 @@ function getWindowFromNode(node) {
7478
return node.window
7579
} else if (node.then instanceof Function) {
7680
throw new Error(
77-
`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...\`?`,
81+
`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...\`?`,
7882
)
7983
} else {
8084
// The user passed something unusual to a calling function

0 commit comments

Comments
 (0)