Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Ignore React 18 legacy root deprecation warnings #929

Merged
merged 2 commits into from Jun 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 14 additions & 5 deletions src/__tests__/cleanup.js
Expand Up @@ -83,7 +83,10 @@ describe('fake timers and missing act warnings', () => {
expect(microTaskSpy).toHaveBeenCalledTimes(0)
// console.error is mocked
// eslint-disable-next-line no-console
expect(console.error).toHaveBeenCalledTimes(0)
expect(console.error).toHaveBeenCalledTimes(
// ReactDOM.render is deprecated in React 18
React.version.startsWith('18') ? 1 : 0,
)
})

test('cleanup does not swallow missing act warnings', () => {
Expand Down Expand Up @@ -115,10 +118,16 @@ describe('fake timers and missing act warnings', () => {
expect(deferredStateUpdateSpy).toHaveBeenCalledTimes(1)
// console.error is mocked
// eslint-disable-next-line no-console
expect(console.error).toHaveBeenCalledTimes(1)
// eslint-disable-next-line no-console
expect(console.error.mock.calls[0][0]).toMatch(
'a test was not wrapped in act(...)',
expect(console.error).toHaveBeenCalledTimes(
// ReactDOM.render is deprecated in React 18
React.version.startsWith('18') ? 2 : 1,
)
// eslint-disable-next-line no-console
expect(
console.error.mock.calls[
// ReactDOM.render is deprecated in React 18
React.version.startsWith('18') ? 1 : 0
][0],
).toMatch('a test was not wrapped in act(...)')
})
})
6 changes: 3 additions & 3 deletions src/__tests__/new-act.js
@@ -1,4 +1,4 @@
let asyncAct
let asyncAct, consoleErrorMock

jest.mock('react-dom/test-utils', () => ({
act: cb => {
Expand All @@ -9,11 +9,11 @@ jest.mock('react-dom/test-utils', () => ({
beforeEach(() => {
jest.resetModules()
asyncAct = require('../act-compat').asyncAct
jest.spyOn(console, 'error').mockImplementation(() => {})
consoleErrorMock = jest.spyOn(console, 'error').mockImplementation(() => {})
})

afterEach(() => {
console.error.mockRestore()
consoleErrorMock.mockRestore()
})

test('async act works when it does not exist (older versions of react)', async () => {
Expand Down
40 changes: 26 additions & 14 deletions src/__tests__/no-act.js
@@ -1,14 +1,15 @@
let act, asyncAct
let act, asyncAct, React, consoleErrorMock

beforeEach(() => {
jest.resetModules()
act = require('../pure').act
asyncAct = require('../act-compat').asyncAct
jest.spyOn(console, 'error').mockImplementation(() => {})
React = require('react')
consoleErrorMock = jest.spyOn(console, 'error').mockImplementation(() => {})
})

afterEach(() => {
console.error.mockRestore()
consoleErrorMock.mockRestore()
})

jest.mock('react-dom/test-utils', () => ({}))
Expand All @@ -17,7 +18,10 @@ test('act works even when there is no act from test utils', () => {
const callback = jest.fn()
act(callback)
expect(callback).toHaveBeenCalledTimes(1)
expect(console.error).toHaveBeenCalledTimes(0)
expect(console.error).toHaveBeenCalledTimes(
// ReactDOM.render is deprecated in React 18
React.version.startsWith('18') ? 1 : 0,
)
})

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

callback.mockClear()
Expand All @@ -36,7 +43,10 @@ test('async act works when it does not exist (older versions of react)', async (
await Promise.resolve()
await callback()
})
expect(console.error).toHaveBeenCalledTimes(0)
expect(console.error).toHaveBeenCalledTimes(
// ReactDOM.render is deprecated in React 18
React.version.startsWith('18') ? 2 : 0,
)
expect(callback).toHaveBeenCalledTimes(1)
})

Expand All @@ -49,14 +59,16 @@ test('async act recovers from errors', async () => {
} catch (err) {
console.error('call console.error')
}
expect(console.error).toHaveBeenCalledTimes(1)
expect(console.error.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
call console.error,
],
]
`)
expect(console.error).toHaveBeenCalledTimes(
// ReactDOM.render is deprecated in React 18
React.version.startsWith('18') ? 2 : 1,
)
expect(
console.error.mock.calls[
// ReactDOM.render is deprecated in React 18
React.version.startsWith('18') ? 1 : 0
][0],
).toMatch('call console.error')
})

test('async act recovers from sync errors', async () => {
Expand Down
6 changes: 3 additions & 3 deletions src/__tests__/old-act.js
@@ -1,13 +1,13 @@
let asyncAct
let asyncAct, consoleErrorMock

beforeEach(() => {
jest.resetModules()
asyncAct = require('../act-compat').asyncAct
jest.spyOn(console, 'error').mockImplementation(() => {})
consoleErrorMock = jest.spyOn(console, 'error').mockImplementation(() => {})
})

afterEach(() => {
console.error.mockRestore()
consoleErrorMock.mockRestore()
})

jest.mock('react-dom/test-utils', () => ({
Expand Down
5 changes: 4 additions & 1 deletion src/__tests__/stopwatch.js
Expand Up @@ -53,5 +53,8 @@ test('unmounts a component', async () => {
// and get an error.
await sleep(5)
// eslint-disable-next-line no-console
expect(console.error).not.toHaveBeenCalled()
expect(console.error).toHaveBeenCalledTimes(
// ReactDOM.render is deprecated in React 18
React.version.startsWith('18') ? 1 : 0,
)
})
19 changes: 19 additions & 0 deletions tests/setup-env.js
@@ -1 +1,20 @@
import '@testing-library/jest-dom/extend-expect'

let consoleErrorMock

beforeEach(() => {
const originalConsoleError = console.error
consoleErrorMock = jest
.spyOn(console, 'error')
.mockImplementation((message, ...optionalParams) => {
// Ignore ReactDOM.render/ReactDOM.hydrate deprecation warning
if (message.indexOf('Use createRoot instead.') !== -1) {
return
}
originalConsoleError(message, ...optionalParams)
})
})

afterEach(() => {
consoleErrorMock.mockRestore()
})