diff --git a/src/util/__snapshots__/getStorybookMain.test.ts.snap b/src/util/__snapshots__/getStorybookMain.test.ts.snap new file mode 100644 index 00000000..be237104 --- /dev/null +++ b/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 "`; + +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 " +`; diff --git a/src/util/getStorybookMain.test.ts b/src/util/getStorybookMain.test.ts index 57105ab2..eda17e77 100644 --- a/src/util/getStorybookMain.test.ts +++ b/src/util/getStorybookMain.test.ts @@ -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', () => { diff --git a/src/util/getStorybookMain.ts b/src/util/getStorybookMain.ts index 4e75ad04..950e4761 100644 --- a/src/util/getStorybookMain.ts +++ b/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; @@ -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 ` ); } + 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 + ` + ); + } + return storybookMainConfig; };