UNPKG

9.7 kBPlain TextView Raw
1[![npm version](https://badge.fury.io/js/%40storybook%2Faddon-console.svg)](https://badge.fury.io/js/%40storybook%2Faddon-console)
2[![addon-console](https://img.shields.io/npm/dt/@storybook/addon-console.svg)](https://github.com/storybooks/storybook-addon-console)
3[![Storybook](https://github.com/storybooks/storybook-addon-console/raw/master/docs/storybook.svg?sanitize=true)](https://storybooks.github.io/storybook-addon-console)
4
5# Storybook Addon Console
6
7Redirects console output into Action Logger Panel
8
9## Why
10
11There'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.
12
13We assume the following possible applications:
14
15- Mobile devices. You might want to make console output reachable for users when they need to work with your storybook from mobile browsers
16
17- Small screens. You can save your screen space keeping browser console closed
18
19- To filter your console output. You can independently configure both action logger and real console output in a wide range.
20
21- To associate console messages with a specific components/stories. You can see which story emits which message
22
23- To output some data into Action Logger from your deep components without importing `addon-actions` for that.
24
25[![storybook-addon-console](https://raw.githubusercontent.com/storybooks/storybook-addon-console/master/docs/storybook-addon-console.png)](#)
26
27### Install
28
29```shell
30npm i @storybook/addon-console --save-dev
31```
32
33### Quick Start
34
35Just import it in your storybook config.js:
36
37```js
38// config.js
39
40import '@storybook/addon-console';
41```
42
43That'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:
44
45```js
46// config.js
47
48import { setConsoleOptions } from '@storybook/addon-console';
49
50setConsoleOptions({
51 panelExclude: [],
52});
53```
54
55You'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:
56
57```js
58// config.js
59
60import { addDecorator } from '@storybook/react';
61import { withConsole } from '@storybook/addon-console';
62
63addDecorator((storyFn, context) => withConsole()(storyFn)(context));
64```
65
66After 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.
67
68
69## API
70<a name="module_@storybook/addon-console"></a>
71
72## @storybook/addon-console
73It 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.
74
75
76* [@storybook/addon-console](#module_@storybook/addon-console)
77 * _static_
78 * [.setConsoleOptions(optionsOrFn)](#module_@storybook/addon-console.setConsoleOptions) ⇒ <code>addonOptions</code>
79 * [.withConsole([optionsOrFn])](#module_@storybook/addon-console.withConsole) ⇒ <code>function</code>
80 * _inner_
81 * [~addonOptions](#module_@storybook/addon-console..addonOptions) : <code>Object</code>
82 * [~optionsCallback](#module_@storybook/addon-console..optionsCallback) ⇒ <code>addonOptions</code>
83
84<a name="module_@storybook/addon-console.setConsoleOptions"></a>
85
86### @storybook/addon-console.setConsoleOptions(optionsOrFn) ⇒ <code>addonOptions</code>
87Set addon options and returns a new one
88
89**Kind**: static method of [<code>@storybook/addon-console</code>](#module_@storybook/addon-console)
90**See**
91
92- addonOptions
93- optionsCallback
94
95
96| Param | Type |
97| --- | --- |
98| optionsOrFn | <code>addonOptions</code> \| <code>optionsCallback</code> |
99
100**Example**
101```js
102import { setConsoleOptions } from '@storybook/addon-console';
103
104const panelExclude = setConsoleOptions({}).panelExclude;
105setConsoleOptions({
106 panelExclude: [...panelExclude, /deprecated/],
107});
108```
109<a name="module_@storybook/addon-console.withConsole"></a>
110
111### @storybook/addon-console.withConsole([optionsOrFn]) ⇒ <code>function</code>
112Wraps your stories with specified addon options.
113If 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.
114
115**Kind**: static method of [<code>@storybook/addon-console</code>](#module_@storybook/addon-console)
116**Returns**: <code>function</code> - wrappedStoryFn
117**See**
118
119- [addonOptions](#storybookaddon-consolesetconsoleoptionsoptionsorfn--addonoptions)
120- [optionsCallback](#storybookaddon-consoleoptionscallback--addonoptions)
121
122
123| Param | Type |
124| --- | --- |
125| [optionsOrFn] | <code>addonOptions</code> \| <code>optionsCallback</code> |
126
127**Example**
128```js
129import { storiesOf } from '@storybook/react';
130import { withConsole } from '@storybook/addon-console';
131
132storiesOf('withConsole', module)
133 .addDecorator((storyFn, context) => withConsole()(storyFn)(context))
134 .add('with Log', () => <Button onClick={() => console.log('Data:', 1, 3, 4)}>Log Button</Button>)
135 .add('with Warning', () => <Button onClick={() => console.warn('Data:', 1, 3, 4)}>Warn Button</Button>)
136 .add('with Error', () => <Button onClick={() => console.error('Test Error')}>Error Button</Button>)
137 .add('with Uncatched Error', () =>
138 <Button onClick={() => console.log('Data:', T.buu.foo)}>Throw Button</Button>
139 )
140 // Action Logger Panel:
141 // withConsole/with Log: ["Data:", 1, 3, 4]
142 // withConsole/with Warning warn: ["Data:", 1, 3, 4]
143 // withConsole/with Error error: ["Test Error"]
144 // withConsole/with Uncatched Error error: ["Uncaught TypeError: Cannot read property 'foo' of undefined", "http://localhost:9009/static/preview.bundle.js", 51180, 42, Object]
145```
146<a name="module_@storybook/addon-console..addonOptions"></a>
147
148### @storybook/addon-console~addonOptions : <code>Object</code>
149This options could be passed to [withConsole](#storybookaddon-consolewithconsoleoptionsorfn--function) or [setConsoleOptions](#module_@storybook/addon-console.setConsoleOptions)
150
151**Kind**: inner typedef of [<code>@storybook/addon-console</code>](#module_@storybook/addon-console)
152**Properties**
153
154| Name | Type | Default | Description |
155| --- | --- | --- | --- |
156| [panelExclude] | <code>Array.&lt;RegExp&gt;</code> | <code>[/[HMR]/]</code> | Optional. Anything matched to at least one of regular expressions will be excluded from output to Action Logger Panel |
157| [panelInclude] | <code>Array.&lt;RegExp&gt;</code> | <code>[]</code> | Optional. If set, only matched outputs will be shown in Action Logger. Higher priority than `panelExclude`. |
158| [consoleExclude] | <code>Array.&lt;RegExp&gt;</code> | <code>[]</code> | Optional. Anything matched to at least one of regular expressions will be excluded from DevTool console output |
159| [consoleInclude] | <code>Array.&lt;RegExp&gt;</code> | <code>[]</code> | Optional. If set, only matched outputs will be shown in console. Higher priority than `consoleExclude`. |
160| [log] | <code>string</code> | <code>&quot;console&quot;</code> | Optional. The marker to display `console.log` outputs in Action Logger |
161| [warn] | <code>string</code> | <code>&quot;warn&quot;</code> | Optional. The marker to display warnings in Action Logger |
162| [error] | <code>string</code> | <code>&quot;error&quot;</code> | Optional. The marker to display errors in Action Logger |
163
164<a name="module_@storybook/addon-console..optionsCallback"></a>
165
166### @storybook/addon-console~optionsCallback ⇒ <code>addonOptions</code>
167This callback could be passed to [setConsoleOptions](setConsoleOptions) or [withConsole](withConsole)
168
169**Kind**: inner typedef of [<code>@storybook/addon-console</code>](#module_@storybook/addon-console)
170**Returns**: <code>addonOptions</code> - - new [addonOptions](addonOptions)
171
172| Param | Type | Description |
173| --- | --- | --- |
174| currentOptions | <code>addonOptions</code> | the current [addonOptions](addonOptions) |
175
176**Example**
177```js
178import { withConsole } from `@storybook/addon-console`;
179
180const optionsCallback = (options) => ({panelExclude: [...options.panelExclude, /Warning/]});
181addDecorator((storyFn, context) => withConsole(optionsCallback)(storyFn)(context));
182```
183
184
185## Contributing
186
187`npm start` runs example Storybook. Then you can edit source code located in the `src` folder and example storybook in the `stories` folder.
188
189### Run tests
190
191Run `npm run test`. It starts jest test in `watch` mode.
192
193### Test coverage
194
195After running tests you can explore coverage details in `.coverage/lcov-report/index.html`
196
197### Debugging
198
199If 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.
200
201### Readme editing
202
203Please, don't edit this `README.md` directly. Run `npm run dev:docs` and change `docs/readme.hbs` and JSDoc comments withing `src` instead.
204
205## Credits
206
207<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>]
208</div>