Skip to content

Commit 2a039d8

Browse files
committed
Fix end-to-end tests
They also need work in DTL since advanceTimersWrapper is now async.
1 parent 12f9187 commit 2a039d8

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

src/__tests__/end-to-end.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -139,18 +139,14 @@ describe.each([
139139
test('waitForElementToBeRemoved', async () => {
140140
await render(<ComponentWithMicrotaskLoader />)
141141
const loading = () => screen.getByText('Loading..')
142-
await waitForElementToBeRemoved(loading)
142+
// Already flushed microtasks so we'll never see the loading state in a test.
143+
expect(loading).toThrowError(/Unable to find an element with the text/)
143144
expect(screen.getByTestId('message')).toHaveTextContent(/Hello World/)
144145
})
145146

146-
test('waitFor', async () => {
147+
test('waitFor is not needed since microtasks are flushed', async () => {
147148
await render(<ComponentWithMicrotaskLoader />)
148-
await waitFor(() => {
149-
screen.getByText('Loading..')
150-
})
151-
await waitFor(() => {
152-
screen.getByText(/Loaded this message:/)
153-
})
149+
154150
expect(screen.getByTestId('message')).toHaveTextContent(/Hello World/)
155151
})
156152

src/pure.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ function jestFakeTimersAreEnabled() {
2828

2929
configureDTL({
3030
unstable_advanceTimersWrapper: cb => {
31+
// Only needed to support test environments that enable fake timers after modules are loaded.
32+
// React's scheduler will detect fake timers when it's initialized and use them.
33+
// So if we change the timers after that, we need to re-initialize the scheduler.
34+
// But not every test runner supports module reset.
35+
// It's not even clear how modules should be reset in ESM.
36+
// So for this brief period we go back to using the act queue.
3137
return act(cb)
3238
},
3339
// We just want to run `waitFor` without IS_REACT_ACT_ENVIRONMENT

0 commit comments

Comments
 (0)