diff --git a/code/lib/core-server/src/utils/StoryIndexGenerator.test.ts b/code/lib/core-server/src/utils/StoryIndexGenerator.test.ts index edc0379b5502..cae6ef1110ab 100644 --- a/code/lib/core-server/src/utils/StoryIndexGenerator.test.ts +++ b/code/lib/core-server/src/utils/StoryIndexGenerator.test.ts @@ -1,3 +1,5 @@ +/// ; + import path from 'path'; import fs from 'fs-extra'; import { normalizeStoriesEntry } from '@storybook/core-common'; @@ -462,6 +464,24 @@ describe('StoryIndexGenerator', () => { } `); }); + + // https://github.com/storybookjs/storybook/issues/19142 + it('does not generate a docs page entry if there are no stories in the CSF file', async () => { + const csfSpecifier: NormalizedStoriesSpecifier = normalizeStoriesEntry( + './errors/NoStories.stories.ts', + options + ); + + const generator = new StoryIndexGenerator([csfSpecifier], docsPageOptions); + await generator.initialize(); + + expect(await generator.getIndex()).toMatchInlineSnapshot(` + Object { + "entries": Object {}, + "v": 4, + } + `); + }); }); describe('docs specifier', () => { diff --git a/code/lib/core-server/src/utils/StoryIndexGenerator.ts b/code/lib/core-server/src/utils/StoryIndexGenerator.ts index 43eb77997743..101c149210ca 100644 --- a/code/lib/core-server/src/utils/StoryIndexGenerator.ts +++ b/code/lib/core-server/src/utils/StoryIndexGenerator.ts @@ -217,7 +217,7 @@ export class StoryIndexGenerator { } }); - if (this.options.docs.enabled) { + if (this.options.docs.enabled && csf.stories.length) { // We always add a template for *.stories.mdx, but only if docs page is enabled for // regular CSF files if (storyIndexer.addDocsTemplate || this.options.docs.docsPage) { diff --git a/code/lib/core-server/src/utils/__mockdata__/errors/NoStories.stories.ts b/code/lib/core-server/src/utils/__mockdata__/errors/NoStories.stories.ts new file mode 100644 index 000000000000..bb9598181ded --- /dev/null +++ b/code/lib/core-server/src/utils/__mockdata__/errors/NoStories.stories.ts @@ -0,0 +1,5 @@ +/* eslint-disable storybook/story-exports */ +const component = {}; +export default { + component, +};