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]: @storybook/experimental-addon-test doesn't have access to the Nuxt instance when running in CLI #30418

Open
pbilyk opened this issue Jan 29, 2025 · 0 comments

Comments

@pbilyk
Copy link

pbilyk commented Jan 29, 2025

Describe the bug

I have a couple of stories that use and components, and they work fine, passing the component tests when run through the Storybook webapp.

However, when I run the tests through the CLI I get the [Error: [nuxt] instance unavailable] and the components that need Nuxt do not render at all resulting in failing tests.

My config is:

main.ts:

/** @type { import('storybook-vue').StorybookConfig } */
const config = {
  stories: ["../components/**/*.mdx", "../components/**/*.stories.@(js|jsx|ts|tsx|mdx)"],
  addons: [
    "@storybook/addon-links",
    "@storybook/addon-essentials",
    "@chromatic-com/storybook",
    "@storybook/addon-coverage",
    "@storybook/experimental-addon-test",
  ],
  framework: {
    name: "@storybook-vue/nuxt",
  },
  viteFinal: (config) => {
    config.resolve.alias = {
      ...config.resolve.alias,
      vue$: "vue/dist/vue.esm-bundler.js",
      vue: "vue/dist/vue.esm-bundler.js",
    };
    return config;
  },
  docs: {
    autodocs: "tag",
  },
};
export default config;

vitest.setup.ts:

import { beforeAll } from "vitest";
import { setProjectAnnotations } from "@storybook/vue3";
import * as previewAnnotations from "./preview";

const annotations = setProjectAnnotations([previewAnnotations]);

// Run Storybook's beforeAll hook
beforeAll(annotations.beforeAll);

vitest.config.ts:

import { fileURLToPath } from "node:url";
import { configDefaults, coverageConfigDefaults } from "vitest/config";
import { defineVitestConfig } from "@nuxt/test-utils/config";

export default defineVitestConfig({
  test: {
    globals: true,
    environment: "jsdom", // 'happy-dom' or 'jsdom'
    environmentOptions: {
      nuxt: {
        domEnvironment: "jsdom", // 'nuxt' or 'jsdom',
        overrides: {
          // @ts-ignore
          auth: {
            globalAppMiddleware: {
              isEnabled: false,
            },
          },
        },
      },
    },
    root: fileURLToPath(new URL("./", import.meta.url)),
    exclude: [...configDefaults.exclude],
    setupFiles: ["vitest-setup.ts"],
    coverage: {
      provider: "v8",
      reporter: [["text"], ["html"], ["lcov", { projectRoot: "../" }], ["json"]],
      // https://vitest.dev/config/#coverage-exclude
      exclude: [
        "tailwind.config.js",
        "**/.storybook/**",
        "**/*.stories.@(js|jsx|ts|tsx|mdx)",
        ...coverageConfigDefaults.exclude,
      ],
    },
  },
});

vitest.workspace.ts:

import { defineWorkspace } from "vitest/config";
import { storybookTest } from "@storybook/experimental-addon-test/vitest-plugin";
import path from "node:path";
import { fileURLToPath } from "node:url";

const dirname = typeof __dirname !== "undefined" ? __dirname : path.dirname(fileURLToPath(import.meta.url));

export default defineWorkspace([
  // This is the path to your existing Vitest config file
  "./vitest.config.ts",
  {
    // This is the path to your existing Vite config file
    extends: "./vite.config.ts",
    plugins: [
      storybookTest({
        // The location of your Storybook config, main.js|ts
        configDir: path.join(dirname, ".storybook"),
        // This should match your package.json script to run Storybook
        // The --ci flag will skip prompts and not open a browser
        storybookScript: "yarn storybook --ci",
      }),
    ],
    test: {
      name: "storybook",
      // Enable browser mode
      browser: {
        enabled: true,
        instances: [
          {
            browser: "chromium",
          },
        ],
        // Make sure to install Playwright
        provider: "playwright",
        headless: true,
      },
      setupFiles: ["./.storybook/vitest.setup.ts"],
    },
  },
]);

Reproduction link

http://example.com

Reproduction steps

No response

System

System:
    OS: macOS 15.2
    CPU: (10) arm64 Apple M1 Pro
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.18.0 - ~/.nvm/versions/node/v18.18.0/bin/node
    Yarn: 1.22.19 - ~/.yarn/bin/yarn <----- active
    npm: 9.5.1 - ~/.nvm/versions/node/v18.18.0/bin/npm
  Browsers:
    Chrome: 132.0.6834.111
    Edge: 132.0.2957.127
    Safari: 18.2
  npmPackages:
    @storybook/addon-coverage: ^1.0.5 => 1.0.5 
    @storybook/addon-essentials: 8.5.2 => 8.5.2 
    @storybook/addon-links: 8.5.2 => 8.5.2 
    @storybook/blocks: 8.5.2 => 8.5.2 
    @storybook/experimental-addon-test: ^8.5.2 => 8.5.2 
    @storybook/test: 8.5.2 => 8.5.2 
    @storybook/testing-vue3: ^1.0.0 => 1.0.0 
    @storybook/vue3-vite: ^8.5.2 => 8.5.2 
    storybook: 8.5.2 => 8.5.2

Additional context

No response

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

1 participant