Skip to content

Commit

Permalink
Update tests asserting on console.error
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon committed Jun 12, 2021
1 parent e44adc5 commit 8216ea4
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 18 deletions.
19 changes: 14 additions & 5 deletions src/__tests__/cleanup.js
Expand Up @@ -82,7 +82,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 @@ -114,10 +117,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(...)')
})
})
36 changes: 24 additions & 12 deletions src/__tests__/no-act.js
@@ -1,9 +1,10 @@
let act, asyncAct
let act, asyncAct, React

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

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 : 1,
)
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 : 1,
)
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
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,
)
})

0 comments on commit 8216ea4

Please sign in to comment.