Use NODE_ENV to detect Jest or Vitest #3144
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What:
Currently, when
@emotion/reactis used in a Vitest environment that hasglobals: false(which is Vitest’s default), it emits the following warning when included more than once:This warning is suppressed in Jest environments and in Vitest environments that have
globals: trueby checking for the presence of a globaljestorvi, respectively. This test does not work, though, whenglobals: falsebecause in that case, there is no globalvi. (This was also documented in the PR that extended the check to also takeviinto account, but the comment was modified for the final version of the PR that got merged.)Why:
If I understand the existing code correctly, the warning is supposed to be suppressed irregardless of whether
globals: trueorglobals: false.How:
This PR changes the test to check for
process.env.NODE_ENV === 'test'instead. According to the docs, this is a reliable way to detect both Jest and Vitest.The PR for Jest that introduced the check in its first form has a comment that voices concerns about accessing
process.envwhen using Webpack as a bundler. Since the code surrounding the check usesprocess.env.NODE_ENV !== 'production'anyway, I felt it’s safe to useNODE_ENVwhen checking for a test environment.Checklist:
I’m not entirely sure which of the checklist’s items apply to this PR and would greatly appreciate any guidance! I ticked off “Documentation” as I added a comment to the line I changed, but I don’t know whether that is sufficient.