Skip to content

Error in useEffect clean up function not causing test to fail #874

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

Closed
jniemin opened this issue Feb 3, 2021 · 4 comments
Closed

Error in useEffect clean up function not causing test to fail #874

jniemin opened this issue Feb 3, 2021 · 4 comments
Labels
bug Something isn't working

Comments

@jniemin
Copy link

jniemin commented Feb 3, 2021

  • @testing-library/react version: 11.2.5
  • Testing Framework and version:
    New application created by create-react-app with dependencies
"@testing-library/jest-dom": "^5.11.4",
"@testing-library/react": "^11.1.0",
"@testing-library/user-event": "^12.1.10",
"react": "^17.0.1",
"react-dom": "^17.0.1",
  • DOM Environment:
    Jest version 26.6.3
    JSDOM 16.4.0

Relevant code or config:

import React from 'react';
import { render } from '@testing-library/react';

function SimpleComponent() {
  React.useEffect(() => {
    return function cleanUp() {
      throw new Error('Clean up throws error');
    };
  }, []);
  return (<div>HelloWorld</div>);
}

describe('Simple test case', function() {
  it('This test case should not pass', () => {
    expect(() => {
      const { unmount } = render(<SimpleComponent />);
      unmount();
    }).not.toThrow();
  });
});

What you did:

Simple test case above

What happened:

When react useEffect cleanup function throws error, test passes when it should not.

Reproduction:

  1. Create new react app (I did this with create-react-app).
  2. Add above test case to app
  3. Run tests

Problem description:

This test case should fail as there was error thrown in useEffect clean up, but it does not. It will log the error but test case passes.

Suggested solution:

Test should fail if cleanup function throws any errors.

@giseleml
Copy link

giseleml commented Mar 4, 2021

Maybe you should reproduce your code on something like CodePen or CodeSandbox and add to your issue because when I'm testing locally the test does not pass. 😕

@eps1lon eps1lon added bug Something isn't working needs investigation labels Mar 4, 2021
@jniemin
Copy link
Author

jniemin commented Mar 5, 2021

@giseleml @eps1lon I added new sample to CodeSandbox https://codesandbox.io/s/intelligent-oskar-puduj?file=/src/example.spec.js. Let me know if you need any more help on debugging this

@eps1lon
Copy link
Member

eps1lon commented Mar 6, 2021

@jniemin Thanks for the repro. This behavior changed in React 17. I've opened facebook/react#20946 on the React repo to get clarification since I don't think that we can do anything about it in @testing-library/*

@eps1lon
Copy link
Member

eps1lon commented Mar 7, 2021

The behavior you're looking for is currently behind a feature flag and not accessible to open-source builds of React. I encourage you to subscribe to facebook/react#20946 to be notified about updates.

We can't do anything about in testing-library/react.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants