Skip to content

[Bug]: Vitest CLI unable to resolve CSS/JS filepaths when they are programmatically generated in main.js #34676

@basher

Description

@basher

Describe the bug

I have multiple CSS/JS themes in my real Storybook/HTML repo, which I manage in .storybook/main.js via .env variables, enabling me to determine if Storybook is running in local DEV mode or PRODUCTION.

This works when running Storybook UI. CSS/JS loads correctly, irrespective of environment.

However, I noticed that Vitest does not load the CSS/JS for Storybook, so the a11y tests are running purely on "naked" HTML components - I tested this by setting headless: false in vitest.config.js.

NOTE: I have a temporary workaround for now, which does not use .env files, or main.js... but I have setup a simplified repro Storybook to demonstrate the actual problem - see below.

Reproduction link

https://github.com/basher/storybook-html-vitest-a11y

Reproduction steps

  1. Uncomment the previewHead() function in main.js.
  2. Comment out the inline <script> in preview-head.html.
  3. npm run storybook - the CSS loads OK in the UI.
  4. Set headless: false in vitest.config.js.
  5. npm run test-storybook - the CSS does NOT load. The rendered <button> is unstyled.

System

│  Storybook Environment Info:
│

│  System:
│  OS: Windows 10 10.0.19045
│  CPU: (4) x64 Intel(R) Core(TM) i7-6500U CPU @ 2.50GHz
│  Binaries:
│  Node: 22.19.0 - C:\Program Files\nodejs\node.EXE
│  Yarn: 1.22.22 - C:\Users\kev\AppData\Roaming\npm\yarn.CMD
│  npm: 10.9.3 - C:\Program Files\nodejs\npm.CMD <----- active
│  Browsers:
│  Chrome: 147.0.7727.117
│  Edge: Chromium (140.0.3485.94)
│  Firefox: 150.0.1 - C:\Program Files (x86)\Mozilla Firefox\firefox.exe
│  npmPackages:
│  @storybook/addon-a11y: ^10.3.5 => 10.3.5
│  @storybook/addon-docs: ^10.3.5 => 10.3.5
│  @storybook/addon-vitest: ^10.3.5 => 10.3.5
│  @storybook/html-vite: ^10.3.5 => 10.3.5
│  storybook: ^10.3.5 => 10.3.5

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions