From 9da01cb343e8812a04c313e91f9275edfdc591b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Jastrze=CC=A8bski?= Date: Mon, 25 Aug 2025 10:46:17 +0200 Subject: [PATCH 1/5] fix: cleanup async --- src/cleanup.ts | 17 ++++++++++++++--- src/index.ts | 4 ++-- src/pure.ts | 2 +- src/render.tsx | 2 +- 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/cleanup.ts b/src/cleanup.ts index 880bc2eee..3f36870e2 100644 --- a/src/cleanup.ts +++ b/src/cleanup.ts @@ -1,8 +1,9 @@ import { clearRenderResult } from './screen'; type CleanUpFunction = () => void; +type CleanUpFunctionAsync = () => Promise; -const cleanupQueue = new Set(); +const cleanupQueue = new Set(); export default function cleanup() { clearRenderResult(); @@ -11,6 +12,16 @@ export default function cleanup() { cleanupQueue.clear(); } -export function addToCleanupQueue(fn: CleanUpFunction) { - cleanupQueue.add(fn); +export async function cleanupAsync() { + clearRenderResult(); + + for (const fn of cleanupQueue) { + await fn(); + } + + cleanupQueue.clear(); } + +export function addToCleanupQueue(fn: CleanUpFunction | CleanUpFunctionAsync) { + cleanupQueue.add(fn); +} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 426042f94..1ab373257 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,7 +3,7 @@ import './matchers/extend-expect'; import { getIsReactActEnvironment, setReactActEnvironment } from './act'; import { flushMicroTasks } from './flush-micro-tasks'; -import { cleanup } from './pure'; +import { cleanupAsync } from './pure'; if (!process?.env?.RNTL_SKIP_AUTO_CLEANUP) { // If we're running in a test runner that supports afterEach @@ -14,7 +14,7 @@ if (!process?.env?.RNTL_SKIP_AUTO_CLEANUP) { if (typeof afterEach === 'function') { afterEach(async () => { await flushMicroTasks(); - cleanup(); + await cleanupAsync(); }); } diff --git a/src/pure.ts b/src/pure.ts index 6b8483081..c538b3401 100644 --- a/src/pure.ts +++ b/src/pure.ts @@ -1,5 +1,5 @@ export { default as act } from './act'; -export { default as cleanup } from './cleanup'; +export { default as cleanup, cleanupAsync } from './cleanup'; export { default as fireEvent, fireEventAsync } from './fire-event'; export { default as render } from './render'; export { default as renderAsync } from './render-async'; diff --git a/src/render.tsx b/src/render.tsx index d103e13dc..2113fa8cd 100644 --- a/src/render.tsx +++ b/src/render.tsx @@ -124,7 +124,7 @@ function buildRenderResult( }); }; - addToCleanupQueue(unmount); + addToCleanupQueue(unmountAsync); const result = { ...getQueriesForElement(instance), From ec4aa355f3aeecb40403208ec2f65a16743d7de4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Jastrze=CC=A8bski?= Date: Mon, 25 Aug 2025 10:49:09 +0200 Subject: [PATCH 2/5] fix lint --- src/cleanup.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cleanup.ts b/src/cleanup.ts index 3f36870e2..f85524389 100644 --- a/src/cleanup.ts +++ b/src/cleanup.ts @@ -24,4 +24,4 @@ export async function cleanupAsync() { export function addToCleanupQueue(fn: CleanUpFunction | CleanUpFunctionAsync) { cleanupQueue.add(fn); -} \ No newline at end of file +} From 17b146a7b69d9207cc4e80b873fbb2ba9a71b394 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Jastrze=CC=A8bski?= Date: Mon, 25 Aug 2025 10:50:50 +0200 Subject: [PATCH 3/5] fix tests --- src/__tests__/cleanup.test.tsx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/__tests__/cleanup.test.tsx b/src/__tests__/cleanup.test.tsx index 5de42b214..3c5b54409 100644 --- a/src/__tests__/cleanup.test.tsx +++ b/src/__tests__/cleanup.test.tsx @@ -1,7 +1,7 @@ import * as React from 'react'; import { View } from 'react-native'; -import { cleanup, render } from '../pure'; +import { cleanupAsync, render } from '../pure'; class Test extends React.Component<{ onUnmount: () => void }> { componentWillUnmount() { @@ -14,13 +14,13 @@ class Test extends React.Component<{ onUnmount: () => void }> { } } -test('cleanup', () => { +test('cleanup', async () => { const fn = jest.fn(); render(); render(); expect(fn).not.toHaveBeenCalled(); - cleanup(); + await cleanupAsync(); expect(fn).toHaveBeenCalledTimes(2); }); From 7c5f51f7a72d214de8e1ad52966feecb28e1f8a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Jastrze=CC=A8bski?= Date: Mon, 25 Aug 2025 10:51:40 +0200 Subject: [PATCH 4/5] more tests --- src/__tests__/cleanup.test.tsx | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/__tests__/cleanup.test.tsx b/src/__tests__/cleanup.test.tsx index 3c5b54409..a687ec8b0 100644 --- a/src/__tests__/cleanup.test.tsx +++ b/src/__tests__/cleanup.test.tsx @@ -1,7 +1,7 @@ import * as React from 'react'; import { View } from 'react-native'; -import { cleanupAsync, render } from '../pure'; +import { cleanupAsync, render, renderAsync } from '../pure'; class Test extends React.Component<{ onUnmount: () => void }> { componentWillUnmount() { @@ -14,7 +14,7 @@ class Test extends React.Component<{ onUnmount: () => void }> { } } -test('cleanup', async () => { +test('cleanup after render', async () => { const fn = jest.fn(); render(); @@ -24,3 +24,14 @@ test('cleanup', async () => { await cleanupAsync(); expect(fn).toHaveBeenCalledTimes(2); }); + +test('cleanup after renderAsync', async () => { + const fn = jest.fn(); + + await renderAsync(); + await renderAsync(); + expect(fn).not.toHaveBeenCalled(); + + await cleanupAsync(); + expect(fn).toHaveBeenCalledTimes(2); +}); \ No newline at end of file From 413e200290a2f76704e2e285a27069afa0a5af91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Jastrze=CC=A8bski?= Date: Mon, 25 Aug 2025 10:53:19 +0200 Subject: [PATCH 5/5] . --- src/__tests__/cleanup.test.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/__tests__/cleanup.test.tsx b/src/__tests__/cleanup.test.tsx index a687ec8b0..e09f3ce9b 100644 --- a/src/__tests__/cleanup.test.tsx +++ b/src/__tests__/cleanup.test.tsx @@ -34,4 +34,4 @@ test('cleanup after renderAsync', async () => { await cleanupAsync(); expect(fn).toHaveBeenCalledTimes(2); -}); \ No newline at end of file +});