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

[Bug]: play function does not work in stories built for Production #19758

Closed
shgtkshruch opened this issue Nov 5, 2022 · 19 comments
Closed

[Bug]: play function does not work in stories built for Production #19758

shgtkshruch opened this issue Nov 5, 2022 · 19 comments

Comments

@shgtkshruch
Copy link

shgtkshruch commented Nov 5, 2022

Describe the bug

I build storybook with build-storybook command.

$ npm run build-storybook

> [email protected] build-storybook
> build-storybook

info @storybook/react v6.5.12
info 
info => Cleaning outputDir: /Users/shigetaka-shirouchi/dev/ghq/github.com/organization/project/storybook-static
info => Loading presets
info => Compiling manager..
info => Compiling preview..
info Addon-docs: using MDX1
info => Using implicit CSS loaders
info => Using default Webpack5 setup
92% sealing asset processing TerserPlugininfo => Manager built (13 s)
info => Preview built (28 s)
WARN asset size limit: The following asset(s) exceed the recommended size limit (244 KiB).
WARN This can impact web performance.
WARN Assets: 
WARN   807.3ba8e026.iframe.bundle.js (792 KiB)
WARN   721.a140354b.iframe.bundle.js (6.69 MiB)
WARN entrypoint size limit: The following entrypoint(s) combined asset size exceeds the recommended limit (244 KiB). This can impact web performance.
WARN Entrypoints:
WARN   main (6.93 MiB)
WARN       runtime~main.9147eaa6.iframe.bundle.js
WARN       721.a140354b.iframe.bundle.js
WARN       main.17af0170.iframe.bundle.js
WARN 
info => Output directory: /Users/shigetaka-shirouchi/dev/ghq/github.com/organization/project/storybook-static

When I open a stories that implements the play function in a browser, I get the following error.

