Home

Awesome

<div align="center"> <h1> <nobr> <a href="https://storybookjs.github.io/storybook-addon-console"> <img src="https://github.com/storybookjs/storybook-addon-console/raw/master/docs/logo.png" alt="logo" height="140"> </a> <img src="https://github.com/storybookjs/storybook-addon-console/raw/master/docs/title.png" alt="Storybook Addon Console"> </nobr> </h1> </div> <br />

Storybook Addon Console

npm
version addon-console Storybook

Why

There're some cases when you can't / don't want / forgot to keep browser console opened. This addon helps you to get all console output in your storybook. In other cases, you might find it difficult to extract the desired information in the information noise issued by the console or to determine which component in what state gives the message. With this addon, you can control what you see and where you see.

We assume the following possible applications:

storybook-addon-console

try live demo

Install

yarn add -D @storybook/addon-console @storybook/addon-actions

Quick Start

Just import it in your storybook config.js:

// config.js

import '@storybook/addon-console';

That's all. You'll start to receive all console messages, warnings, errors in your action logger panel. Everything except HMR logs.

If you want to enable HMR messages, do the following:

// config.js

import { setConsoleOptions } from '@storybook/addon-console';

const panelExclude = setConsoleOptions({}).panelExclude;
setConsoleOptions({
  panelExclude: [...panelExclude, /deprecated/],
});

You'll receive console outputs as a console, warn and error actions in the panel. You might want to know from what stories they come. In this case, add withConsole decorator:

// preview.js

import type { Preview } from '@storybook/react';
import { withConsole } from '@storybook/addon-console';

const preview: Preview = {
  decorators: [(storyFn, context) => withConsole()(storyFn)(context)],
  // ...
};

After that your messages in Action Logger will be prefixed with the story path, like molecules/atoms/electron: ["ComponentDidMount"] or molecules/atoms/electron error: ["Warning: Failed prop type..."]. You can setup addon behavior by passing options to withConsole or setConsoleOptions methods, both have the same API.

Panel

Addon console don't have own UI panel to output logs, it use addon-console instead. Make sure that main.js contains this line:

// main.js

export default {
  addons: [
    "@storybook/addon-actions/register",
  ],
};

API

<a name="module_@storybook/addon-console"></a>

@storybook/addon-console

It handles console.log, console.warn, and console.error methods and not catched errors. By default, it just reflects all console messages in the Action Logger Panel (should be installed as a peerDependency) except [HMR] logs.

<a name="module_@storybook/addon-console.setConsoleOptions"></a>

@storybook/addon-console.setConsoleOptions(optionsOrFn) ⇒ <code>addonOptions</code>

Set addon options and returns a new one

Kind: static method of <code>@storybook/addon-console</code>
See

ParamType
optionsOrFn<code>addonOptions</code> | <code>optionsCallback</code>

Example

import { setConsoleOptions } from '@storybook/addon-console';

const panelExclude = setConsoleOptions({}).panelExclude;
setConsoleOptions({
  panelExclude: [...panelExclude, /deprecated/],
});

<a name="module_@storybook/addon-console.withConsole"></a>

@storybook/addon-console.withConsole([optionsOrFn]) ⇒ <code>function</code>

Wraps your stories with specified addon options. If you don't pass {log, warn, error} in options argument it'll create them from context for each story individually. Hence you'll see from what exact story you got a log or error. You can log from component's lifecycle methods or within your story.

Kind: static method of <code>@storybook/addon-console</code>
Returns: <code>function</code> - wrappedStoryFn
See

ParamType
[optionsOrFn]<code>addonOptions</code> | <code>optionsCallback</code>

Example

import type { Meta, StoryObj } from '@storybook/react';
import { withConsole } from '@storybook/addon-console';

const meta: Meta<typeof Button> = {
  title: 'Example/Button',
  component: Button,
};

export default meta;
type Story = StoryObj<typeof Button>;

export const Primary: Story = {
  args: {
    primary: true,
    label: 'Button',
    onClick: () => console.log(['Data:', 1, 3, 4]),
  },
};
 // Action Logger Panel:
 // withConsole/with Log: ["Data:", 1, 3, 4]

<a name="module_@storybook/addon-console..addonOptions"></a>

@storybook/addon-console~addonOptions : <code>Object</code>

This options could be passed to withConsole or setConsoleOptions

Kind: inner typedef of <code>@storybook/addon-console</code>
Properties

NameTypeDefaultDescription
[panelExclude]<code>[ 'Array' ].<RegExp></code><code>[/[HMR]/]</code>Optional. Anything matched to at least one of regular expressions will be excluded from output to Action Logger Panel
[panelInclude]<code>[ 'Array' ].<RegExp></code><code>[]</code>Optional. If set, only matched outputs will be shown in Action Logger. Higher priority than panelExclude.
[consoleExclude]<code>[ 'Array' ].<RegExp></code><code>[]</code>Optional. Anything matched to at least one of regular expressions will be excluded from DevTool console output
[consoleInclude]<code>[ 'Array' ].<RegExp></code><code>[]</code>Optional. If set, only matched outputs will be shown in console. Higher priority than consoleExclude.
[log]<code>string</code><code>"console"</code>Optional. The marker to display console.log outputs in Action Logger
[warn]<code>string</code><code>"warn"</code>Optional. The marker to display warnings in Action Logger
[error]<code>string</code><code>"error"</code>Optional. The marker to display errors in Action Logger

<a name="module_@storybook/addon-console..optionsCallback"></a>

@storybook/addon-console~optionsCallback ⇒ <code>addonOptions</code>

This callback could be passed to setConsoleOptions or withConsole

Kind: inner typedef of <code>@storybook/addon-console</code>
Returns: <code>addonOptions</code> - - new addonOptions

ParamTypeDescription
currentOptions<code>addonOptions</code>the current addonOptions

Example

import { withConsole } from `@storybook/addon-console`;

const optionsCallback = (options) => ({panelExclude: [...options.panelExclude, /Warning/]});
export default {
  title: 'Button',
  decorators: [withConsole(optionsCallback)],
};

Contributing

yarn storybook runs example Storybook. Then you can edit source code located in the src folder and example storybook in the stories folder.

Run tests

Run yarn test.

Run tests in watch mode

Run yarn tdd.

Test coverage

After running tests you can explore coverage details in .coverage/lcov-report/index.html

Debugging

If you're using VSCode you can debug tests and source by launching Jest Tests task from Debug Menu. It allows to set breakpoints in *.test.js and *.js files.

Readme editing

Please, don't edit this README.md directly. Run yarn dev:docs and change docs/readme.hbs and JSDoc comments withing src instead.

Credits

<div align="left" style="height: 16px;">Created with ❤︎ to <b>React</b> and <b>Storybook</b> by <a href="https://twitter.com/UsulPro">@usulpro</a> [<a href="https://github.com/react-theming">React Theming</a>] </div>