Skip to content

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

Closed
@jniemin

Description

@jniemin
  • @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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions