Skip to content

Commit

Permalink
Merge pull request #21496 from storybookjs/21478-fail-dupe-story-docs
Browse files Browse the repository at this point in the history
  • Loading branch information
tmeasday authored Mar 9, 2023
2 parents e0ee29c + 36c1842 commit 2d280e6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 54 deletions.
59 changes: 11 additions & 48 deletions code/lib/core-server/src/utils/StoryIndexGenerator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -942,7 +942,7 @@ describe('StoryIndexGenerator', () => {
});

describe('duplicates', () => {
it('warns when two MDX entries reference the same CSF file without a name', async () => {
it('errors when two MDX entries reference the same CSF file without a name', async () => {
const docsErrorSpecifier: NormalizedStoriesSpecifier = normalizeStoriesEntry(
'./errors/**/A.mdx',
options
Expand All @@ -954,25 +954,12 @@ describe('StoryIndexGenerator', () => {
);
await generator.initialize();

expect(Object.keys((await generator.getIndex()).entries)).toMatchInlineSnapshot(`
Array [
"a--story-one",
"componentreference--docs",
"a--metaof",
"notitle--docs",
"a--second-docs",
"docs2-yabbadabbadooo--docs",
"a--docs",
]
`);

expect(logger.warn).toHaveBeenCalledTimes(1);
expect(jest.mocked(logger.warn).mock.calls[0][0]).toMatchInlineSnapshot(
`"🚨 You have two component docs pages with the same name A:docs. Use \`<Meta of={} name=\\"Other Name\\">\` to distinguish them."`
await expect(generator.getIndex()).rejects.toThrowErrorMatchingInlineSnapshot(
`"Unable to index ./errors/A.mdx,./errors/duplicate/A.mdx"`
);
});

it('warns when a MDX entry has the same name as a story', async () => {
it('errors when a MDX entry has the same name as a story', async () => {
const docsErrorSpecifier: NormalizedStoriesSpecifier = normalizeStoriesEntry(
'./errors/MetaOfClashingName.mdx',
options
Expand All @@ -984,24 +971,12 @@ describe('StoryIndexGenerator', () => {
);
await generator.initialize();

expect(Object.keys((await generator.getIndex()).entries)).toMatchInlineSnapshot(`
Array [
"a--story-one",
"componentreference--docs",
"a--metaof",
"notitle--docs",
"a--second-docs",
"docs2-yabbadabbadooo--docs",
]
`);

expect(logger.warn).toHaveBeenCalledTimes(1);
expect(jest.mocked(logger.warn).mock.calls[0][0]).toMatchInlineSnapshot(
`"🚨 You have a story for A with the same name as your component docs page (Story One), so the docs page is being dropped. Use \`<Meta of={} name=\\"Other Name\\">\` to distinguish them."`
await expect(generator.getIndex()).rejects.toThrowErrorMatchingInlineSnapshot(
`"Unable to index ./src/A.stories.js,./errors/MetaOfClashingName.mdx"`
);
});

it('warns when a story has the default docs name', async () => {
it('errors when a story has the default docs name', async () => {
const docsErrorSpecifier: NormalizedStoriesSpecifier = normalizeStoriesEntry(
'./errors/A.mdx',
options
Expand All @@ -1016,23 +991,11 @@ describe('StoryIndexGenerator', () => {
);
await generator.initialize();

expect(Object.keys((await generator.getIndex()).entries)).toMatchInlineSnapshot(`
Array [
"a--story-one",
"componentreference--story-one",
"a--metaof",
"notitle--story-one",
"a--second-docs",
"docs2-yabbadabbadooo--story-one",
]
`);

expect(logger.warn).toHaveBeenCalledTimes(1);
expect(jest.mocked(logger.warn).mock.calls[0][0]).toMatchInlineSnapshot(
`"🚨 You have a story for A with the same name as your default docs entry name (Story One), so the docs page is being dropped. Consider changing the story name."`
await expect(generator.getIndex()).rejects.toThrowErrorMatchingInlineSnapshot(
`"Unable to index ./src/A.stories.js,./errors/A.mdx"`
);
});
it('warns when two duplicate stories exists, with duplicated entries details', async () => {
it('errors when two duplicate stories exists, with duplicated entries details', async () => {
const generator = new StoryIndexGenerator([storiesSpecifier, docsSpecifier], {
...options,
});
Expand All @@ -1049,7 +1012,7 @@ describe('StoryIndexGenerator', () => {
}).toThrowErrorMatchingInlineSnapshot(`"Duplicate stories with id: StoryId"`);
});

it('DOES NOT warns when the same MDX file matches two specifiers', async () => {
it('DOES NOT error when the same MDX file matches two specifiers', async () => {
const generator = new StoryIndexGenerator(
[storiesSpecifier, docsSpecifier, docsSpecifier],
options
Expand Down
15 changes: 9 additions & 6 deletions code/lib/core-server/src/utils/StoryIndexGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -418,19 +418,22 @@ export class StoryIndexGenerator {
? `component docs page`
: `automatically generated docs page`;
if (betterEntry.name === this.options.docs.defaultName) {
logger.warn(
`🚨 You have a story for ${betterEntry.title} with the same name as your default docs entry name (${betterEntry.name}), so the docs page is being dropped. Consider changing the story name.`
throw new IndexingError(
`You have a story for ${betterEntry.title} with the same name as your default docs entry name (${betterEntry.name}), so the docs page is being dropped. Consider changing the story name.`,
[firstEntry.importPath, secondEntry.importPath]
);
} else {
logger.warn(
`🚨 You have a story for ${betterEntry.title} with the same name as your ${worseDescriptor} (${worseEntry.name}), so the docs page is being dropped. ${changeDocsName}`
throw new IndexingError(
`You have a story for ${betterEntry.title} with the same name as your ${worseDescriptor} (${worseEntry.name}), so the docs page is being dropped. ${changeDocsName}`,
[firstEntry.importPath, secondEntry.importPath]
);
}
} else if (isMdxEntry(betterEntry)) {
// Both entries are MDX but pointing at the same place
if (isMdxEntry(worseEntry)) {
logger.warn(
`🚨 You have two component docs pages with the same name ${betterEntry.title}:${betterEntry.name}. ${changeDocsName}`
throw new IndexingError(
`You have two component docs pages with the same name ${betterEntry.title}:${betterEntry.name}. ${changeDocsName}`,
[firstEntry.importPath, secondEntry.importPath]
);
}

Expand Down

0 comments on commit 2d280e6

Please sign in to comment.