NickMcBurney / storybook-vue3-router

A Storybook decorator that allows you to use Vue 3 / vue-router@4 routing-aware components.
MIT License
29 stars 8 forks source link

`vueRouter` import in a story produce an error in storyshot #27

Closed budet-b closed 2 years ago

budet-b commented 2 years ago

I have a story with a vueRouter mocked with custom routes like this:

const customRoutes = [
  {
    path: '/campaign/<id>',
    name: 'Campaign',
    params: {
      campaignId: '<id>'
    },
    component: MetricContent
  }
];

And I declare my template like this:

export const MetricContentView = defaultTemplate.bind({});
MetricContentView.args = defaultProps;
MetricContentView.decorators = [vueRouter(customRoutes)];

When I start storybook it works well as expected but I need to create some storyshots I have the following:

 Unexpected error while loading ./index.stories.js: Cannot use import statement outside a module
     Jest encountered an unexpected token

    Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.

    Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

    By default "node_modules" folder is ignored by transformers.

    Here's what you can do:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
     • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/configuration
    For information about custom transformations, see:
    https://jestjs.io/docs/code-transformation

    Details:

    /Users/xxx/node_modules/storybook-vue3-router/dist/esm/index.js:5
    import vueRouter from './withVueRouter';
    ^^^^^^

    SyntaxError: Cannot use import statement outside a module
        at new Script (node:vm:100:7)
        at Runtime.createScriptFromCode (/Users/xxx/node_modules/jest-runtime/build/index.js:1728:14)
        at Runtime._execModule (/Users/xxx/node_modules/jest-runtime/build/index.js:1596:25)
        at Runtime._loadModule (/Users/xxx/node_modules/jest-runtime/build/index.js:1185:12)
        at Runtime.requireModule (/Users/xxx/node_modules/jest-runtime/build/index.js:1009:12)
        at Runtime.requireModuleOrMock (/Users/xxx/node_modules/jest-runtime/build/index.js:1210:21)
        at Object.<anonymous> (/Users/xxx/src/sections/CampaignDetailed/HeaderBlock/components/MetricContent/index.stories.js:2:1)
        at Runtime._execModule (/Users/xxx/node_modules/jest-runtime/build/index.js:1646:24)
        at Runtime._loadModule (/Users/xxx/node_modules/jest-runtime/build/index.js:1185:12)
        at Runtime.requireModule (/Users/xxx/node_modules/jest-runtime/build/index.js:1009:12)

      at Object.error (node_modules/@storybook/client-logger/dist/cjs/index.js:74:67)
      at node_modules/@storybook/core-client/dist/cjs/preview/executeLoadable.js:84:32
          at Array.forEach (<anonymous>)
      at node_modules/@storybook/core-client/dist/cjs/preview/executeLoadable.js:77:18
          at Array.forEach (<anonymous>)
      at executeLoadable (node_modules/@storybook/core-client/dist/cjs/preview/executeLoadable.js:76:10)
      at executeLoadableForChanges (node_modules/@storybook/core-client/dist/cjs/preview/executeLoadable.js:127:20)

Did I miss something in the configuration ?

budet-b commented 2 years ago

Or do you know how can I mock this module with Jest ? I've try something like:

jest.mock('storybook-vue3-router');
import vueRouter from 'storybook-vue3-router';
const mockedDependency = vueRouter as jest.Mocked<typeof vueRouter>;
export default mockedDependency;

and reference it in jest.config but with no result.

Do you have an idea @NickMcBurney ?

NickMcBurney commented 2 years ago

For the initial you mentioned, you could try using require rather than import.

I'm not sure why you would want to use this package within a jest test, it's specifically built for Storybook not jest and won't work outside of Storybook - you probably ne next importing full vue-router for this.

NickMcBurney commented 2 years ago

@budet-b, would it be possible for you to provide a small reproduction repo? If you can I'd be happy to investigate for you.

NickMcBurney commented 2 years ago

Closing as stale.