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

release: 14.0.0 #1178

Merged
merged 5 commits into from
Feb 16, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .codesandbox/ci.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"installCommand": "install:csb",
"sandboxes": ["new", "github/kentcdodds/react-testing-library-examples"],
"node": "12"
"node": "14"
}
3 changes: 2 additions & 1 deletion .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
strategy:
fail-fast: false
matrix:
node: [12, 14, 16, 18]
node: [14, 16, 18]
react: [latest, next, experimental]
runs-on: ubuntu-latest
steps:
Expand Down Expand Up @@ -65,6 +65,7 @@ jobs:
permissions:
actions: write # to cancel/stop running workflows (styfle/cancel-workflow-action)
contents: write # to create release tags (cycjimmy/semantic-release-action)
issues: write # to post release that resolves an issue (cycjimmy/semantic-release-action)

needs: main
runs-on: ubuntu-latest
Expand Down
14 changes: 10 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"types": "types/index.d.ts",
"module": "dist/@testing-library/react.esm.js",
"engines": {
"node": ">=12"
"node": ">=14"
},
"scripts": {
"prebuild": "rimraf dist",
Expand Down Expand Up @@ -46,15 +46,15 @@
"license": "MIT",
"dependencies": {
"@babel/runtime": "^7.12.5",
"@testing-library/dom": "^8.5.0",
"@testing-library/dom": "^9.0.0",
"@types/react-dom": "^18.0.0"
},
"devDependencies": {
"@testing-library/jest-dom": "^5.11.6",
"chalk": "^4.1.2",
"dotenv-cli": "^4.0.0",
"jest-diff": "^27.5.1",
"kcd-scripts": "^11.1.0",
"jest-diff": "^29.4.1",
"kcd-scripts": "^13.0.0",
"npm-run-all": "^4.1.5",
"react": "^18.0.0",
"react-dom": "^18.0.0",
Expand All @@ -67,6 +67,9 @@
},
"eslintConfig": {
"extends": "./node_modules/kcd-scripts/eslint.js",
"parserOptions": {
"ecmaVersion": 2022
},
"globals": {
"globalThis": "readonly"
},
Expand All @@ -76,8 +79,11 @@
"import/no-unassigned-import": "off",
"import/named": "off",
"testing-library/no-container": "off",
"testing-library/no-debugging-utils": "off",
"testing-library/no-dom-import": "off",
"testing-library/no-unnecessary-act": "off",
"testing-library/prefer-explicit-assert": "off",
"testing-library/prefer-find-by": "off",
"testing-library/prefer-user-event": "off"
}
},
Expand Down
3 changes: 2 additions & 1 deletion src/__tests__/cleanup.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe('fake timers and missing act warnings', () => {
let cancelled = false
Promise.resolve().then(() => {
microTaskSpy()
// eslint-disable-next-line jest/no-if -- false positive
// eslint-disable-next-line jest/no-if, jest/no-conditional-in-test -- false positive
if (!cancelled) {
setDeferredCounter(counter)
}
Expand Down Expand Up @@ -96,6 +96,7 @@ describe('fake timers and missing act warnings', () => {
let cancelled = false
setTimeout(() => {
deferredStateUpdateSpy()
// eslint-disable-next-line jest/no-conditional-in-test -- false-positive
if (!cancelled) {
setDeferredCounter(counter)
}
Expand Down
3 changes: 1 addition & 2 deletions src/__tests__/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ test('allows same arguments as prettyDOM', () => {
debug(container, 6, {highlight: false})
expect(console.log).toHaveBeenCalledTimes(1)
expect(console.log.mock.calls[0]).toMatchInlineSnapshot(`
Array [
[
<div>
...,
]
Expand All @@ -52,5 +52,4 @@ test('allows same arguments as prettyDOM', () => {
/*
eslint
no-console: "off",
testing-library/no-debug: "off",
*/
211 changes: 151 additions & 60 deletions src/__tests__/end-to-end.js
Original file line number Diff line number Diff line change
@@ -1,73 +1,164 @@
import * as React from 'react'
import {render, waitForElementToBeRemoved, screen, waitFor} from '../'

const fetchAMessage = () =>
new Promise(resolve => {
// we are using random timeout here to simulate a real-time example
// of an async operation calling a callback at a non-deterministic time
const randomTimeout = Math.floor(Math.random() * 100)
setTimeout(() => {
resolve({returnedMessage: 'Hello World'})
}, randomTimeout)
})

function ComponentWithLoader() {
const [state, setState] = React.useState({data: undefined, loading: true})
React.useEffect(() => {
let cancelled = false
fetchAMessage().then(data => {
if (!cancelled) {
setState({data, loading: false})
}
describe.each([
['real timers', () => jest.useRealTimers()],
['fake legacy timers', () => jest.useFakeTimers('legacy')],
['fake modern timers', () => jest.useFakeTimers('modern')],
])(
'it waits for the data to be loaded in a macrotask using %s',
(label, useTimers) => {
beforeEach(() => {
useTimers()
})

afterEach(() => {
jest.useRealTimers()
})

return () => {
cancelled = true
const fetchAMessageInAMacrotask = () =>
new Promise(resolve => {
// we are using random timeout here to simulate a real-time example
// of an async operation calling a callback at a non-deterministic time
const randomTimeout = Math.floor(Math.random() * 100)
setTimeout(() => {
resolve({returnedMessage: 'Hello World'})
}, randomTimeout)
})

function ComponentWithMacrotaskLoader() {
const [state, setState] = React.useState({data: undefined, loading: true})
React.useEffect(() => {
let cancelled = false
fetchAMessageInAMacrotask().then(data => {
if (!cancelled) {
setState({data, loading: false})
}
})

return () => {
cancelled = true
}
}, [])

if (state.loading) {
return <div>Loading...</div>
}

return (
<div data-testid="message">
Loaded this message: {state.data.returnedMessage}!
</div>
)
}
}, [])

if (state.loading) {
return <div>Loading...</div>
}
test('waitForElementToBeRemoved', async () => {
render(<ComponentWithMacrotaskLoader />)
const loading = () => screen.getByText('Loading...')
await waitForElementToBeRemoved(loading)
expect(screen.getByTestId('message')).toHaveTextContent(/Hello World/)
})

test('waitFor', async () => {
render(<ComponentWithMacrotaskLoader />)
await waitFor(() => screen.getByText(/Loading../))
await waitFor(() => screen.getByText(/Loaded this message:/))
expect(screen.getByTestId('message')).toHaveTextContent(/Hello World/)
})

return (
<div data-testid="message">
Loaded this message: {state.data.returnedMessage}!
</div>
)
}
test('findBy', async () => {
render(<ComponentWithMacrotaskLoader />)
await expect(screen.findByTestId('message')).resolves.toHaveTextContent(
/Hello World/,
)
})
},
)

describe.each([
['real timers', () => jest.useRealTimers()],
['fake legacy timers', () => jest.useFakeTimers('legacy')],
['fake modern timers', () => jest.useFakeTimers('modern')],
])('it waits for the data to be loaded using %s', (label, useTimers) => {
beforeEach(() => {
useTimers()
})

afterEach(() => {
jest.useRealTimers()
})

test('waitForElementToBeRemoved', async () => {
render(<ComponentWithLoader />)
const loading = () => screen.getByText('Loading...')
await waitForElementToBeRemoved(loading)
expect(screen.getByTestId('message')).toHaveTextContent(/Hello World/)
})

test('waitFor', async () => {
render(<ComponentWithLoader />)
const message = () => screen.getByText(/Loaded this message:/)
await waitFor(message)
expect(screen.getByTestId('message')).toHaveTextContent(/Hello World/)
})

test('findBy', async () => {
render(<ComponentWithLoader />)
await expect(screen.findByTestId('message')).resolves.toHaveTextContent(
/Hello World/,
)
})
})
])(
'it waits for the data to be loaded in a microtask using %s',
(label, useTimers) => {
beforeEach(() => {
useTimers()
})

afterEach(() => {
jest.useRealTimers()
})

const fetchAMessageInAMicrotask = () =>
Promise.resolve({
status: 200,
json: () => Promise.resolve({title: 'Hello World'}),
})

function ComponentWithMicrotaskLoader() {
const [fetchState, setFetchState] = React.useState({fetching: true})

React.useEffect(() => {
if (fetchState.fetching) {
fetchAMessageInAMicrotask().then(res => {
return (
res
.json()
// By spec, the runtime can only yield back to the event loop once
// the microtask queue is empty.
// So we ensure that we actually wait for that as well before yielding back from `waitFor`.
.then(data => data)
.then(data => data)
.then(data => data)
.then(data => data)
.then(data => data)
.then(data => data)
.then(data => data)
.then(data => data)
.then(data => data)
.then(data => data)
.then(data => data)
.then(data => {
setFetchState({todo: data.title, fetching: false})
})
)
})
}
}, [fetchState])

if (fetchState.fetching) {
return <p>Loading..</p>
}

return (
<div data-testid="message">Loaded this message: {fetchState.todo}</div>
)
}

test('waitForElementToBeRemoved', async () => {
render(<ComponentWithMicrotaskLoader />)
const loading = () => screen.getByText('Loading..')
await waitForElementToBeRemoved(loading)
expect(screen.getByTestId('message')).toHaveTextContent(/Hello World/)
})

test('waitFor', async () => {
render(<ComponentWithMicrotaskLoader />)
await waitFor(() => {
screen.getByText('Loading..')
})
await waitFor(() => {
screen.getByText(/Loaded this message:/)
})
expect(screen.getByTestId('message')).toHaveTextContent(/Hello World/)
})

test('findBy', async () => {
render(<ComponentWithMicrotaskLoader />)
await expect(screen.findByTestId('message')).resolves.toHaveTextContent(
/Hello World/,
)
})
},
)
8 changes: 4 additions & 4 deletions src/__tests__/new-act.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ test('async act recovers from errors', async () => {
}
expect(console.error).toHaveBeenCalledTimes(1)
expect(console.error.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
[
[
call console.error,
],
]
Expand All @@ -65,8 +65,8 @@ test('async act recovers from sync errors', async () => {
}
expect(console.error).toHaveBeenCalledTimes(1)
expect(console.error.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
[
[
call console.error,
],
]
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/renderHook.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ test('allows rerendering', () => {
const [left, setLeft] = React.useState('left')
const [right, setRight] = React.useState('right')

// eslint-disable-next-line jest/no-if
// eslint-disable-next-line jest/no-if, jest/no-conditional-in-test -- false-positive
switch (branch) {
case 'left':
return [left, setLeft]
Expand Down
Loading