Skip to content

Commit

Permalink
only restore the mock that was created in the same scope
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon committed Jun 12, 2021
1 parent 545a35e commit 0df0127
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 14 deletions.
6 changes: 3 additions & 3 deletions src/__tests__/new-act.js
Original file line number Diff line number Diff line change
@@ -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
6 changes: 3 additions & 3 deletions src/__tests__/no-act.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
let act, asyncAct, React
let act, asyncAct, React, consoleErrorMock

beforeEach(() => {
jest.resetModules()
act = require('../pure').act
asyncAct = require('../act-compat').asyncAct
React = require('react')
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
6 changes: 3 additions & 3 deletions src/__tests__/old-act.js
Original file line number Diff line number Diff line change
@@ -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
9 changes: 4 additions & 5 deletions tests/setup-env.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import '@testing-library/jest-dom/extend-expect'

let consoleErrorMock

beforeEach(() => {
const originalConsoleError = console.error
jest
consoleErrorMock = jest
.spyOn(console, 'error')
.mockImplementation((message, ...optionalParams) => {
// Ignore ReactDOM.render/ReactDOM.hydrate deprecation warning
Expand All @@ -14,8 +16,5 @@ beforeEach(() => {
})

afterEach(() => {
// maybe another test already restore console error mocks
if (typeof console.error.mockRestore === 'function') {
console.error.mockRestore()
}
consoleErrorMock.mockRestore()
})

0 comments on commit 0df0127

Please sign in to comment.