Skip to content

Commit 545a35e

Browse files
committed
test: Ignore React 18 legacy root deprecation warnings (#928)
1 parent c1a931d commit 545a35e

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
@@ -83,7 +83,10 @@ describe('fake timers and missing act warnings', () => {
8383
expect(microTaskSpy).toHaveBeenCalledTimes(0)
8484
// console.error is mocked
8585
// eslint-disable-next-line no-console
86-
expect(console.error).toHaveBeenCalledTimes(0)
86+
expect(console.error).toHaveBeenCalledTimes(
87+
// ReactDOM.render is deprecated in React 18
88+
React.version.startsWith('18') ? 1 : 0,
89+
)
8790
})
8891

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

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('../pure').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)