Frequently Asked Questions

Here are some answers to frequently asked questions. If you have a question, you can ask it by opening an issue on the Storybook Repository.

How can I run coverage tests with Create React App and leave out stories?

Create React App does not allow providing options to Jest in your package.json, however you can run jest with commandline arguments:

  1. npm test -- --coverage --collectCoverageFrom='["src/**/*.{js,jsx}","!src/**/stories/*"]'

I see ReferenceError: React is not defined when using storybooks with Next.js

Next automatically defines React for all of your files via a babel plugin. You must define React for JSX to work. You can solve this either by:

How do I setup Storybook to share Webpack configuration with Next.js?

You can generally reuse webpack rules fairly easily by placing them in a file that is require()-ed from both your next.config.js and your .storybook/webpack.config.js files. For example, this gist sets both next.js and storybook up with global stylesheets.

Why is there no addons channel?

A common error is that an addon tries to access the “channel”, but the channel is not set. This can happen in a few different cases:

  • You’re trying to access addon channel (e.g. by calling setOptions) in a non-browser environment like Jest. You may need to add a channel mock:
  1. import addons, { mockChannel } from '@storybook/addons';
  2. addons.setChannel(mockChannel());
  • In React Native, it’s a special case that’s documented in #1192

Can I modify React component state in stories?

Not directly. If you control the component source, you can do something like this:

  1. import React, { Component} from 'react';
  2. import { storiesOf } from '@storybook/react';
  3. class MyComponent extends Component {
  4. constructor(props) {
  5. super(props)
  6. this.state = {
  7. someVar: 'defaultValue',
  8. ...props.initialState
  9. }
  10. }
  11. // ...
  12. }
  13. storiesOf('MyComponent', module)
  14. .add('default', () => <MyComponent />)
  15. .add('other', () => <MyComponent initialState={{ someVar: 'otherVal' }} />);