Skip to content
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

Incorrect automatic cleanup order #1565

Closed
alpha0010 opened this issue Feb 15, 2024 · 2 comments
Closed

Incorrect automatic cleanup order #1565

alpha0010 opened this issue Feb 15, 2024 · 2 comments

Comments

@alpha0010
Copy link

Describe the bug

While running tests, I get:

ReferenceError: You are trying to access a property or method of the Jest environment after it has been torn down.

This appears to be due to the useEffect() cleanup being called after Jest has done its own cleanup; manually unmount()ing the component before exiting the test resolves the issue.

This is problematic since jest --runInBand exits non-zero, causing pipeline failures (even though all tests pass).

Expected behavior

Automatic cleanup has access to the Jest environment, so I do not need to manually unmount().

Steps to Reproduce

// Add to `MyComponent`
  useEffect(() => {
    const handle = InteractionManager.runAfterInteractions(async () => {
      let nextFrame = new Promise((resolve) => requestAnimationFrame(() => resolve()));
      await nextFrame();
    });
    return () => {
      handle.cancel();
    };
  }, []);
test('Render example', async () => {
  const {toJSON, unmount} = render( <MyComponent /> );

  expect(toJSON()).toMatchSnapshot();

  // Uncomment to resolve access Jest environment after teardown.
  //unmount();
});

Screenshots

Versions

  npmPackages:
    @testing-library/react-native: ^12.4.3 => 12.4.3 
    react: 18.2.0 => 18.2.0 
    react-native: 0.73.4 => 0.73.4 
    react-test-renderer: 18.2.0 => 18.2.0 
@mdjastrzebski
Copy link
Member

@alpha0010 thank you for reporting this.

As far as I can see:

What you are doing in the workaround is that you are calling unmount() directly in the test, so before afterEach. That causes the useEffect cleanup be called inside the test, and not in "after test" phase.

I'm not sure if cleanup could technically be called by RNLT "inside" the test, as that is controlled by Jest (and you as the user). So the manual unmount() workaround seems the best we can do now. Let me know if you see other options.

@mdjastrzebski
Copy link
Member

Closing as stale. Workaround available (manual calling of unmount.

@mdjastrzebski mdjastrzebski closed this as not planned Won't fix, can't repro, duplicate, stale May 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants