From 17c08a8d64e580a460d3ad74b0f53fd9cd14b5f5 Mon Sep 17 00:00:00 2001 From: Tom Coleman Date: Wed, 19 Oct 2022 12:42:01 +1100 Subject: [PATCH] Don't generate docs page entries for CSF files with no stories --- .../src/utils/StoryIndexGenerator.test.ts | 20 +++++++++++++++++++ .../src/utils/StoryIndexGenerator.ts | 2 +- .../__mockdata__/errors/NoStories.stories.ts | 5 +++++ 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 code/lib/core-server/src/utils/__mockdata__/errors/NoStories.stories.ts 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, +};