Skip to content

Noisy React console error output is not automatically suppressed as documented when using test frameworks other than Jest #564

Closed
@jaydenseric

Description

@jaydenseric
  • react-hooks-testing-library version: v5.0.3

Reproduction:

In a blank project, run:

npm install react-test-renderer @testing-library/react-hooks --save-dev

In test.js:

const { renderHook } = require('@testing-library/react-hooks');

function useExample() {
  throw new Error('Message.');
}

renderHook(() => useExample());

Run:

node test.js

Notice the unexpected console error output in the terminal:

Screen Shot 2021-02-19 at 11 13 14 am

The above error occurred in the <TestComponent> component:

    at TestComponent (/[redacted]/node_modules/@testing-library/react-hooks/lib/helpers/createTestHarness.js:20:5)
    at Suspense
    at ErrorBoundary (/[redacted]/node_modules/react-error-boundary/dist/react-error-boundary.cjs.js:40:35)

React will try to recreate this component tree from scratch using the error boundary you provided, ErrorBoundary.

Problem description:

The documentation says:

To keep test output clean, we patch console.error when importing from @testing-library/react-hooks (or any of the other non-pure imports) to filter out the unnecessary logging and restore the original version during cleanup.
https://github.com/testing-library/react-hooks-testing-library/blob/master/docs/api-reference.md#consoleerror

In truth, patching only occurs if certain beforeEach and afterEach globals, arbitrary to specific testing frameworks, are present:

typeof beforeEach === 'function' &&
typeof afterEach === 'function' &&

Presumably it's assumed that every project in the world uses Jest 🙃

In another part of the docs:

In order to run tests, you will probably want to be using a test framework. If you have not already got one, we recommend using Jest, but this library should work without issues with any of the alternatives.
https://github.com/testing-library/react-hooks-testing-library/blob/master/docs/installation.md#testing-framework

“this library should work without issues with any of the alternatives” is not accurate. Plenty of testing frameworks don't mess with globals at all, while some do, but not in the same way as Jest. I use test-director to test my published packages (55.9 kB install size vs 34.4 MB for jest). Sometimes it makes sense to avoid a "framework" altogether and just use a vanilla Node.js script with the Node.js assert API.

Suggested solutions:

  1. Do nothing, except update all the documentation to explain specifically what globals need to present for the automatic console error suppression to work, and ideally list what testing frameworks do or don't provide these globals.

    This is not great because it doesn't provide an elegant solution for users with a test environment that doesn't provide these globals; they would be forced to either accept the console noise or hackily define beforeEach and afterEach globals in test scripts just for react-hooks-testing-library to use. It smells to have to copy the globals of an arbitrary testing framework not used in your codebase. The people that specifically chose a testing library that doesn't pollute the global namespace will be opposed to a globals based hack. From a code quality / maintenance perspective, it would not be intuitive without elaborate comments what beforeEach and afterEach refers to or why that workaround code is there.

  2. Support console error output suppression for any testing environment. Export functions to monkeypatch and restore the global console, available via the index and deep imports. Internally, these same functions could be used with the automatic beforeEach and afterEach globals detection.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions