Skip to content

Commit 487eb85

Browse files
authored
test: Ignore React 18 legacy root deprecation warnings (#928)
1 parent 7957355 commit 487eb85

File tree

4 files changed

+62
-18
lines changed

4 files changed

+62
-18
lines changed

src/__tests__/cleanup.js

+14-5
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,10 @@ describe('fake timers and missing act warnings', () => {
8282
expect(microTaskSpy).toHaveBeenCalledTimes(0)
8383
// console.error is mocked
8484
// eslint-disable-next-line no-console
85-
expect(console.error).toHaveBeenCalledTimes(0)
85+
expect(console.error).toHaveBeenCalledTimes(
86+
// ReactDOM.render is deprecated in React 18
87+
React.version.startsWith('18') ? 1 : 0,
88+
)
8689
})
8790

8891
test('cleanup does not swallow missing act warnings', () => {
@@ -114,10 +117,16 @@ describe('fake timers and missing act warnings', () => {
114117
expect(deferredStateUpdateSpy).toHaveBeenCalledTimes(1)
115118
// console.error is mocked
116119
// eslint-disable-next-line no-console
117-
expect(console.error).toHaveBeenCalledTimes(1)
118-
// eslint-disable-next-line no-console
119-
expect(console.error.mock.calls[0][0]).toMatch(
120-
'a test was not wrapped in act(...)',
120+
expect(console.error).toHaveBeenCalledTimes(
121+
// ReactDOM.render is deprecated in React 18
122+
React.version.startsWith('18') ? 2 : 1,
121123
)
124+
// eslint-disable-next-line no-console
125+
expect(
126+
console.error.mock.calls[
127+
// ReactDOM.render is deprecated in React 18
128+
React.version.startsWith('18') ? 1 : 0
129+
][0],
130+
).toMatch('a test was not wrapped in act(...)')
122131
})
123132
})

src/__tests__/no-act.js

+24-12
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
let act, asyncAct
1+
let act, asyncAct, React
22

33
beforeEach(() => {
44
jest.resetModules()
55
act = require('..').act
66
asyncAct = require('../act-compat').asyncAct
7+
React = require('react')
78
jest.spyOn(console, 'error').mockImplementation(() => {})
89
})
910

@@ -17,7 +18,10 @@ test('act works even when there is no act from test utils', () => {
1718
const callback = jest.fn()
1819
act(callback)
1920
expect(callback).toHaveBeenCalledTimes(1)
20-
expect(console.error).toHaveBeenCalledTimes(0)
21+
expect(console.error).toHaveBeenCalledTimes(
22+
// ReactDOM.render is deprecated in React 18
23+
React.version.startsWith('18') ? 1 : 0,
24+
)
2125
})
2226

2327
test('async act works when it does not exist (older versions of react)', async () => {
@@ -26,7 +30,10 @@ test('async act works when it does not exist (older versions of react)', async (
2630
await Promise.resolve()
2731
await callback()
2832
})
29-
expect(console.error).toHaveBeenCalledTimes(0)
33+
expect(console.error).toHaveBeenCalledTimes(
34+
// ReactDOM.render is deprecated in React 18
35+
React.version.startsWith('18') ? 2 : 0,
36+
)
3037
expect(callback).toHaveBeenCalledTimes(1)
3138

3239
callback.mockClear()
@@ -36,7 +43,10 @@ test('async act works when it does not exist (older versions of react)', async (
3643
await Promise.resolve()
3744
await callback()
3845
})
39-
expect(console.error).toHaveBeenCalledTimes(0)
46+
expect(console.error).toHaveBeenCalledTimes(
47+
// ReactDOM.render is deprecated in React 18
48+
React.version.startsWith('18') ? 2 : 0,
49+
)
4050
expect(callback).toHaveBeenCalledTimes(1)
4151
})
4252

@@ -49,14 +59,16 @@ test('async act recovers from errors', async () => {
4959
} catch (err) {
5060
console.error('call console.error')
5161
}
52-
expect(console.error).toHaveBeenCalledTimes(1)
53-
expect(console.error.mock.calls).toMatchInlineSnapshot(`
54-
Array [
55-
Array [
56-
"call console.error",
57-
],
58-
]
59-
`)
62+
expect(console.error).toHaveBeenCalledTimes(
63+
// ReactDOM.render is deprecated in React 18
64+
React.version.startsWith('18') ? 2 : 1,
65+
)
66+
expect(
67+
console.error.mock.calls[
68+
// ReactDOM.render is deprecated in React 18
69+
React.version.startsWith('18') ? 1 : 0
70+
][0],
71+
).toMatch('call console.error')
6072
})
6173

6274
test('async act recovers from sync errors', async () => {

src/__tests__/stopwatch.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,8 @@ test('unmounts a component', async () => {
5353
// and get an error.
5454
await sleep(5)
5555
// eslint-disable-next-line no-console
56-
expect(console.error).not.toHaveBeenCalled()
56+
expect(console.error).toHaveBeenCalledTimes(
57+
// ReactDOM.render is deprecated in React 18
58+
React.version.startsWith('18') ? 1 : 0,
59+
)
5760
})

tests/setup-env.js

+20
Original file line numberDiff line numberDiff line change
@@ -1 +1,21 @@
11
import '@testing-library/jest-dom/extend-expect'
2+
3+
beforeEach(() => {
4+
const originalConsoleError = console.error
5+
jest
6+
.spyOn(console, 'error')
7+
.mockImplementation((message, ...optionalParams) => {
8+
// Ignore ReactDOM.render/ReactDOM.hydrate deprecation warning
9+
if (message.indexOf('Use createRoot instead.') !== -1) {
10+
return
11+
}
12+
originalConsoleError(message, ...optionalParams)
13+
})
14+
})
15+
16+
afterEach(() => {
17+
// maybe another test already restore console error mocks
18+
if (typeof console.error.mockRestore === 'function') {
19+
console.error.mockRestore()
20+
}
21+
})

0 commit comments

Comments
 (0)