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

Update getStorybookMain to throw an error if stories are not found in main.js #278

Merged
merged 1 commit into from
Mar 15, 2023
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
15 changes: 15 additions & 0 deletions src/util/__snapshots__/getStorybookMain.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`getStorybookMain no stories should throw an error if no stories are defined 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>"
`;
exports[`getStorybookMain no stories should throw an error if stories list is empty 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>"
`;
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>"`;
22 changes: 20 additions & 2 deletions src/util/getStorybookMain.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
import { getStorybookMain } from './getStorybookMain';
import { getStorybookMain, resetStorybookMainCache } from './getStorybookMain';
import * as coreCommon from '@storybook/core-common';

jest.mock('@storybook/core-common');

describe('getStorybookMain', () => {
beforeEach(() => {
resetStorybookMainCache();
});

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

describe('no stories', () => {
it('should throw an error if no stories are defined', () => {
jest.spyOn(coreCommon, 'serverRequire').mockImplementation(() => ({}));

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

it('should throw an error if stories list is empty', () => {
jest.spyOn(coreCommon, 'serverRequire').mockImplementation(() => ({ stories: [] }));

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

it('should return mainjs', () => {
Expand Down
30 changes: 24 additions & 6 deletions src/util/getStorybookMain.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,38 @@
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;
let storybookMainConfig = new Map<string, StorybookConfig>();

export const getStorybookMain = (configDir: string) => {
if (storybookMainConfig) {
return storybookMainConfig;
if (storybookMainConfig.has(configDir)) {
return storybookMainConfig.get(configDir);
} else {
storybookMainConfig.set(configDir, serverRequire(join(resolve(configDir), 'main')));
}

storybookMainConfig = serverRequire(join(resolve(configDir), 'main'));
if (!storybookMainConfig) {
const mainConfig = storybookMainConfig.get(configDir);

if (!mainConfig) {
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>`
);
}

return storybookMainConfig;
if (!mainConfig.stories || mainConfig.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 mainConfig;
};

export function resetStorybookMainCache() {
storybookMainConfig.clear();
}