Skip to content
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
7 changes: 5 additions & 2 deletions __tests__/Clock.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ describe('Clock App', () => {
);

const clearIntervalSpy = sinon.spy(global, 'clearInterval');

/*
Please keep the cleanup in a separate afterAll.
Otherwise, it will lose the scope of the document, and it won't be able to clear the body.
*/
afterAll(cleanup);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity? Why was this afterAll split required?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure, but @testing-library/dom changed, and If you do not add the cleanup directly to the afterAll or afterEach, but run it in a function (it does not matter if it is an arrow function or not) the cleanup will lose the scope of the document so it is unable to find the body and clean it up, and the function breaks.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, we could have our tests isolated so we would not need to use cleanup at all. Maybe I should start working on it.

afterAll(() => {
cleanup();
clock.restore();
clearIntervalSpy.restore();
});
Expand Down
21 changes: 21 additions & 0 deletions __tests__/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
## React Testing Library

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).

On writing new tests, please keep the `cleanup` functions in __separate__ repeating or one-time functions like `afterEach` or `afterAll`.
Otherwise, it will lose the scope of the `document`, and it won't be able to clear the `body`.

BAD
```js
afterAll(() => {
cleanup();
clock.restore();
clearIntervalSpy.restore();
});
```

GOOD
```js
afterAll(cleanup);
afterAll(() => {
clock.restore();
clearIntervalSpy.restore();
});
```
6 changes: 5 additions & 1 deletion __tests__/router.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ import {
} from 'react-router-dom';

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

Expand Down
4 changes: 1 addition & 3 deletions __tests__/styled.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ describe('withRouter interaction', () => {
<ThemeProvider theme={theme}>{children}</ThemeProvider>
);

afterEach(() => {
cleanup();
});
afterEach(cleanup);

describe('function components', () => {
test('should be reactive with withTheme(view(Comp))', () => {
Expand Down
1,352 changes: 798 additions & 554 deletions examples/beer-finder/package-lock.json

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions examples/beer-finder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"@material-ui/core": "^4.9.2",
"@material-ui/core": "^4.9.5",
"@material-ui/icons": "^4.9.1",
"axios": "^0.19.2",
"material-ui-search-bar": "1.0.0-beta.13",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react": "^16.13.0",
"react-dom": "^16.13.0",
"react-easy-state": "^6.1.3",
"react-scripts": "3.3.1"
"react-scripts": "3.4.0"
},
"scripts": {
"start": "react-scripts start",
Expand Down
1,170 changes: 698 additions & 472 deletions examples/clock/package-lock.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions examples/clock/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
"private": true,
"dependencies": {
"moment": "^2.24.0",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react": "^16.13.0",
"react-dom": "^16.13.0",
"react-easy-state": "^6.1.3",
"react-scripts": "3.3.1"
"react-scripts": "3.4.0"
},
"scripts": {
"start": "react-scripts start",
Expand Down
Loading