storybookjs / storybook

Storybook is the industry standard workshop for building, documenting, and testing UI components in isolation
https://storybook.js.org
MIT License
84.61k stars 9.31k forks source link

getStorybook() errors with "Cannot read properties of undefined (reading 'stories')" #19363

Open drewbrend opened 2 years ago

drewbrend commented 2 years ago

Describe the bug We use screener-storybook to run visual test against our storybook stories. It uses require('@storybook/react').getStorybook to hook into storybook. Since 2 days ago the call to this API started failing consistently. We did not change anything with storybook and I'm able to reproduce outside of our project and without screener-storybook so I don't think this is related to our configs or that library. This also started happening at the same time across multiple independent repos.

To Reproduce I tried to reproduce with npx sb@next repro, however I had issues with the generator so I reproduced here instead: https://stackblitz.com/edit/github-kza2ty?file=.storybook/preview.js

All I added from the default storybook setup is a call to require('@storybook/react').getStorybook().

System

Environment Info:

  System:
    OS: macOS 12.6
    CPU: (10) x64 Apple M1 Pro
  Binaries:
    Node: 14.19.3 - ~/.nvm/versions/node/v14.19.3/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v14.19.3/bin/yarn
    npm: 6.14.17 - ~/.nvm/versions/node/v14.19.3/bin/npm
  Browsers:
    Chrome: 105.0.5195.125
    Safari: 16.0
  npmPackages:
    @storybook/addon-a11y: ~6.5.12 => 6.5.12 
    @storybook/addon-actions: ~6.5.12 => 6.5.12 
    @storybook/addon-docs: ~6.5.12 => 6.5.12 
    @storybook/addon-essentials: ~6.5.12 => 6.5.12 
    @storybook/addon-interactions: ^6.5.12 => 6.5.12 
    @storybook/addon-links: ~6.5.12 => 6.5.12 
    @storybook/addon-storysource: ~6.5.12 => 6.5.12 
    @storybook/addons: ~6.5.12 => 6.5.12 
    @storybook/builder-webpack5: ^6.5.12 => 6.5.12 
    @storybook/components: ~6.5.12 => 6.5.12 
    @storybook/jest: ^0.0.10 => 0.0.10 
    @storybook/manager-webpack5: ^6.5.12 => 6.5.12 
    @storybook/react: ^6.5.12 => 6.5.12 
    @storybook/source-loader: ~6.5.12 => 6.5.12 
    @storybook/testing-library: ^0.0.13 => 0.0.13 
    @storybook/theming: ~6.5.12 => 6.5.12 

Additional context I have tried downgrading the version of storybook back to 6.4 and even upgrading to latest 7.0-alpha.34 (stories is now called entries in v7) but still see the error, so it seems unrelated to the version of storybook.

Error

bootstrap:27 Uncaught TypeError: Cannot read properties of undefined (reading 'stories')
    at ClientApi.getStorybook (ClientApi.js:371:1)
    at ./.storybook/preview.js (preview.js:108:1)
    at __webpack_require__ (bootstrap:24:1)
    at fn (hot module replacement:62:1)
    at ./.storybook/preview.js-generated-config-entry.js (preview.js:112:1)
    at __webpack_require__ (bootstrap:24:1)
    at __webpack_exec__ (generated-stories-entry.cjs:6:1)
    at generated-stories-entry.cjs:6:1
    at __webpack_require__.O (chunk loade
blovato commented 2 years ago

➗ I'm also running into this issue in my repo. Please triage!

sauce-tartar commented 1 year ago

Thanks to the excellent repro shared, we were able to prove our new screener-storybook hook for storyStoreV7 also works in your mysterious edge-case here. We have end to end Visual test results working and looking to push out an alpha soon for feedback.

This is the basis for our first step into proper V7 support in screener-storybook, we'll make sure there's an option for this path as well where:

storeV7-step1-new-hook

sauce-tartar commented 1 year ago

Update: Screener Storybook beta no longer uses this approach, note "Hookless" remarks in README including for static builds where you'll want to remove any manual additions to your preview.js.

sauce-tartar commented 1 year ago

Screener Storybook 1.0 is now released, and require('@storybook/react').getStorybook is no longer present as of this version.

Please also remove any __screener_storybook__ legacy hooks that may have been manually added to your builds per our old instructions.