Skip to content

[Bug] Portable stories with browser mode disabled fail with react dependent libraries #63

@sylvaingi

Description

@sylvaingi

Describe the bug

When trying to use this plugin with composeStories and browser mode disabled (as there are issues with MSW), dependencies like MUI, Radix that import React in their code, end up throwing errors at runtime when using React APIs like useEffect, useContext, etc.

Steps to reproduce the behavior

  1. Clone [email protected]:sylvaingi/vite-plugin-storybook-nextjs.git
  2. Switch to mui-issue branch
  3. In example folder, launch pnpm test MuiButton
  4. Test fails

Expected behavior

Test pass successfully

Screenshots and/or logs

stderr | src/app/components/MuiButton/MuiButton.test.tsx > MuiButton > should render the MUI Button
Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app
See https://react.dev/link/invalid-hook-call for tips about how to debug and fix this problem.

 FAIL   storybook  src/app/components/MuiButton/MuiButton.test.tsx > MuiButton > should render the MUI Button
TypeError: Cannot read properties of null (reading 'useContext')
 ❯ Module.process.env.NODE_ENV.exports.useContext ../node_modules/.pnpm/[email protected]/node_modules/react/cjs/react.development.js:1168:25
 ❯ Button ../node_modules/.pnpm/@[email protected]_@[email protected]_@[email protected][email protected]__@emotion+styled_mvpn2cah7wbu7btlp5cl54ev3i/node_modules/@mui/material/esm/Button/Button.js:473:30
 ❯ Object.react-stack-bottom-frame ../node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected][email protected]/node_modules/next/dist/compiled/react-dom/cjs/react-dom-client.development.js:23348:20
 ❯ renderWithHooks ../node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected][email protected]/node_modules/next/dist/compiled/react-dom/cjs/react-dom-client.development.js:6723:22
 ❯ updateForwardRef ../node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected][email protected]/node_modules/next/dist/compiled/react-dom/cjs/react-dom-client.development.js:8736:19
 ❯ beginWork ../node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected][email protected]/node_modules/next/dist/compiled/react-dom/cjs/react-dom-client.development.js:10936:18
 ❯ runWithFiberInDEV ../node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected][email protected]/node_modules/next/dist/compiled/react-dom/cjs/react-dom-client.development.js:857:13
 ❯ performUnitOfWork ../node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected][email protected]/node_modules/next/dist/compiled/react-dom/cjs/react-dom-client.development.js:15575:22
 ❯ workLoopSync ../node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected][email protected]/node_modules/next/dist/compiled/react-dom/cjs/react-dom-client.development.js:15387:41
 ❯ renderRootSync ../node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected][email protected]/node_modules/next/dist/compiled/react-dom/cjs/react-dom-client.development.js:15367:11

Environment

  • OS: macOS 26.0.1
  • Node.js version: 22.12.0
  • NPM version: 10.9.0
  • Browser (if applicable) N/A
  • Browser version (if applicable): N/A
  • Device (if applicable): N/A

Additional context

My understanding is that the dependency importing React somehow end up with a different React build than the one used by the rest of the code. Maybe an issue with the storybook.global.ts file?

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions