Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/index.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* It handles `console.log`, `console.warn`, `console.dir`, 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.
* It handles `console.log`, `console.warn`, `console.dir`, `console.error` and `console.info` 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.
* @module @storybook/addon-console
*
*
Expand All @@ -22,6 +22,7 @@ const cLogger = {
warn: logger.warn.bind(logger),
error: logger.error.bind(logger),
dir: logger.dir.bind(logger),
info: logger.info.bind(logger),
};

/**
Expand All @@ -34,6 +35,7 @@ const cLogger = {
* @property {string} [warn = warn] - Optional. The marker to display warnings in Action Logger
* @property {string} [error = error] - Optional. The marker to display errors in Action Logger
* @property {string} [dir = dir] - Optional. The marker to display `console.dir` outputs in Action Logger
* @property {string} [info = info] - Optional. The marker to display `console.info` outputs in Action Logger
*/
const addonOptions = {
panelExclude: [/\[HMR\]/],
Expand All @@ -44,6 +46,7 @@ const addonOptions = {
warn: 'warn',
error: 'error',
dir: 'dir',
info: 'info',
};

let currentOptions = addonOptions;
Expand All @@ -53,6 +56,7 @@ const createLogger = options => ({
warn: action(options.warn),
error: action(options.error),
dir: action(options.dir),
info: action(options.info),
});

const shouldDisplay = (messages, exclude, include) => {
Expand Down Expand Up @@ -101,6 +105,13 @@ function setScope(options) {
if (toConsole.length) cLogger.dir(...toConsole);
};

logger.info = (...args) => {
const toPanel = shouldDisplay(args, panelExclude, panelInclude);
const toConsole = shouldDisplay(args, consoleExclude, consoleInclude);
if (toPanel.length) aLogger.info(...toPanel);
if (toConsole.length) cLogger.info(...toConsole);
};

global.onerror = (...args) => {
const toPanel = shouldDisplay([args[0]], panelExclude, panelInclude);
const toConsole = shouldDisplay([args[0]], consoleExclude, consoleInclude);
Expand Down Expand Up @@ -188,6 +199,7 @@ function addConsole(storyFn, context, consoleOptions) {
warn: `${context.kind}/${context.story}/warn`,
error: `${context.kind}/${context.story}/error`,
dir: `${context.kind}/${context.story}/dir`,
info: `${context.kind}/${context.story}/info`,
}
: {};

Expand All @@ -213,7 +225,7 @@ function addConsole(storyFn, context, consoleOptions) {

/**
* Wraps your stories with specified addon options.
* If you don't pass {`log`, `warn`, `error`, `dir`} 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.
* If you don't pass {`log`, `warn`, `error`, `dir`, `info`} 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.
* @param {addonOptions|optionsCallback} [optionsOrFn]
* @see [addonOptions]{@link #storybookaddon-consolesetconsoleoptionsoptionsorfn--addonoptions}
* @see [optionsCallback]{@link #storybookaddon-consoleoptionscallback--addonoptions}
Expand Down
15 changes: 15 additions & 0 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ const consoleLog = jest.fn();
const consoleWarn = jest.fn();
const consoleError = jest.fn();
const consoleDir = jest.fn();
const consoleInfo = jest.fn();

global.console = {
log: consoleLog,
warn: consoleWarn,
error: consoleError,
dir: consoleDir,
info: consoleInfo,
};

const { withConsole, setConsoleOptions } = require('./');
Expand Down Expand Up @@ -43,6 +45,7 @@ describe('addon Console', () => {
warn: 'warn',
error: 'error',
dir: 'dir',
info: 'info',
};
describe('global scope', () => {
describe('check options `setConsoleOptions`', () => {
Expand Down Expand Up @@ -114,6 +117,13 @@ describe('addon Console', () => {
expect(consoleDir.mock.calls[0]).toEqual([logString]);
});

it('should output `console.info` to panel and console', () => {
logger.info(logString);
expect(aLogResults.msg).toBe(defaultOptions.info);
expect(aLogResults.data).toEqual([logString]);
expect(consoleInfo.mock.calls[0]).toEqual([logString]);
});

it('should catch error and output to panel and console', () => {
const isCatched = global.window.onerror.call(global, logString, 'url', 2);
expect(aLogResults.msg).toBe(defaultOptions.error);
Expand Down Expand Up @@ -198,6 +208,11 @@ describe('addon Console', () => {
expect(aLogResults).toEqual({});
expect(consoleLog.mock.calls[0]).toBeUndefined();
});
it('should not info anything at all', () => {
logger.info(logString);
expect(aLogResults).toEqual({});
expect(consoleLog.mock.calls[0]).toBeUndefined();
});
it('should not log even objects', () => {
const logObj = [1, 2, { foo: 'buu' }, [logString]];
logger.log(logObj);
Expand Down