Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CSF: Warn when there are no exported stories #10357

Merged
merged 2 commits into from
Apr 9, 2020
Merged
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
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