Skip to content
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

Use NODE_ENV to detect Jest or Vitest #3144

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

cruessler
Copy link

What:

Currently, when @emotion/react is used in a Vitest environment that has globals: false (which is Vitest’s default), it emits the following warning when included more than once:

You are loading @emotion/react when it is already loaded. Running multiple instances may cause problems. This can happen if multiple versions are used, or if multiple builds of the same version are used.

This warning is suppressed in Jest environments and in Vitest environments that have globals: true by checking for the presence of a global jest or vi, respectively. This test does not work, though, when globals: false because in that case, there is no global vi. (This was also documented in the PR that extended the check to also take vi into 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: true or globals: 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.env when using Webpack as a bundler. Since the code surrounding the check uses process.env.NODE_ENV !== 'production' anyway, I felt it’s safe to use NODE_ENV when 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.

  • Documentation
  • Tests
  • Code complete
  • Changeset

Copy link

changeset-bot bot commented Dec 24, 2023

⚠️ No Changeset found

Latest commit: d42e3fb

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit d42e3fb:

Sandbox Source
Emotion Configuration

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant