storybook: [Bug]: (vitest integration) `test.include` should not be ignored

Describe the bug

About removing test.include here,

One drawback is then user can’t run non-storybook tests. i.e. user may have test in .test.ts which uses import { render } from 'vitest-browser-react' to write unit tests. Those needs to be included by Vitest in test.include.

I believe many users will be on the same boat as they migrate their existing projects to use Storybook to replace current tests.

Reproduction link

https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#addon-test-indexing-behavior-of-storybookexperimental-addon-test-is-changed

Reproduction steps

It is described in the migration guide: https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#addon-test-indexing-behavior-of-storybookexperimental-addon-test-is-changed

System

Storybook Environment Info:

  System:
    OS: Linux 5.15 Ubuntu 24.04.1 LTS 24.04.1 LTS (Noble Numbat)
    CPU: (16) x64 AMD Ryzen 7 5800X 8-Core Processor
    Shell: 5.9 - /usr/bin/zsh
  Binaries:
    Node: 20.18.0 - /run/user/1000/fnm_multishells/720_1737231505770/bin/node
    npm: 10.8.2 - /run/user/1000/fnm_multishells/720_1737231505770/bin/npm
    pnpm: 9.12.2 - /run/user/1000/fnm_multishells/720_1737231505770/bin/pnpm <----- active

Additional context

No response

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 4
  • Comments: 23 (17 by maintainers)

Commits related to this issue

Most upvoted comments

This is only for Storybook Test UI which can only show stories anyway, right? When running from the Vitest CLI, users should still be able to run all their unit tests (and non-storybook component tests). If that’s not the case then I agree this is a bug.

Not only for Storybook Test UI. Running vitest run will use the vitest.config.ts that uses browser mode and with storybookTest(). The current behavior does prevent the tests written in .test.tsx from being executed.

Technically you can create two Vitest configs. One run in browser mode without storybookTest() and one in browser mode with, but that would complicate the configuration unnecessary as now you need to have two different scripts and/or using Vitest workspace.

It also make thing more flaky as running multiple instance of browsers causes test timeouts both locally and in CI.

Render tests written in .test.tsx and in .stories.tsx should run together as they are both testing rendering and interactions (and run on browser).

Thing would be even more complicate if user runs multiple instances of different browsers. Everything has to be duplicated.

Hey @unional, I will take a deeper look into this use case, and will get back to this issue later!

@unional I’ll discuss with @valentinpalkovic on Monday and see what we can do. Sorry for blocking!

Happy to open a separate issue for my comment, but I think it’s ultimately the same: ignoring test.include makes it unclear how to use the Vitest test addon with Vitest Workspace. I have a project with multiple kinds of test files and I want the Storybook test addon to only apply to a subset of the test files (i.e. the story files). I use the following vitest.workspace.ts file:

import { storybookTest } from "@storybook/experimental-addon-test/vitest-plugin";
import { defineWorkspace } from "vitest/config";

export default defineWorkspace([
  {
    extends: "./vite.config.ts",
    plugins: [
      storybookTest({
        configDir: "./storybook",
        storybookScript: "pnpm storybook --ci",
        storybookUrl: process.env.SB_URL,
      }),
    ],
    test: {
      include: ["src/**/*.stories.tsx"],
      name: "stories",
      setupFiles: ["./storybook/vitest.setup.ts"],
    },
  },
  {
    extends: "./vite.config.ts",
    test: {
      include: ["src/**/*.test-d.tsx"],
      name: "types",
    },
  },
  {
    extends: "./vite.config.ts",
    test: {
      include: ["src/**/utilities/**/*.test.ts"],
      name: "utilities",
    },
  },
]);

I’ve had no issues with this configuration in Storybook < 8.5. Ignoring test.include in 8.5 changes the behavior of the Vitest configuration, though. Even if the storybookTest plugin has no effect on non-story files, it still runs when I want to run non-storybook tests (e.g. pnpm vitest --project=utilities). Moreover, the warning added in https://github.com/storybookjs/storybook/commit/c385838557a30793de90759a1f53907fbe81cc86 only stops printing when I remove all 3 test.include from the workspace file (again, because storybookTest is running for each project in the workspace, instead of just the first one), but removing test.include defeats the purpose of the workspace feature (separate Vitest configurations for separate subsets of files).