Skip to content

use of process.env and filter-console library prevents running tests in browser environment #617

Closed
@bdwain

Description

@bdwain
  • react-hooks-testing-library version: 5.1.2
  • react version: 17
  • react-dom version (if applicable): 17
  • react-test-renderer version (if applicable): n/a
  • node version: 14
  • npm (or yarn) version: yarn 2

Relevant code or config:

import '@testing-library/react-hooks/dom/pure';

What you did:

just imported the library

What happened:

ERROR in ./node_modules/filter-console/index.js 2:13-28
Module not found: Error: Can't resolve 'util' in '/Users/bwain/dev/react-hooks-testing-lib-process-bug/node_modules/filter-console'

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

I've also seen other errors (depending on my exact config) due to use of process.env for things like process.env.RHTL_SKIP_AUTO_CLEANUP.

Reproduction:

https://github.com/bdwain/react-hooks-testing-lib-process-bug

Problem description:

react testing library can not run in a browser environment because it uses process.env and because it pulls in the filter-console library, which is targeted at node and uses the util library under the hood.

Webpack used to provide a node polyfill for you automatically in webpack <5, which likely masked the issue for a long time. Now that they've stopped polyfilling node, issues like this are being exposed.

While Jest is probably the most widely use test runner right now (and it uses a node environment with JSDom), many people prefer to run their tests in an actual browser using tools like karma. There's no good way to do that though with the current usages of process.env and filter-console. If we were to start modifying our test environments to polyfill node modules or stub them out somehow, we'd lose our ability to detect usages of those node modules in tests, which decreases their value.

Suggested solution:

If the usages of node modules could be replaced with things that work in both a browser environment and a node environment, that would open up usage to a lot of people.

Alternatively, the things that use node modules could be made to be separately imported from the rest of the library, so that they are opt-in rather than forced on you.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions