Skip to content

Commit

Permalink
Merge pull request #17435 from storybookjs/fix-configure-error-handling
Browse files Browse the repository at this point in the history
Core: Ensure we show an error when `configure()` throws
  • Loading branch information
shilman committed Feb 12, 2022
1 parent a1cc55e commit 3100d03
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/api/src/modules/stories.ts
Expand Up @@ -10,6 +10,7 @@ import {
SET_STORIES,
STORY_SPECIFIED,
STORY_INDEX_INVALIDATED,
CONFIG_ERROR,
} from '@storybook/core-events';
import deprecate from 'util-deprecate';
import { logger } from '@storybook/client-logger';
Expand Down Expand Up @@ -513,6 +514,13 @@ export const init: ModuleFn = ({
}
);

fullAPI.on(CONFIG_ERROR, function handleConfigError(err) {
store.setState({
storiesConfigured: true,
storiesFailed: err,
});
});

if (FEATURES?.storyStoreV7) {
provider.serverChannel?.on(STORY_INDEX_INVALIDATED, () => fullAPI.fetchStoryList());
await fullAPI.fetchStoryList();
Expand Down
20 changes: 20 additions & 0 deletions lib/api/src/tests/stories.test.js
Expand Up @@ -6,6 +6,7 @@ import {
STORY_SPECIFIED,
STORY_PREPARED,
STORY_INDEX_INVALIDATED,
CONFIG_ERROR,
} from '@storybook/core-events';
import { EventEmitter } from 'events';
import global from 'global';
Expand Down Expand Up @@ -1136,6 +1137,25 @@ describe('stories API', () => {
});
});

describe('CONFIG_ERROR', () => {
it('shows error to user', async () => {
const navigate = jest.fn();
const store = createMockStore();
const fullAPI = Object.assign(new EventEmitter(), {});

const { api, init } = initStories({ store, navigate, provider, fullAPI });
Object.assign(fullAPI, api);

await init();

fullAPI.emit(CONFIG_ERROR, { message: 'Failed to run configure' });

const { storiesConfigured, storiesFailed } = store.getState();
expect(storiesConfigured).toBe(true);
expect(storiesFailed.message).toMatch(/Failed to run configure/);
});
});

describe('v2 SET_STORIES event', () => {
it('normalizes parameters and calls setStories for local stories', () => {
const fullAPI = Object.assign(new EventEmitter(), {
Expand Down

0 comments on commit 3100d03

Please sign in to comment.