Skip to content

sb.mock() doesn't work - mocked(...).mockReturnValue is not a function #999

Description

@briete

Environment

  • Operating system macOS 25.1.0
  • CPU Apple M1 Pro (10 cores)
  • Node.js version v22.13.1
  • nuxt/cli version 3.32.0
  • Package manager yarn 4.6.0
  • Nuxt version 3.13.2
  • Nitro version 2.13.1
  • Builder vite 7.3.0
  • Config -
  • Modules -

Reproduction

NA

Describe the bug

sb.mock() in .storybook/preview.ts doesn't work with @storybook-vue/nuxt. The mocked() helper returns the original function instead of a mock function.

Steps to Reproduce

  1. Set up module mocking in .storybook/preview.ts:
import { sb } from "storybook/test";

sb.mock(import("../composables/useMyComposable.ts"), { spy: true });
  1. Use mocked() in a story:
import { mocked, fn } from "storybook/test";
import { useMyComposable } from "../composables/useMyComposable.ts";

export const Default: Story = {
  beforeEach: async () => {
    mocked(useMyComposable).mockReturnValue({
      // mock return value
    });
  },
};
  1. Run Storybook and navigate to the story

Expected Behavior

mocked(useMyComposable) should return a mock function with mockReturnValue, mockImplementation, etc.

Actual Behavior

TypeError: mocked(...).mockReturnValue is not a function

The mocked() function returns the original function unchanged, without mock capabilities.

Workaround

It worked by adding the following code to .storybook/main.ts

import { createRequire } from "node:module";
import { dirname, join } from "node:path";

const config: StorybookConfig = {
  // ... other config
  
  async viteFinal(config, options) {
    // Call @storybook/builder-vite's viteFinal to enable viteMockPlugin
    const require = createRequire(import.meta.url);
    const vue3VitePath = dirname(require.resolve("@storybook/vue3-vite/package.json"));
    const builderVitePresetPath = join(vue3VitePath, "node_modules/@storybook/builder-vite/dist/preset.js");
    const { viteFinal: builderViteFinal } = await import(builderVitePresetPath);
    return builderViteFinal(config, options);
  },
};

Additional context

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions