Skip to content
Permalink

Comparing changes

This is a direct comparison between two commits made in this repository or its related repositories. View the default comparison for this range or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: testing-library/react-testing-library
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 0a52d494b781afad6eb231bb6a68c447eafe261d
Choose a base ref
..
head repository: testing-library/react-testing-library
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: f57072204ca534137c4c357ce1de525486c09d7e
Choose a head ref
Showing with 75 additions and 30 deletions.
  1. +2 −1 .codesandbox/ci.json
  2. +1 −1 .github/workflows/validate.yml
  3. +2 −2 package.json
  4. +14 −5 src/__tests__/cleanup.js
  5. +3 −3 src/__tests__/new-act.js
  6. +26 −14 src/__tests__/no-act.js
  7. +3 −3 src/__tests__/old-act.js
  8. +4 −1 src/__tests__/stopwatch.js
  9. +20 −0 tests/setup-env.js
3 changes: 2 additions & 1 deletion .codesandbox/ci.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"sandboxes": ["new", "github/kentcdodds/react-testing-library-examples"]
"sandboxes": ["new", "github/kentcdodds/react-testing-library-examples"],
"node": "12"
}
2 changes: 1 addition & 1 deletion .github/workflows/validate.yml
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@ jobs:
strategy:
fail-fast: false
matrix:
node: [10.13, 12, 14, 16]
node: [12, 14, 16]
react: [latest, next, experimental]
runs-on: ubuntu-latest
steps:
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@
"types": "types/index.d.ts",
"module": "dist/@testing-library/react.esm.js",
"engines": {
"node": ">=10"
"node": ">=12"
},
"scripts": {
"prebuild": "rimraf dist",
@@ -44,7 +44,7 @@
"license": "MIT",
"dependencies": {
"@babel/runtime": "^7.12.5",
"@testing-library/dom": "^8.0.0-alpha.3"
"@testing-library/dom": "^8.0.0-alpha.6"
},
"devDependencies": {
"@testing-library/jest-dom": "^5.11.6",
19 changes: 14 additions & 5 deletions src/__tests__/cleanup.js
Original file line number Diff line number Diff line change
@@ -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', () => {
@@ -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
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 => {
@@ -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 () => {
40 changes: 26 additions & 14 deletions src/__tests__/no-act.js
Original file line number Diff line number Diff line change
@@ -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', () => ({}))
@@ -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 () => {
@@ -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()
@@ -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)
})

@@ -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 () => {
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', () => ({
5 changes: 4 additions & 1 deletion src/__tests__/stopwatch.js
Original file line number Diff line number Diff line change
@@ -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,
)
})
20 changes: 20 additions & 0 deletions tests/setup-env.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
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()
})

// TODO: Can be removed in a future React release: https://github.com/reactwg/react-18/discussions/23#discussioncomment-798952
// eslint-disable-next-line import/no-extraneous-dependencies -- need the version from React not an explicitly declared one
jest.mock('scheduler', () => require('scheduler/unstable_mock'))