Skip to content

Commit 0df0127

Browse files
committed
only restore the mock that was created in the same scope
1 parent 545a35e commit 0df0127

File tree

4 files changed

+13
-14
lines changed

4 files changed

+13
-14
lines changed

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

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

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

1111
afterEach(() => {
12-
console.error.mockRestore()
12+
consoleErrorMock.mockRestore()
1313
})
1414

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

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', () => ({

tests/setup-env.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import '@testing-library/jest-dom/extend-expect'
22

3+
let consoleErrorMock
4+
35
beforeEach(() => {
46
const originalConsoleError = console.error
5-
jest
7+
consoleErrorMock = jest
68
.spyOn(console, 'error')
79
.mockImplementation((message, ...optionalParams) => {
810
// Ignore ReactDOM.render/ReactDOM.hydrate deprecation warning
@@ -14,8 +16,5 @@ beforeEach(() => {
1416
})
1517

1618
afterEach(() => {
17-
// maybe another test already restore console error mocks
18-
if (typeof console.error.mockRestore === 'function') {
19-
console.error.mockRestore()
20-
}
19+
consoleErrorMock.mockRestore()
2120
})

0 commit comments

Comments
 (0)