-
Notifications
You must be signed in to change notification settings - Fork 232
use of process.env and filter-console library prevents running tests in browser environment #617
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
Comments
Thanks @bdwain for raising this. When I first read it I was sure you must not be importing from the While importing I wanted to avoid writing the console filtering ourselves as it would be just another thing we need to maintain, but unless there is a reasonable alternative, I cannot think of a better solution. |
thanks! Let me know if there's anything I can do to help make this happen. |
PRs are always welcome 😉 Basically, this is the code we need to replace. We want to prevent calls to |
cool I can check that out. what about the configuration via Are they technically a supported API? Because if they are, removing them would be a breaking change, which is not ideal. |
The env variables are a supported and documented part of the API, and is consistent with the auto cleanup approach we use, as well as |
Sorry, hit the wrong button |
i think the process.env stuff already works when you use I tried example //switch
if (typeof afterEach === 'function' && !process.env.RHTL_SKIP_AUTO_CLEANUP) {
afterEach(async () => {
await cleanup()
})
}
//to
let skipAutoCleanup;
try {
skipAutoCleanup = process.env.RHTL_SKIP_AUTO_CLEANUP;
}
catch(err){
skipAutoCleanup = false;
}
if (typeof afterEach === 'function' && !skipAutoCleanup) {
afterEach(async () => {
await cleanup()
})
} for a second i was thinking you could make a helper to read env variables so it wouldn't have to be so verbose each time, but that would break a lot of tools that populate env variables by looking for the actual string in the code and replacing it inline (like webpack define plugin) |
You can probably use Edit: actually, just thinking that through, I think it gets problematic if somone does replace the variable inline and then run in a non-node environment ( function skipAutoCleanup() {
try {
return process.env.RHTL_SKIP_AUTO_CLEANUP;
}
catch(err){
return false;
}
}
// ...
if (typeof afterEach === 'function' && !skipAutoCleanup()) {
afterEach(async () => {
await cleanup()
})
} |
that might not work because of tools that find/replace process.env. They won't replace usages of
|
Yeah, I realised that in my edit. I think you're on the right track with it. |
cool ill messing with a PR |
Hey @bdwain, how are you going with this? |
I haven't been able to start yet. I should be able to get to it this weekend though. |
I had a quick play around with it last night and I have something that is working. I can clean it up and make a PR for you to review or I can hold off and wait to see what you come up with this weekend (I don't want to steal your opportunity to contribute, I was looking at something else and removing the |
If you have something working then go for it! I’m just happy to get the
node errors fixed. I can review the pr.
…On Thu, May 20, 2021 at 5:51 PM Michael Peyper ***@***.***> wrote:
I had a quick play around with it last night and I have something that is
working. I can clean it up and make a PR for you to review or I can hold
off and wait to see what you come up with this weekend (I don't want to
steal your opportunity to contribute, I was looking at something else and
removing the filter-console dependency was helpful for that as well).
Whatever you prefer?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#617 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AA6MGDUR4XOOJ5Q537N7F5DTOWG7LANCNFSM44UQSP7Q>
.
|
@bdwain, GH is not letting be add you as a reviewer, but you can find the PR in the above link. |
…sting in browser environments * feat: removed filter-console dependency and fallback if process.env is not available (#624) * fix: protect import helpers for setting env variables and comment why try/catch is being used BREAKING CHANGE: `suppressErrorOutput` will now work when explicitly called, even if the `RHTL_DISABLE_ERROR_FILTERING` env variable has been set Fixes #617
🎉 This issue has been resolved in version 7.0.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
react-hooks-testing-library
version: 5.1.2react
version: 17react-dom
version (if applicable): 17react-test-renderer
version (if applicable): n/anode
version: 14npm
(oryarn
) version: yarn 2Relevant code or config:
import '@testing-library/react-hooks/dom/pure';
What you did:
just imported the library
What happened:
I've also seen other errors (depending on my exact config) due to use of
process.env
for things likeprocess.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 thefilter-console
library, which is targeted atnode
and uses theutil
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
andfilter-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.
The text was updated successfully, but these errors were encountered: