Skip to content

fix: find by timeout with detached screen #1576

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/queries/__tests__/find-by.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import * as React from 'react';
import { View } from 'react-native';
import { render, screen } from '../..';
import { clearRenderResult } from '../../screen';

test('findByTestId detects screen being detached', async () => {
render(<View />);

const promise = screen.findByTestId('not-exists', {}, { timeout: 50 });

// Detach screen
clearRenderResult();

await expect(promise).rejects.toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with testID: not-exists

Screen is no longer attached. Check your test for "findBy*" or "waitFor" calls that have not been awaited.

We recommend enabling "eslint-plugin-testing-library" to catch these issues at build time:
https://callstack.github.io/react-native-testing-library/docs/getting-started#eslint-plugin"
`);
});
4 changes: 4 additions & 0 deletions src/queries/make-queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ function formatErrorMessage(message: string, printElementTree: boolean) {
return message;
}

if (screen.isDetached) {
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`;
}

const json = screen.toJSON();
if (!json) {
return message;
Expand Down
13 changes: 9 additions & 4 deletions src/screen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ const notImplementedDebug = () => {
};
notImplementedDebug.shallow = notImplemented;

const defaultScreen: RenderResult = {
interface Screen extends RenderResult {
isDetached?: boolean;
}

const defaultScreen: Screen = {
isDetached: true,
get root(): ReactTestInstance {
throw new Error(SCREEN_ERROR);
},
Expand Down Expand Up @@ -112,10 +117,10 @@ const defaultScreen: RenderResult = {
findAllByText: notImplemented,
};

export let screen: RenderResult = defaultScreen;
export let screen: Screen = defaultScreen;

export function setRenderResult(output: RenderResult) {
screen = output;
export function setRenderResult(renderResult: RenderResult) {
screen = renderResult;
}

export function clearRenderResult() {
Expand Down