Skip to content

Commit

Permalink
warn the user when stories glob does not match any file
Browse files Browse the repository at this point in the history
  • Loading branch information
yannbf committed Mar 3, 2023
1 parent 3ea1a30 commit f70204c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
19 changes: 18 additions & 1 deletion code/lib/core-server/src/utils/StoryIndexGenerator.test.ts
Expand Up @@ -11,7 +11,7 @@ import { normalizeStoriesEntry } from '@storybook/core-common';
import type { NormalizedStoriesSpecifier, StoryIndexer, StoryIndexEntry } from '@storybook/types';
import { loadCsf, getStorySortParameter } from '@storybook/csf-tools';
import { toId } from '@storybook/csf';
import { logger } from '@storybook/node-logger';
import { logger, once } from '@storybook/node-logger';

import { StoryIndexGenerator } from './StoryIndexGenerator';

Expand Down Expand Up @@ -61,6 +61,7 @@ describe('StoryIndexGenerator', () => {
const actual = jest.requireActual('@storybook/csf-tools');
loadCsfMock.mockImplementation(actual.loadCsf);
jest.mocked(logger.warn).mockClear();
jest.mocked(once.warn).mockClear();
});
describe('extraction', () => {
const storiesSpecifier: NormalizedStoriesSpecifier = normalizeStoriesEntry(
Expand Down Expand Up @@ -925,6 +926,22 @@ describe('StoryIndexGenerator', () => {
});
});

describe('warnings', () => {
it('when entries do not match any files', async () => {
const generator = new StoryIndexGenerator(
[normalizeStoriesEntry('./src/docs2/wrong.js', options)],
options
);
await generator.initialize();
await generator.getIndex();

expect(once.warn).toHaveBeenCalledTimes(1);
expect(jest.mocked(once.warn).mock.calls[0][0]).toMatchInlineSnapshot(
`"No story files found for the specified pattern: src/docs2/wrong.js"`
);
});
});

describe('duplicates', () => {
it('warns when two MDX entries reference the same CSF file without a name', async () => {
const docsErrorSpecifier: NormalizedStoriesSpecifier = normalizeStoriesEntry(
Expand Down
11 changes: 10 additions & 1 deletion code/lib/core-server/src/utils/StoryIndexGenerator.ts
Expand Up @@ -21,7 +21,7 @@ import type {
} from '@storybook/types';
import { userOrAutoTitleFromSpecifier, sortStoriesV7 } from '@storybook/preview-api';
import { normalizeStoryPath } from '@storybook/core-common';
import { logger } from '@storybook/node-logger';
import { logger, once } from '@storybook/node-logger';
import { getStorySortParameter } from '@storybook/csf-tools';
import { toId } from '@storybook/csf';
import { analyze } from '@storybook/docs-mdx';
Expand Down Expand Up @@ -122,6 +122,15 @@ export class StoryIndexGenerator {
path.join(this.options.workingDir, specifier.directory, specifier.files)
);
const files = await glob(fullGlob);

if (files.length === 0) {
once.warn(
`No story files found for the specified pattern: ${chalk.blue(
path.join(specifier.directory, specifier.files)
)}`
);
}

files.sort().forEach((absolutePath: Path) => {
const ext = path.extname(absolutePath);
if (ext === '.storyshot') {
Expand Down

0 comments on commit f70204c

Please sign in to comment.