storybooks/storybook-addon-console

Name: storybook-addon-console

Owner: Storybook

Description: storybook-addon. Redirects console output into action logger panel

Created: 2017-09-04 13:10:23.0

Updated: 2018-04-23 18:56:52.0

Pushed: 2017-10-03 11:26:21.0

Homepage: https://storybooks.github.io/storybook-addon-console

Size: 663

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

npm version Live demo

Storybook Addon Console

Redirects console output into Action Logger Panel

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

Install
i @storybook/addon-console --save-dev
Quick Start

Just import it in your storybook config.js:

onfig.js

rt '@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:

onfig.js

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

onsoleOptions({
nelExclude: [],

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:

onfig.js

rt { addDecorator } from '@storybook/react';
rt { withConsole } from '@storybook/addon-console';

ecorator((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.

API

@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.

@storybook/addon-console.setConsoleOptions(optionsOrFn) ? addonOptions

Set addon options and returns a new one

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

| Param | Type | | — | — | | optionsOrFn | addonOptions \| optionsCallback |

Example

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

t panelExclude = setConsoleOptions({}).panelExclude;
onsoleOptions({
nelExclude: [...panelExclude, /deprecated/],

@storybook/addon-console.withConsole([optionsOrFn]) ? function

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 @storybook/addon-console
Returns: function - wrappedStoryFn
See

| Param | Type | | — | — | | [optionsOrFn] | addonOptions \| optionsCallback |

Example

rt { storiesOf } from '@storybook/react';
rt { withConsole } from '@storybook/addon-console';

iesOf('withConsole', module)
dDecorator((storyFn, context) => withConsole()(storyFn)(context))
d('with Log', () => <Button onClick={() => console.log('Data:', 1, 3, 4)}>Log Button</Button>)
d('with Warning', () => <Button onClick={() => console.warn('Data:', 1, 3, 4)}>Warn Button</Button>)
d('with Error', () => <Button onClick={() => console.error('Test Error')}>Error Button</Button>)
d('with Uncatched Error', () =>
Button onClick={() => console.log('Data:', T.buu.foo)}>Throw Button</Button>

Action Logger Panel:
withConsole/with Log: ["Data:", 1, 3, 4]
withConsole/with Warning warn: ["Data:", 1, 3, 4]
withConsole/with Error error: ["Test Error"]
withConsole/with Uncatched Error error: ["Uncaught TypeError: Cannot read property 'foo' of undefined", "http://localhost:9009/static/preview.bundle.js", 51180, 42, Object]

@storybook/addon-console~addonOptions : Object

This options could be passed to withConsole or setConsoleOptions

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

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

@storybook/addon-console~optionsCallback ? addonOptions

This callback could be passed to setConsoleOptions or withConsole

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

| Param | Type | Description | | — | — | — | | currentOptions | addonOptions | the current addonOptions |

Example

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

t optionsCallback = (options) => ({panelExclude: [...options.panelExclude, /Warning/]});
ecorator((storyFn, context) => withConsole(optionsCallback)(storyFn)(context));
Contributing

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

Run tests

Run npm run test. It starts jest test in watch mode.

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 npm run dev:docs and change docs/readme.hbs and JSDoc comments withing src instead.

Credits
Created with ?? to React and React Storybook by @UsulPro and React Theming.

This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.