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]: Overriding tsconfig aliases does not work #30186

Open
yoksel opened this issue Jan 4, 2025 · 0 comments
Open

[Bug]: Overriding tsconfig aliases does not work #30186

yoksel opened this issue Jan 4, 2025 · 0 comments

Comments

@yoksel
Copy link

yoksel commented Jan 4, 2025

Describe the bug

I'm trying to mock modules using overriding tsconfig aliases in .storybook/main.ts and it does not work for me

tsconfig:

{
  "compilerOptions": {
    "target": "ES2017",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "bundler",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "incremental": true,
    "baseUrl": "src/",
    "paths": {
      "@/util/*": ["util/*"],
      "@/testModule": ["util/testModule/index.ts"]
    }
  },
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
  "exclude": ["node_modules"]
}

Aliases replacements in .storybook/main.ts

webpackFinal: async (config) => {
  if (config.resolve) {
    config.resolve.alias = {
      ...config.resolve.alias,
      // Example from documentation:
      // '@/lib/db': path.resolve(__dirname, './lib/db.mock.ts'),
      // --------------------------------------------------------
      // WORKS — we can see replacement in TestComponent story
      nanoid: path.resolve(__dirname, './mocks/nanoid.ts'),
      // WORKS — we can see replacement in TestComponent story
      '../../util/testModule': path.resolve(__dirname, './mocks/testModule.ts'),
      // DOES NOT WORK
      // — we can NOT see replacement in TestComponent story
      // — if we provide broken path instead of real mock file, Storybook build won't fail because all these aliases replacements actually ignored
      '@/util': path.resolve(__dirname, './mocks/testModule.ts'),
      '@/util/testModule': path.resolve(__dirname, './mocks/testModule.ts'),
      '@/util/testModule$': path.resolve(__dirname, './mocks/testModule.ts'),
      '@/testModule': path.resolve(__dirname, './mocks/testModule.ts'),
      '@/testModule$': path.resolve(__dirname, './mocks/testModule.ts'),
    };
  }

  // console.log(config.resolve?.alias);

  return config;
},

Reproduction link

https://github.com/yoksel/storybook-aliases-bug/

Reproduction steps

  1. Install dependencies: npm i
  2. Run Storybook: npm run storybook
  3. Open Storybook and check component TestComponent: http://localhost:6007/?path=/story/components-testcomponent--default
  4. See Real content was not replaced by MOCKED content despite of rewriting aliases in .storybook/main.ts.

We also can provide broken paths instead of real mocks like:

'@/testModule': path.resolve(__dirname, './mocks/BROKEN_PATH.ts'),

And if aliases work, Storybook build should fail, but it does not, so replacing aliases for Storybook does not work.

System

Storybook Environment Info:

  System:
    OS: macOS 15.0
    CPU: (12) arm64 Apple M2 Pro
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.18.0 - ~/.nvm/versions/node/v18.18.0/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 9.8.1 - ~/.nvm/versions/node/v18.18.0/bin/npm <----- active
    pnpm: 8.10.0 - /usr/local/bin/pnpm
  Browsers:
    Chrome: 131.0.6778.205
    Edge: 131.0.2903.112
    Safari: 18.0
  npmPackages:
    @storybook/addon-essentials: ^8.4.7 => 8.4.7 
    @storybook/addon-interactions: ^8.4.7 => 8.4.7 
    @storybook/addon-onboarding: ^8.4.7 => 8.4.7 
    @storybook/blocks: ^8.4.7 => 8.4.7 
    @storybook/nextjs: ^8.4.7 => 8.4.7 
    @storybook/react: ^8.4.7 => 8.4.7 
    @storybook/test: ^8.4.7 => 8.4.7 
    eslint-plugin-storybook: ^0.11.2 => 0.11.2 
    storybook: ^8.4.7 => 8.4.7

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