From 84ccf43f0ba1bc9c5d26082777b844a7afbe88d2 Mon Sep 17 00:00:00 2001 From: Phil Parsons Date: Thu, 29 Sep 2022 11:30:50 +0100 Subject: [PATCH 1/2] Check vitest global for @emotion/react loaded twice warning --- .changeset/loud-dancers-return.md | 5 +++++ flow-typed/npm/vitest_vx.x.x.js | 1 + packages/react/src/index.js | 4 ++-- 3 files changed, 8 insertions(+), 2 deletions(-) create mode 100644 .changeset/loud-dancers-return.md create mode 100644 flow-typed/npm/vitest_vx.x.x.js diff --git a/.changeset/loud-dancers-return.md b/.changeset/loud-dancers-return.md new file mode 100644 index 000000000..0f0d46288 --- /dev/null +++ b/.changeset/loud-dancers-return.md @@ -0,0 +1,5 @@ +--- +'@emotion/react': patch +--- + +Include vitest global check when omitting the warning about duplicate instantiation in mocked modules. Will only capture vitest global `vi` if globals are configured. diff --git a/flow-typed/npm/vitest_vx.x.x.js b/flow-typed/npm/vitest_vx.x.x.js new file mode 100644 index 000000000..4fbf0e5de --- /dev/null +++ b/flow-typed/npm/vitest_vx.x.x.js @@ -0,0 +1 @@ +declare var vi: {} diff --git a/packages/react/src/index.js b/packages/react/src/index.js index 24323ed22..11107901d 100644 --- a/packages/react/src/index.js +++ b/packages/react/src/index.js @@ -17,9 +17,9 @@ export { default as css } from './css' if (process.env.NODE_ENV !== 'production') { const isBrowser = typeof document !== 'undefined' // #1727 for some reason Jest evaluates modules twice if some consuming module gets mocked with jest.mock - const isJest = typeof jest !== 'undefined' + const isTestEnv = typeof jest !== 'undefined' || typeof vi !== 'undefined' - if (isBrowser && !isJest) { + if (isBrowser && !isTestEnv) { // globalThis has wide browser support - https://caniuse.com/?search=globalThis, Node.js 12 and later const globalContext = // $FlowIgnore From 5d31936358aff34ae05b2bf9cee0a69140382f76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Thu, 27 Oct 2022 09:35:26 +0200 Subject: [PATCH 2/2] code review tweaks --- .changeset/loud-dancers-return.md | 2 +- packages/react/src/index.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.changeset/loud-dancers-return.md b/.changeset/loud-dancers-return.md index 0f0d46288..c0a93cf7e 100644 --- a/.changeset/loud-dancers-return.md +++ b/.changeset/loud-dancers-return.md @@ -2,4 +2,4 @@ '@emotion/react': patch --- -Include vitest global check when omitting the warning about duplicate instantiation in mocked modules. Will only capture vitest global `vi` if globals are configured. +Do not warn about `@emotion/react` being loaded twice in Vitest as that might easily happen with mocked modules. diff --git a/packages/react/src/index.js b/packages/react/src/index.js index 11107901d..3b4a3ad6a 100644 --- a/packages/react/src/index.js +++ b/packages/react/src/index.js @@ -16,7 +16,7 @@ export { default as css } from './css' if (process.env.NODE_ENV !== 'production') { const isBrowser = typeof document !== 'undefined' - // #1727 for some reason Jest evaluates modules twice if some consuming module gets mocked with jest.mock + // #1727, #2905 for some reason Jest and Vitest evaluate modules twice if some consuming module gets mocked const isTestEnv = typeof jest !== 'undefined' || typeof vi !== 'undefined' if (isBrowser && !isTestEnv) {