Skip to content

Commit c1878a9

Browse files
authored
test: Ignore React 18 legacy root deprecation warnings (#929)
1 parent c1a931d commit c1878a9

File tree

6 files changed

+69
-26
lines changed

6 files changed

+69
-26
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__/new-act.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
let asyncAct
1+
let asyncAct, consoleErrorMock
22

33
jest.mock('react-dom/test-utils', () => ({
44
act: cb => {
@@ -9,11 +9,11 @@ jest.mock('react-dom/test-utils', () => ({
99
beforeEach(() => {
1010
jest.resetModules()
1111
asyncAct = require('../act-compat').asyncAct
12-
jest.spyOn(console, 'error').mockImplementation(() => {})
12+
consoleErrorMock = jest.spyOn(console, 'error').mockImplementation(() => {})
1313
})
1414

1515
afterEach(() => {
16-
console.error.mockRestore()
16+
consoleErrorMock.mockRestore()
1717
})
1818

1919
test('async act works when it does not exist (older versions of react)', async () => {

src/__tests__/no-act.js

+26-14
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
let act, asyncAct
1+
let act, asyncAct, React, consoleErrorMock
22

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

1011
afterEach(() => {
11-
console.error.mockRestore()
12+
consoleErrorMock.mockRestore()
1213
})
1314

1415
jest.mock('react-dom/test-utils', () => ({}))
@@ -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__/old-act.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
let asyncAct
1+
let asyncAct, consoleErrorMock
22

33
beforeEach(() => {
44
jest.resetModules()
55
asyncAct = require('../act-compat').asyncAct
6-
jest.spyOn(console, 'error').mockImplementation(() => {})
6+
consoleErrorMock = jest.spyOn(console, 'error').mockImplementation(() => {})
77
})
88

99
afterEach(() => {
10-
console.error.mockRestore()
10+
consoleErrorMock.mockRestore()
1111
})
1212

1313
jest.mock('react-dom/test-utils', () => ({

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

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

0 commit comments

Comments
 (0)