Skip to content

Commit b2a29b1

Browse files
fix: find by timeout with detached screen (#1576)
* fix: improve error findBy* error message when screen is not beign attached. * fix: detect detached screen and display proper error message. * chore: improve error message * chore: update link * chore: update snapshots
1 parent 9b252f8 commit b2a29b1

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import * as React from 'react';
2+
import { View } from 'react-native';
3+
import { render, screen } from '../..';
4+
import { clearRenderResult } from '../../screen';
5+
6+
test('findByTestId detects screen being detached', async () => {
7+
render(<View />);
8+
9+
const promise = screen.findByTestId('not-exists', {}, { timeout: 50 });
10+
11+
// Detach screen
12+
clearRenderResult();
13+
14+
await expect(promise).rejects.toThrowErrorMatchingInlineSnapshot(`
15+
"Unable to find an element with testID: not-exists
16+
17+
Screen is no longer attached. Check your test for "findBy*" or "waitFor" calls that have not been awaited.
18+
19+
We recommend enabling "eslint-plugin-testing-library" to catch these issues at build time:
20+
https://callstack.github.io/react-native-testing-library/docs/getting-started#eslint-plugin"
21+
`);
22+
});

src/queries/make-queries.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ function formatErrorMessage(message: string, printElementTree: boolean) {
8787
return message;
8888
}
8989

90+
if (screen.isDetached) {
91+
return `${message}\n\nScreen is no longer attached. Check your test for "findBy*" or "waitFor" calls that have not been awaited.\n\nWe recommend enabling "eslint-plugin-testing-library" to catch these issues at build time:\nhttps://callstack.github.io/react-native-testing-library/docs/getting-started#eslint-plugin`;
92+
}
93+
9094
const json = screen.toJSON();
9195
if (!json) {
9296
return message;

src/screen.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ const notImplementedDebug = () => {
1212
};
1313
notImplementedDebug.shallow = notImplemented;
1414

15-
const defaultScreen: RenderResult = {
15+
interface Screen extends RenderResult {
16+
isDetached?: boolean;
17+
}
18+
19+
const defaultScreen: Screen = {
20+
isDetached: true,
1621
get root(): ReactTestInstance {
1722
throw new Error(SCREEN_ERROR);
1823
},
@@ -112,10 +117,10 @@ const defaultScreen: RenderResult = {
112117
findAllByText: notImplemented,
113118
};
114119

115-
export let screen: RenderResult = defaultScreen;
120+
export let screen: Screen = defaultScreen;
116121

117-
export function setRenderResult(output: RenderResult) {
118-
screen = output;
122+
export function setRenderResult(renderResult: RenderResult) {
123+
screen = renderResult;
119124
}
120125

121126
export function clearRenderResult() {

0 commit comments

Comments
 (0)