Skip to content

Commit

Permalink
Update getStorybookMain to throw an error if stories are not found in…
Browse files Browse the repository at this point in the history
… main.js
  • Loading branch information
valentinpalkovic committed Mar 13, 2023
1 parent f2191b7 commit 9d8f112
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/util/__snapshots__/getStorybookMain.test.ts.snap
@@ -0,0 +1,9 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`getStorybookMain should throw an error if no configuration is found 1`] = `"Could not load main.js in .storybook. Is the config directory correct? You can change it by using --config-dir <path-to-dir>"`;
exports[`getStorybookMain should throw an error if no stories are found 1`] = `
"Could not find stories in main.js in .storybook.
If you are using a mono-repository, please run the test-runner only against your sub-package, which contains a .storybook folder with \\"stories\\" defined in main.js.
You can change the config directory by using --config-dir <path-to-dir>"
`;
12 changes: 11 additions & 1 deletion src/util/getStorybookMain.test.ts
Expand Up @@ -5,7 +5,17 @@ jest.mock('@storybook/core-common');

describe('getStorybookMain', () => {
it('should throw an error if no configuration is found', () => {
expect(() => getStorybookMain('.storybook')).toThrow();
expect(() => getStorybookMain('.storybook')).toThrowErrorMatchingSnapshot();
});

it('should throw an error if no stories are found', () => {
const mockedMain = {
stories: [],
};

jest.spyOn(coreCommon, 'serverRequire').mockImplementation(() => mockedMain);

expect(() => getStorybookMain('.storybook')).toThrowErrorMatchingSnapshot();
});

it('should return mainjs', () => {
Expand Down
12 changes: 12 additions & 0 deletions src/util/getStorybookMain.ts
@@ -1,6 +1,7 @@
import { join, resolve } from 'path';
import { serverRequire } from '@storybook/core-common';
import type { StorybookConfig } from '@storybook/types';
import dedent from 'ts-dedent';

let storybookMainConfig: StorybookConfig;

Expand All @@ -10,11 +11,22 @@ export const getStorybookMain = (configDir: string) => {
}

storybookMainConfig = serverRequire(join(resolve(configDir), 'main'));

if (!storybookMainConfig) {
throw new Error(
`Could not load main.js in ${configDir}. Is the config directory correct? You can change it by using --config-dir <path-to-dir>`
);
}

if (!storybookMainConfig.stories || storybookMainConfig.stories.length === 0) {
throw new Error(
dedent`
Could not find stories in main.js in ${configDir}.
If you are using a mono-repository, please run the test-runner only against your sub-package, which contains a .storybook folder with "stories" defined in main.js.
You can change the config directory by using --config-dir <path-to-dir>
`
);
}

return storybookMainConfig;
};

0 comments on commit 9d8f112

Please sign in to comment.