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

Components styled with emotion do not get theme from material theme provider #5664

Open
6 tasks done
AlexDroll opened this issue May 3, 2024 · 3 comments
Open
6 tasks done

Comments

@AlexDroll
Copy link

Describe the bug

Having a react app using material ui and emotion/styled, components styled with emotion/styled do not receive the theme from the material ui ThemeProvider in tests.
This is working for production code.

As a workaround one could wrap the tested component into an additional theme provider from emotions. See also this stack overflow discussion.

Reproduction

A minimal example can be found in this StackBlitz sandbox

System Info

System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 18.18.0 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 10.2.3 - /usr/local/bin/npm
    pnpm: 8.15.6 - /usr/local/bin/pnpm
  npmPackages:
    @vitejs/plugin-react: ^4.2.1 => 4.2.1 
    @vitest/ui: 1.5.3 => 1.5.3 
    vite: 5.2.10 => 5.2.10 
    vitest: 1.5.3 => 1.5.3

Used Package Manager

npm

Validations

@hi-ogawa
Copy link
Contributor

hi-ogawa commented May 4, 2024

As indicated by the warning:

stderr | node_modules/@ emotion/react/dist/emotion-react.esm.js:472:15
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 is a dual package issue of @emotion/react. The one imported through @mui/material (via NodeJS) and the other from your source code (via Vite/Vitest) are seeing different context singleton.

It looks like NodeJS is loading @emotion/react/dist/emotion-react.cjs.mjs, so you can force Vitest's resolution via alias:
https://stackblitz.com/edit/vitest-dev-vitest-wpfmsa?file=vitest.config.ts

  test: {
    alias: {
      '@emotion/react': path.resolve(
        'node_modules/@emotion/react/dist/emotion-react.cjs.mjs'
      ),
    },
  },

Looking at https://publint.dev/@emotion/react@11.11.4, the package exports is quite complicated, but seems legitimate, so it might be possible that Vite / Vitest's default resolution is doing something wrong.

@AlexDroll
Copy link
Author

Well as workaround this works but I think it is not good that vite and vitest behave in a different way.
Isn't there a way to force the loading of the correct module on vitest side?

@hi-ogawa
Copy link
Contributor

hi-ogawa commented May 6, 2024

I tried to decipher what's happening but unfortunately I couldn't figure out yet where this goes wrong. https://github.com/hi-ogawa/reproductions/tree/main/vitest-5664-mui-emotion-provider

It seems like Vite SSR is fine, so Vitest / Vite-node might be making bad resolution. It could be a bug, so we can leave this issue open.

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

No branches or pull requests

2 participants