Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mock modules in storybook's stories?

I have a Vue component which includes some external modules with complicated logic. For example:

// Component.vue
import Vue from 'vue';
import { externalModule } from '@/path/to/module';

export default {
  methods: {
    useExternalModule() {
      externalModule.doSomethig();
    }
  }
};

Is it possible to mock the externalModule inside the story?

I'm using Storybook v6.

like image 266
Ky6uk Avatar asked Aug 22 '20 13:08

Ky6uk


People also ask

How do you write a story in storybook?

A story captures the rendered state of a UI component. It's a function that returns a component's state given a set of arguments. Storybook uses the generic term arguments (args for short) when talking about React's props , Vue's props , Angular's @Input , and other similar concepts.


1 Answers

You can create a __mocks__ folder to put your mock components in. Then in your .storybook/main.js file use this to point webpack to your mock file.

module.exports = {
  // your Storybook configuration

  webpackFinal: (config) => {
    config.resolve.alias['externalModule'] = require.resolve('../__mocks__/externalModule.js');
    return config;
  },
};

This is covered in the docs under "Mocking imports".

However, this is a global configuration and not a story level configuration.

like image 160
Frank Ali Avatar answered Sep 20 '22 16:09

Frank Ali