test-runner icon indicating copy to clipboard operation
test-runner copied to clipboard

[Bug] generated testMatch assumes Storybook working dir matches Jest rootDir

Open FokkeZB opened this issue 2 years ago • 0 comments

Describe the bug

A clear and concise description of what the bug is.

Steps to reproduce the behavior

  1. Create a monorepo that contains stories in multiple packages, but .storybook only in one of them, sourcing from all packages.
  2. Run yarn test-runner from the package that has .storybook notice only stories in that package get found.
  3. Eject and set rootDir: '../..' hoping that Jest finds the other stories as well.
  4. Run yarn test-runner again and notice the absolute testMatch paths now end up 2 levels to high.

Expected behavior

Test-runner should be able to run any story that the Storybook can build.

Screenshots and/or logs

@yannbf has details.

Environment

Only relevant version is that of @storybook/test-runner, which is 0.2.0.

Additional context

The assumption that the Storybook working dir matches the Jest rootDir is baked into https://github.com/storybookjs/test-runner/blob/668e56ca710f5079f72dc1fa62db5f8473a5b117/src/util/getStorybookMetadata.ts#L20

We can fix this by either:

  • Getting our hands on the rootDir value and make the paths relative to that.
  • Drop <rootDir> and make the paths absolute.

The second one might make most sense since Jest will ultimately make them absolute anyway.

However, even if we fix this, Jest isn't really made to run across multiple packages and it's very likely that we run into other issues, like multiple packages defining the same __mocks__.

An alternative is to use --stories-json but that has cons like not having --watch.

Perhaps it's best to consider sourcing stories from other packages a bad-practice (if it isn't already) and steer towards one storybook per package, optionally composed.

FokkeZB avatar Jun 27 '22 10:06 FokkeZB