Skip to content

Commit c419a70

Browse files
fix: cleanup async (#1817)
1 parent 9d89ea9 commit c419a70

File tree

5 files changed

+31
-9
lines changed

5 files changed

+31
-9
lines changed

src/__tests__/cleanup.test.tsx

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as React from 'react';
22
import { View } from 'react-native';
33

4-
import { cleanup, render } from '../pure';
4+
import { cleanupAsync, render, renderAsync } from '../pure';
55

66
class Test extends React.Component<{ onUnmount: () => void }> {
77
componentWillUnmount() {
@@ -14,13 +14,24 @@ class Test extends React.Component<{ onUnmount: () => void }> {
1414
}
1515
}
1616

17-
test('cleanup', () => {
17+
test('cleanup after render', async () => {
1818
const fn = jest.fn();
1919

2020
render(<Test onUnmount={fn} />);
2121
render(<Test onUnmount={fn} />);
2222
expect(fn).not.toHaveBeenCalled();
2323

24-
cleanup();
24+
await cleanupAsync();
25+
expect(fn).toHaveBeenCalledTimes(2);
26+
});
27+
28+
test('cleanup after renderAsync', async () => {
29+
const fn = jest.fn();
30+
31+
await renderAsync(<Test onUnmount={fn} />);
32+
await renderAsync(<Test onUnmount={fn} />);
33+
expect(fn).not.toHaveBeenCalled();
34+
35+
await cleanupAsync();
2536
expect(fn).toHaveBeenCalledTimes(2);
2637
});

src/cleanup.ts

Lines changed: 13 additions & 2 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+
export async function cleanupAsync() {
16+
clearRenderResult();
17+
18+
for (const fn of cleanupQueue) {
19+
await fn();
20+
}
21+
22+
cleanupQueue.clear();
23+
}
24+
25+
export function addToCleanupQueue(fn: CleanUpFunction | CleanUpFunctionAsync) {
1526
cleanupQueue.add(fn);
1627
}

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)