Skip to content

Commit 6bf8d24

Browse files
detectHostComponentNames: render test tree inside act to avoid timer leaks (#1371)
* detectHostComponentNames: render test tree inside act to avoid timer leaks * chore: tweaks * refactor: extract common renderWithAct function * chore: tweaks --------- Co-authored-by: Maciej Jastrzebski <[email protected]>
1 parent f79d41d commit 6bf8d24

File tree

3 files changed

+28
-28
lines changed

3 files changed

+28
-28
lines changed

src/helpers/host-component-names.tsx

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import React from 'react';
1+
import * as React from 'react';
22
import { Text, TextInput, View } from 'react-native';
3-
import TestRenderer from 'react-test-renderer';
43
import { configureInternal, getConfig, HostComponentNames } from '../config';
4+
import { renderWithAct } from '../render-act';
55
import { getQueriesForElement } from '../within';
66

77
const userConfigErrorMessage = `There seems to be an issue with your configuration that prevents React Native Testing Library from working correctly.
@@ -29,13 +29,12 @@ export function configureHostComponentNamesIfNeeded() {
2929

3030
function detectHostComponentNames(): HostComponentNames {
3131
try {
32-
const renderer = TestRenderer.create(
32+
const renderer = renderWithAct(
3333
<View>
3434
<Text testID="text">Hello</Text>
3535
<TextInput testID="textInput" />
3636
</View>
3737
);
38-
3938
const { getByTestId } = getQueriesForElement(renderer.root);
4039
const textHostName = getByTestId('text').type;
4140
const textInputHostName = getByTestId('textInput').type;

src/render-act.ts

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import TestRenderer from 'react-test-renderer';
2+
import type {
3+
ReactTestRenderer,
4+
TestRendererOptions,
5+
} from 'react-test-renderer';
6+
7+
export function renderWithAct(
8+
component: React.ReactElement,
9+
options?: TestRendererOptions
10+
): ReactTestRenderer {
11+
let renderer: ReactTestRenderer;
12+
13+
TestRenderer.act(() => {
14+
renderer = TestRenderer.create(component, options);
15+
});
16+
17+
// @ts-ignore act is synchronous, so renderer is already initialised here
18+
return renderer;
19+
}

src/render.tsx

+6-24
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,24 @@
1-
import TestRenderer from 'react-test-renderer';
21
import type { ReactTestInstance, ReactTestRenderer } from 'react-test-renderer';
32
import * as React from 'react';
43
import { Profiler } from 'react';
54
import act from './act';
65
import { addToCleanupQueue } from './cleanup';
7-
import debugShallow from './helpers/debugShallow';
8-
import debugDeep, { DebugOptions } from './helpers/debugDeep';
9-
import { getQueriesForElement } from './within';
10-
import { setRenderResult, screen } from './screen';
11-
import { validateStringsRenderedWithinText } from './helpers/stringValidation';
126
import { getConfig } from './config';
137
import { getHostChildren } from './helpers/component-tree';
8+
import debugDeep, { DebugOptions } from './helpers/debugDeep';
9+
import debugShallow from './helpers/debugShallow';
1410
import { configureHostComponentNamesIfNeeded } from './helpers/host-component-names';
11+
import { validateStringsRenderedWithinText } from './helpers/stringValidation';
12+
import { renderWithAct } from './render-act';
13+
import { setRenderResult, screen } from './screen';
14+
import { getQueriesForElement } from './within';
1515

1616
export type RenderOptions = {
1717
wrapper?: React.ComponentType<any>;
1818
createNodeMock?: (element: React.ReactElement) => any;
1919
unstable_validateStringsRenderedWithinText?: boolean;
2020
};
2121

22-
type TestRendererOptions = {
23-
createNodeMock: (element: React.ReactElement) => any;
24-
};
25-
2622
export type RenderResult = ReturnType<typeof render>;
2723

2824
/**
@@ -129,20 +125,6 @@ function buildRenderResult(
129125
return result;
130126
}
131127

132-
function renderWithAct(
133-
component: React.ReactElement,
134-
options?: TestRendererOptions
135-
): ReactTestRenderer {
136-
let renderer: ReactTestRenderer;
137-
138-
act(() => {
139-
renderer = TestRenderer.create(component, options);
140-
});
141-
142-
// @ts-ignore act is sync, so renderer is always initialised here
143-
return renderer;
144-
}
145-
146128
function updateWithAct(
147129
renderer: ReactTestRenderer,
148130
wrap: (innerElement: React.ReactElement) => React.ReactElement

0 commit comments

Comments
 (0)