diff --git a/.all-contributorsrc b/.all-contributorsrc
index 270dd6a0..16957ca9 100644
--- a/.all-contributorsrc
+++ b/.all-contributorsrc
@@ -1344,8 +1344,19 @@
"contributions": [
"doc"
]
+ },
+ {
+ "login": "cmdcolin",
+ "name": "Colin Diesh",
+ "avatar_url": "https://avatars.githubusercontent.com/u/6511937?v=4",
+ "profile": "http://cmdcolin.github.io",
+ "contributions": [
+ "doc"
+ ]
}
],
"contributorsPerLine": 7,
- "repoHost": "https://github.com"
+ "repoHost": "https://github.com",
+ "commitType": "docs",
+ "commitConvention": "angular"
}
diff --git a/.github/workflows/validate.yml b/.github/workflows/validate.yml
index 5db8153c..f1359d76 100644
--- a/.github/workflows/validate.yml
+++ b/.github/workflows/validate.yml
@@ -11,7 +11,12 @@ on:
- 'beta'
- 'alpha'
- '!all-contributors/**'
- pull_request: {}
+ pull_request:
+
+concurrency:
+ group: ${{ github.workflow }}-${{ github.ref }}
+ cancel-in-progress: true
+
permissions:
actions: write # to cancel/stop running workflows (styfle/cancel-workflow-action)
contents: read # to fetch code (actions/checkout)
@@ -25,12 +30,9 @@ jobs:
fail-fast: false
matrix:
node: [14, 16, 18]
- react: [latest, next, experimental]
+ react: [latest, canary, experimental]
runs-on: ubuntu-latest
steps:
- - name: π Cancel Previous Runs
- uses: styfle/cancel-workflow-action@0.9.0
-
- name: β¬οΈ Checkout repo
uses: actions/checkout@v2
@@ -73,9 +75,6 @@ jobs:
${{ github.repository == 'testing-library/react-testing-library' &&
github.event_name == 'push' }}
steps:
- - name: π Cancel Previous Runs
- uses: styfle/cancel-workflow-action@0.9.0
-
- name: β¬οΈ Checkout repo
uses: actions/checkout@v2
diff --git a/README.md b/README.md
index 45324901..a3731749 100644
--- a/README.md
+++ b/README.md
@@ -376,9 +376,6 @@ Some included are:
- [`react-router`](https://codesandbox.io/s/github/kentcdodds/react-testing-library-examples/tree/main/?fontsize=14&module=%2Fsrc%2F__tests__%2Freact-router.js&previewwindow=tests)
- [`react-context`](https://codesandbox.io/s/github/kentcdodds/react-testing-library-examples/tree/main/?fontsize=14&module=%2Fsrc%2F__tests__%2Freact-context.js&previewwindow=tests)
-You can also find React Testing Library examples at
-[react-testing-examples.com](https://react-testing-examples.com/jest-rtl/).
-
## Hooks
If you are interested in testing a custom hook, check out [React Hooks Testing
@@ -452,189 +449,189 @@ Thanks goes to these people ([emoji key][emojis]):
@@ -654,7 +651,7 @@ Contributions of any kind welcome!
[npm]: https://www.npmjs.com/
[yarn]: https://classic.yarnpkg.com
[node]: https://nodejs.org
-[build-badge]: https://img.shields.io/github/workflow/status/testing-library/react-testing-library/validate?logo=github&style=flat-square
+[build-badge]: https://img.shields.io/github/actions/workflow/status/testing-library/react-testing-library/validate.yml?branch=main&logo=github
[build]: https://github.com/testing-library/react-testing-library/actions?query=workflow%3Avalidate
[coverage-badge]: https://img.shields.io/codecov/c/github/testing-library/react-testing-library.svg?style=flat-square
[coverage]: https://codecov.io/github/testing-library/react-testing-library
diff --git a/src/__tests__/end-to-end.js b/src/__tests__/end-to-end.js
index 005591d3..f93c23be 100644
--- a/src/__tests__/end-to-end.js
+++ b/src/__tests__/end-to-end.js
@@ -80,7 +80,7 @@ describe.each([
['fake legacy timers', () => jest.useFakeTimers('legacy')],
['fake modern timers', () => jest.useFakeTimers('modern')],
])(
- 'it waits for the data to be loaded in a microtask using %s',
+ 'it waits for the data to be loaded in many microtask using %s',
(label, useTimers) => {
beforeEach(() => {
useTimers()
@@ -162,3 +162,73 @@ describe.each([
})
},
)
+
+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 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().then(data => {
+ setFetchState({todo: data.title, fetching: false})
+ })
+ })
+ }
+ }, [fetchState])
+
+ if (fetchState.fetching) {
+ return Loading..
+ }
+
+ return (
+ Loaded this message: {fetchState.todo}
+ )
+ }
+
+ test('waitForElementToBeRemoved', async () => {
+ render()
+ const loading = () => screen.getByText('Loading..')
+ await waitForElementToBeRemoved(loading)
+ expect(screen.getByTestId('message')).toHaveTextContent(/Hello World/)
+ })
+
+ test('waitFor', async () => {
+ render()
+ await waitFor(() => {
+ screen.getByText('Loading..')
+ })
+ await waitFor(() => {
+ screen.getByText(/Loaded this message:/)
+ })
+ expect(screen.getByTestId('message')).toHaveTextContent(/Hello World/)
+ })
+
+ test('findBy', async () => {
+ render()
+ await expect(screen.findByTestId('message')).resolves.toHaveTextContent(
+ /Hello World/,
+ )
+ })
+ },
+)
diff --git a/src/index.js b/src/index.js
index bb0d0270..26028a9a 100644
--- a/src/index.js
+++ b/src/index.js
@@ -20,6 +20,10 @@ if (typeof process === 'undefined' || !process.env?.RTL_SKIP_AUTO_CLEANUP) {
teardown(() => {
cleanup()
})
+ } else {
+ console.warn(
+ `The current test runner does not support afterEach/teardown hooks. This means we won't be able to run automatic cleanup and you should be calling cleanup() manually.`,
+ )
}
// No test setup with other test runners available
@@ -35,6 +39,10 @@ if (typeof process === 'undefined' || !process.env?.RTL_SKIP_AUTO_CLEANUP) {
afterAll(() => {
setReactActEnvironment(previousIsReactActEnvironment)
})
+ } else {
+ console.warn(
+ 'The current test runner does not support beforeAll/afterAll hooks. This means you should be setting IS_REACT_ACT_ENVIRONMENT manually.',
+ )
}
}