Skip to content

Commit 55aa167

Browse files
author
Kent C. Dodds
committed
feat(cleanup): automatically cleanup if afterEach is detected
You can disable this with the RTL_SKIP_CLEANUP environment variable if you so choose, but it's recommended to have cleanup work this way. Closes #428
1 parent 98b9605 commit 55aa167

12 files changed

+35
-32
lines changed

cleanup-after-each.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
afterEach(() => {
2-
return require('./dist/cleanup-async')()
3-
})
1+
afterEach(() => require('./dist/cleanup-async').default())

src/__tests__/act.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import React from 'react'
2-
import {render, cleanup, fireEvent} from '../'
3-
4-
afterEach(cleanup)
2+
import {render, fireEvent} from '../'
53

64
test('render calls useEffect immediately', () => {
75
const effectCb = jest.fn()

src/__tests__/auto-cleanup.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import React from 'react'
2+
import {render} from '../'
3+
4+
// This just verifies that by importing RTL in an
5+
// environment which supports afterEach (like jests)
6+
// we'll get automatic cleanup between tests.
7+
test('first', () => {
8+
render(<div>hi</div>)
9+
})
10+
11+
test('second', () => {
12+
expect(document.body.innerHTML).toEqual('')
13+
})

src/__tests__/cleanup-after-each.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react'
2-
import {render} from '../index'
2+
import {render} from '../'
33
import cleanupAsync from '../cleanup-async'
44

55
afterEach(() => {
@@ -20,13 +20,13 @@ function App() {
2020
return 123
2121
}
2222

23-
it('cleanup-after-each does not leave any hanging microtasks: part 1', () => {
23+
test('cleanup-after-each does not leave any hanging microtasks: part 1', () => {
2424
render(<App />)
2525
expect(document.body.textContent).toBe('123')
2626
expect(log).toEqual([])
2727
})
2828

29-
it('cleanup-after-each does not leave any hanging microtasks: part 2', () => {
29+
test('cleanup-after-each does not leave any hanging microtasks: part 2', () => {
3030
expect(log).toEqual([0])
3131
expect(document.body.innerHTML).toBe('')
3232
})

src/__tests__/debug.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import React from 'react'
2-
import {render, cleanup} from '../'
2+
import {render} from '../'
33

44
beforeEach(() => {
55
jest.spyOn(console, 'log').mockImplementation(() => {})
66
})
77

88
afterEach(() => {
9-
cleanup()
109
console.log.mockRestore()
1110
})
1211

src/__tests__/end-to-end.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import React from 'react'
2-
import {render, wait, cleanup} from '../'
3-
4-
afterEach(cleanup)
2+
import {render, wait} from '../'
53

64
const fetchAMessage = () =>
75
new Promise(resolve => {

src/__tests__/events.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react'
2-
import {render, cleanup, fireEvent} from '../'
2+
import {render, fireEvent} from '../'
33

44
const eventTypes = [
55
{
@@ -128,8 +128,6 @@ const eventTypes = [
128128
},
129129
]
130130

131-
afterEach(cleanup)
132-
133131
eventTypes.forEach(({type, events, elementType, init}) => {
134132
describe(`${type} Events`, () => {
135133
events.forEach(eventName => {

src/__tests__/multi-base.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react'
2-
import {render, cleanup} from '../'
2+
import {render} from '../'
33

44
// these are created once per test suite and reused for each case
55
let treeA, treeB
@@ -15,8 +15,6 @@ afterAll(() => {
1515
treeB.parentNode.removeChild(treeB)
1616
})
1717

18-
afterEach(cleanup)
19-
2018
test('baseElement isolates trees from one another', () => {
2119
const {getByText: getByTextInA} = render(<div>Jekyll</div>, {
2220
baseElement: treeA,

src/__tests__/render.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ test('returns baseElement which defaults to document.body', () => {
5252
expect(baseElement).toBe(document.body)
5353
})
5454

55-
it('cleansup document', () => {
55+
test('cleanup cleans up document', () => {
5656
const spy = jest.fn()
5757
const divId = 'my-div'
5858

@@ -73,7 +73,7 @@ it('cleansup document', () => {
7373
expect(spy).toHaveBeenCalledTimes(1)
7474
})
7575

76-
it('supports fragments', () => {
76+
test('supports fragments', () => {
7777
class Test extends React.Component {
7878
render() {
7979
return (
@@ -86,8 +86,6 @@ it('supports fragments', () => {
8686

8787
const {asFragment} = render(<Test />)
8888
expect(asFragment()).toMatchSnapshot()
89-
cleanup()
90-
expect(document.body.innerHTML).toBe('')
9189
})
9290

9391
test('renders options.wrapper around node', () => {

src/__tests__/stopwatch.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import React from 'react'
2-
import {render, cleanup, fireEvent} from '../'
3-
4-
afterEach(cleanup)
2+
import {render, fireEvent} from '../'
53

64
class StopWatch extends React.Component {
75
state = {lapse: 0, running: false}

src/index.js

+10
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,16 @@ fireEvent.select = (node, init) => {
142142
fireEvent.keyUp(node, init)
143143
}
144144

145+
// if we're running in a test runner that supports afterEach
146+
// then we'll automatically run cleanup afterEach test
147+
// this ensures that tests run in isolation from each other
148+
if (typeof afterEach === 'function' && !process.env.RTL_SKIP_CLEANUP) {
149+
afterEach(async () => {
150+
await asyncAct(async () => {})
151+
cleanup()
152+
})
153+
}
154+
145155
// just re-export everything from dom-testing-library
146156
export * from '@testing-library/dom'
147157
export {render, cleanup, fireEvent, act}

tests/setup-env.js

-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1 @@
11
import '@testing-library/jest-dom/extend-expect'
2-
3-
afterEach(() => {
4-
// have to do a dynamic import so we don't mess up jest mocking for old-act.js
5-
require('../src').cleanup()
6-
})

0 commit comments

Comments
 (0)