act(...) is not supported in production builds of React.
Error: act(...) is not supported in production builds of React.
    at exports.unstable_act (file:///Users/shigetaka-shirouchi/dev/ghq/github.com/organization/project/storybook-static/721.a140354b.iframe.bundle.js:2:6483330)
    at file:///Users/shigetaka-shirouchi/dev/ghq/github.com/organization/project/storybook-static/721.a140354b.iframe.bundle.js:2:4428150
    at Object.eventWrapper (file:///Users/shigetaka-shirouchi/dev/ghq/github.com/organization/project/storybook-static/721.a140354b.iframe.bundle.js:2:4430316)
    at fireEvent (file:///Users/shigetaka-shirouchi/dev/ghq/github.com/organization/project/storybook-static/721.a140354b.iframe.bundle.js:2:4716076)
    at Object.keys.forEach.fireEvent.<computed> [as pointerOver] (file:///Users/shigetaka-shirouchi/dev/ghq/github.com/organization/project/storybook-static/721.a140354b.iframe.bundle.js:2:4719213)
    at hover (file:///Users/shigetaka-shirouchi/dev/ghq/github.com/organization/project/storybook-static/721.a140354b.iframe.bundle.js:2:4893126)
    at Object.click (file:///Users/shigetaka-shirouchi/dev/ghq/github.com/organization/project/storybook-static/721.a140354b.iframe.bundle.js:2:4889059)
    at _callee$ (file:///Users/shigetaka-shirouchi/dev/ghq/github.com/organization/project/storybook-static/main.17af0170.iframe.bundle.js:2:171469)
    at tryCatch (file:///Users/shigetaka-shirouchi/dev/ghq/github.com/organization/project/storybook-static/main.17af0170.iframe.bundle.js:2:160899)
    at Generator._invoke (file:///Users/shigetaka-shirouchi/dev/ghq/github.com/organization/project/storybook-static/main.17af0170.iframe.bundle.js:2:160522)

I build storybook with NODE_ENV=development,

$ NODE_ENV=development npm run build-storybook
> [email protected] build-storybook
> NODE_ENV=development build-storybook

info @storybook/react v6.5.12
info 
info => Cleaning outputDir: /Users/shigetaka-shirouchi/dev/ghq/github.com/organization/project/storybook-static
info => Loading presets
info => Compiling manager..
info => Compiling preview..
info Addon-docs: using MDX1
info => Using implicit CSS loaders
info => Using default Webpack5 setup
92% sealing asset processing TerserPlugininfo => Manager built (14 s)
WARN DefinePlugin
WARN Conflicting values for 'process.env.NODE_ENV'
info => Preview built (30 s)
WARN DefinePlugin
WARN Conflicting values for 'process.env.NODE_ENV'
WARN asset size limit: The following asset(s) exceed the recommended size limit (244 KiB).
WARN This can impact web performance.
WARN Assets: 
WARN   807.3ba8e026.iframe.bundle.js (792 KiB)
WARN   721.c956dec1.iframe.bundle.js (7.31 MiB)
WARN entrypoint size limit: The following entrypoint(s) combined asset size exceeds the recommended limit (244 KiB). This can impact web performance.
WARN Entrypoints:
WARN   main (7.56 MiB)
WARN       runtime~main.229412ac.iframe.bundle.js
WARN       721.c956dec1.iframe.bundle.js
WARN       main.36f20661.iframe.bundle.js
WARN 
info => Output directory: /Users/shigetaka-shirouchi/dev/ghq/github.com/organization/project/storybook-static

This is work fine.
スクリーンショット 2022-11-05 19 01 24

To Reproduce

No response

System

Environment Info:

  System:
    OS: macOS 12.6
    CPU: (10) arm64 Apple M1 Max
  Binaries:
    Node: 18.9.0 - /opt/homebrew/bin/node
    npm: 8.19.1 - /opt/homebrew/bin/npm
  Browsers:
    Chrome: 107.0.5304.87
    Safari: 16.0
  npmPackages:
    @storybook/addon-actions: ^6.5.12 => 6.5.12 
    @storybook/addon-essentials: ^6.4.22 => 6.5.12 
    @storybook/addon-interactions: ^6.5.12 => 6.5.12 
    @storybook/addon-links: ^6.5.12 => 6.5.12 
    @storybook/builder-webpack5: ^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.1.21 => 6.5.12 
    @storybook/testing-library: ^0.0.13 => 0.0.13 
    @storybook/testing-react: ^1.3.0 => 1.3.0

Additional context

No response

@jtbandes
Copy link

jtbandes commented Dec 17, 2022

I'm having this problem too: storybook, code, build step. Is there any advice or known workaround?

Edit: never mind, it was fixed by ensuring the version of all storybook packages match.

@shilman
Copy link
Member

shilman commented Dec 18, 2022

@shgtkshruch do you have a reproduction repo you can share?

@shgtkshruch
Copy link
Author

@shilman Thanks for response.
I made sure to use the latest version of Storybook and related libraries and the error no longer occurs.

Copy link
Member

shilman commented Dec 18, 2022

@shgtkshruch glad to hear that solved it!

@lokeshwarobuli
Copy link

@shgtkshruch how did you add NODE_ENV to your code ?

@shgtkshruch
Copy link
Author

I run npm command with NODE_ENV like this.

NODE_ENV=development npm run build-storybook

@Mattgic
Copy link

Mattgic commented Apr 5, 2023

I had the exact same problem, and found out it was because I had a story importing something from my test-utils file, which contains this line :

export * from '@testing-library/react'

This file is where I define a custom render function for my component tests with React Testing Library, and acts as a wrapper for '@testing-library/react'.
The fix was to move out the util function I needed to another file.

I guess there is a conflict between '@testing-library/react' and '@storybook/testing-library'. Make sure only '@storybook/testing-library' is pulled in your stories.

Note that the issue only happens when using userEvent actions (clicks) in your play functions. No problem if you are just waiting for an element to display.

@foxaltus
Copy link

foxaltus commented May 24, 2023

Im struggling with this one too... I've made sure all packages are on the same version (6.5.10 for me) and @storybook/testing-library is uptodate (0.0.13).

Also userEvent is imported from @storybook/testing-library in the story.

Not sure what's going on...

Did you guy solve it by building storybook in development mode?

@Mattgic
Copy link

Mattgic commented May 24, 2023

With the fix I mentioned, play function now also work in production builds.

@foxaltus
Copy link

foxaltus commented May 24, 2023

I really can't make this work :( I even removed the @testing-library/user-event package from my dependencies and deleted all the tests that imported it.

Also updated all storybook packages to 6.5.15, no luck

@rgdzv
Copy link

rgdzv commented Aug 22, 2023

@foxaltus, did you handle the error?

@foxaltus
Copy link

@foxaltus, did you handle the error?

Unfortunately, no, I gave up with this :( Let me know if you ever find a workaround

@rgdzv
Copy link

rgdzv commented Aug 22, 2023

@foxaltus, ok)

@rgdzv
Copy link

rgdzv commented Aug 22, 2023

@foxaltus, well, that helped me in resolving an error:

  1. Install cross-env
    npm install cross-env --save-dev

  2. Change your script

   "script": {
      "build-storybook": "cross-env NODE_ENV=development storybook build"
   }
  1. Run command to launch your build version on the server
    npx http-server storybook-static

  2. Run test with URL from http-server
    npm run test-storybook -- --url YOUR_URL

All tests should pass. I think (as mentioned above) there are some problems between userEvent from @storybook/testing-library and userEvent from @testing-library/user-event. All of that forces us to install additional package (cross-env). But I think not everyone has that kind of problem.

@foxaltus
Copy link

OK so that confirms that if you want to use interaction testing, you need to build in development mode. I'm not sure how I feel about that 🤔

@Mattgic
Copy link

Mattgic commented Aug 23, 2023

Building in dev mode is a bad idea, your bundle will be much bigger and slower.
I managed to make it work, I'm sure you will find a way too :)

@rgdzv
Copy link

rgdzv commented Aug 23, 2023

@foxaltus, well, found a desicion. Not need to build in development mode, I have 2 files for testing components with RTL. They are both for providers.

One of them for creating function to cover all providers (I took an example from here)

import { type ReactElement } from 'react'
import {
    render,
    type RenderResult,
    type RenderOptions,
    type queries
} from '@testing-library/react'
import { ThemeProviderTest } from './__mocks__/ThemeProviderTest'
import { MemoryRouter } from 'react-router-dom'

type R = RenderResult<typeof queries, HTMLElement, HTMLElement>

const AllTheProviders = ({
    children
}: {
    children: React.ReactNode
}): JSX.Element => {
    return (
        <MemoryRouter>
            <ThemeProviderTest>{children}</ThemeProviderTest>
        </MemoryRouter>
    )
}

export const customRender = (
    ui: ReactElement,
    options?: Omit<RenderOptions, 'wrapper'>
): R => render(ui, { wrapper: AllTheProviders, ...options })

Second file also for provider (custom router):

import {
    RouterProvider,
    type RouterProviderProps,
    createMemoryRouter
} from 'react-router-dom'
import { render } from '@testing-library/react'
import { routes } from 'app/providers/RouterProvider'

export const renderWithRouter = (path = '/'): RouterProviderProps => {
    const router = createMemoryRouter(routes, {
        initialEntries: [path]
    })

    render(<RouterProvider router={router} />)

    return { router }
}

Commenting them or deleting them made all tests pass in build version.

It means that providers (for testing components with RTL) not organized properly and seems that they somehow affect @storybook

@foxaltus
Copy link

Commenting them or deleting them made all tests pass in build version.

hey @rgdzv do you have any import of @testing-library/react left in your codebase after deleting these files?

@rgdzv
Copy link

rgdzv commented Aug 24, 2023

@foxaltus, yes, in all .test. files where I use render and screen

Before coming to a decision, I deleted node_modules and package-lock.json and reinstalled it.

Also I deleted @testing-library/jest-dom, @testing-library/react, jest, jest-environment-jsdom and tried to test build version of SB. It worked. After I started installing all deleted packages one by one back to understand what makes error (and check tests of SB between). Also I tried to launch local version of SB between installing packages. There was a mistake (don't remember exact message) which indicated on these files.

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

7 participants