Skip to content

Commit

Permalink
Merge pull request #10357 from storybookjs/feat/warn-with-no-exported…
Browse files Browse the repository at this point in the history
…-stories

CSF: Warn when there are no exported stories
  • Loading branch information
shilman authored Apr 9, 2020
2 parents 465c4cc + e7a95ed commit 7c20b3b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
35 changes: 35 additions & 0 deletions lib/core/src/client/preview/loadCsf.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { ConfigApi, ClientApi, StoryStore } from '@storybook/client-api';
import { logger } from '@storybook/client-logger';
import { RequireContext } from './types';

import { loadCsf } from './loadCsf';

jest.mock('@storybook/client-logger', () => ({
logger: { warn: jest.fn() },
}));

let cbs: ((data: any) => void)[];
let mod: NodeModule;
beforeEach(() => {
Expand Down Expand Up @@ -365,4 +370,34 @@ describe('core.preview.loadCsf', () => {
expect(storyStore.incrementRevision).toHaveBeenCalled();
expect(mockedStoriesOf).toHaveBeenCalledWith('a', true);
});

it('gives a warning if there are no exported stories', () => {
const { configure } = makeMocks();

const input = {
a: {
default: {
title: 'MissingExportsComponent',
},
// no named exports, will not present a story
},
};
configure(makeRequireContext(input), mod, 'react');
expect(logger.warn).toHaveBeenCalled();
});

it('does not give a warning if there are exported stories', () => {
const { configure } = makeMocks();

const input = {
a: {
default: {
title: 'MissingExportsComponent',
},
x: () => 0,
},
};
configure(makeRequireContext(input), mod, 'react');
expect(logger.warn).not.toHaveBeenCalled();
});
});
12 changes: 11 additions & 1 deletion lib/core/src/client/preview/loadCsf.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ConfigApi, ClientApi, StoryStore } from '@storybook/client-api';
import { isExportStory, storyNameFromExport, toId } from '@storybook/csf';
import { logger } from '@storybook/client-logger';
import dedent from 'ts-dedent';

import { Loadable, LoaderFunction, RequireContext } from './types';

Expand Down Expand Up @@ -113,7 +114,16 @@ const loadStories = (
kind.addDecorator(decorator);
});

Object.keys(exports).forEach((key) => {
const storyExports = Object.keys(exports);
if (storyExports.length === 0) {
logger.warn(
dedent`Found a story file for "${kindName}" but no exported stories.
Check the docs for reference: https://storybook.js.org/docs/formats/component-story-format/`
);
return;
}

storyExports.forEach((key) => {
if (isExportStory(key, meta)) {
const storyFn = exports[key];
const { name, parameters, decorators, args, argTypes } = storyFn.story || {};
Expand Down

0 comments on commit 7c20b3b

Please sign in to comment.