Skip to content

Commit 9da01cb

Browse files
committed
fix: cleanup async
1 parent 2015fb8 commit 9da01cb

File tree

4 files changed

+18
-7
lines changed

4 files changed

+18
-7
lines changed

src/cleanup.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { clearRenderResult } from './screen';
22

33
type CleanUpFunction = () => void;
4+
type CleanUpFunctionAsync = () => Promise<void>;
45

5-
const cleanupQueue = new Set<CleanUpFunction>();
6+
const cleanupQueue = new Set<CleanUpFunction | CleanUpFunctionAsync>();
67

78
export default function cleanup() {
89
clearRenderResult();
@@ -11,6 +12,16 @@ export default function cleanup() {
1112
cleanupQueue.clear();
1213
}
1314

14-
export function addToCleanupQueue(fn: CleanUpFunction) {
15-
cleanupQueue.add(fn);
15+
export async function cleanupAsync() {
16+
clearRenderResult();
17+
18+
for (const fn of cleanupQueue) {
19+
await fn();
20+
}
21+
22+
cleanupQueue.clear();
1623
}
24+
25+
export function addToCleanupQueue(fn: CleanUpFunction | CleanUpFunctionAsync) {
26+
cleanupQueue.add(fn);
27+
}

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import './matchers/extend-expect';
33

44
import { getIsReactActEnvironment, setReactActEnvironment } from './act';
55
import { flushMicroTasks } from './flush-micro-tasks';
6-
import { cleanup } from './pure';
6+
import { cleanupAsync } from './pure';
77

88
if (!process?.env?.RNTL_SKIP_AUTO_CLEANUP) {
99
// If we're running in a test runner that supports afterEach
@@ -14,7 +14,7 @@ if (!process?.env?.RNTL_SKIP_AUTO_CLEANUP) {
1414
if (typeof afterEach === 'function') {
1515
afterEach(async () => {
1616
await flushMicroTasks();
17-
cleanup();
17+
await cleanupAsync();
1818
});
1919
}
2020

src/pure.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export { default as act } from './act';
2-
export { default as cleanup } from './cleanup';
2+
export { default as cleanup, cleanupAsync } from './cleanup';
33
export { default as fireEvent, fireEventAsync } from './fire-event';
44
export { default as render } from './render';
55
export { default as renderAsync } from './render-async';

src/render.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ function buildRenderResult(
124124
});
125125
};
126126

127-
addToCleanupQueue(unmount);
127+
addToCleanupQueue(unmountAsync);
128128

129129
const result = {
130130
...getQueriesForElement(instance),

0 commit comments

Comments
 (0)