Skip to content

Commit

Permalink
fix: find by timeout with detached screen (#1576)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
mdjastrzebski committed Mar 28, 2024
1 parent 9b252f8 commit b2a29b1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
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

0 comments on commit b2a29b1

Please sign in to comment.