Skip to content

Commit 20969cb

Browse files
aleclarsonthymikee
authored andcommitted
feat: automatically call "cleanup" after each test
...if an "afterEach" global function is defined, and process.env.RTL_SKIP_AUTO_CLEANUP is falsy. Taken from: https://github.com/testing-library/react-testing-library/blob/14670debd45236d2c5d0a61a83dadc72af1bef7c/src/index.js
1 parent 42cd20e commit 20969cb

File tree

2 files changed

+32
-18
lines changed

2 files changed

+32
-18
lines changed

src/index.js

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
1-
// @flow
2-
import act from './act';
3-
import cleanup from './cleanup';
4-
import debug from './debug';
5-
import fireEvent from './fireEvent';
61
import flushMicrotasksQueue from './flushMicrotasksQueue';
7-
import render from './render';
8-
import shallow from './shallow';
9-
import waitForElement from './waitForElement';
10-
import within from './within';
2+
import { cleanup } from './pure';
113

12-
export { act };
13-
export { cleanup };
14-
export { debug };
15-
export { fireEvent };
16-
export { flushMicrotasksQueue };
17-
export { render };
18-
export { shallow };
19-
export { waitForElement };
20-
export { within };
4+
// if we're running in a test runner that supports afterEach
5+
// then we'll automatically run cleanup afterEach test
6+
// this ensures that tests run in isolation from each other
7+
// if you don't like this then either import the `pure` module
8+
// or set the RTL_SKIP_AUTO_CLEANUP env variable to 'true'.
9+
if (typeof afterEach === 'function' && !process.env.RTL_SKIP_AUTO_CLEANUP) {
10+
afterEach(async () => {
11+
await flushMicrotasksQueue();
12+
cleanup();
13+
});
14+
}
15+
16+
export * from './pure';

src/pure.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// @flow
2+
import act from './act';
3+
import cleanup from './cleanup';
4+
import debug from './debug';
5+
import fireEvent from './fireEvent';
6+
import flushMicrotasksQueue from './flushMicrotasksQueue';
7+
import render from './render';
8+
import shallow from './shallow';
9+
import waitForElement from './waitForElement';
10+
11+
export { act };
12+
export { cleanup };
13+
export { debug };
14+
export { fireEvent };
15+
export { flushMicrotasksQueue };
16+
export { render };
17+
export { shallow };
18+
export { waitForElement };

0 commit comments

Comments
 (0)