Skip to content

Commit d0a0297

Browse files
rolandszokesolkimicreb
authored andcommitted
docs(tests): add instructions for cleanup
1 parent 45bbdab commit d0a0297

3 files changed

Lines changed: 29 additions & 0 deletions

File tree

__tests__/Clock.test.jsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ describe('Clock App', () => {
1212
);
1313

1414
const clearIntervalSpy = sinon.spy(global, 'clearInterval');
15+
/*
16+
Please keep the cleanup in a separate afterAll.
17+
Otherwise, it will lose the scope of the document, and it won't be able to clear the body.
18+
*/
1519
afterAll(cleanup);
1620
afterAll(() => {
1721
clock.restore();

__tests__/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
11
## React Testing Library
22

33
Heads-up: Currently we are using `@testing-library/react/pure` instead of `@testing-library/react` since our tests are not [isolated](https://kentcdodds.com/blog/test-isolation-with-react).
4+
5+
On writing new tests, please keep the `cleanup` functions in __separate__ repeating or one-time functions like `afterEach` or `afterAll`.
6+
Otherwise, it will lose the scope of the `document`, and it won't be able to clear the `body`.
7+
8+
BAD
9+
```js
10+
afterAll(() => {
11+
cleanup();
12+
clock.restore();
13+
clearIntervalSpy.restore();
14+
});
15+
```
16+
17+
GOOD
18+
```js
19+
afterAll(cleanup);
20+
afterAll(() => {
21+
clock.restore();
22+
clearIntervalSpy.restore();
23+
});
24+
```

__tests__/router.test.jsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ import {
1515
} from 'react-router-dom';
1616

1717
describe('withRouter interaction', () => {
18+
/*
19+
Please keep the cleanup in a separate afterEach.
20+
Otherwise, it will lose the scope of the document, and it won't be able to clear the body.
21+
*/
1822
afterEach(cleanup);
1923
afterEach(() => {
2024
window.history.replaceState({}, '', '/');

0 commit comments

Comments
 (0)