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

Core: added .entries property to error thrown when duplicate stories are present #20038

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
25 changes: 23 additions & 2 deletions code/lib/core-server/src/utils/StoryIndexGenerator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
import path from 'path';
import fs from 'fs-extra';
import { normalizeStoriesEntry } from '@storybook/core-common';
import type { NormalizedStoriesSpecifier, StoryIndexer } from '@storybook/types';
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 { StoryIndexGenerator } from './StoryIndexGenerator';
import { StoryIndexGenerator, DuplicateEntriesError } from './StoryIndexGenerator';

jest.mock('@storybook/csf-tools');
jest.mock('@storybook/csf', () => {
Expand Down Expand Up @@ -902,6 +902,27 @@ describe('StoryIndexGenerator', () => {
`"🚨 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."`
);
});
it('warns when two duplicate stories exists, with duplicated entries details', async () => {
const generator = new StoryIndexGenerator([storiesSpecifier, docsSpecifier], {
...options,
});
await generator.initialize();
const mockEntry: StoryIndexEntry = {
id: 'StoryId',
name: 'StoryName',
title: 'ComponentTitle',
importPath: 'Path',
type: 'story',
};
expect(() => {
generator.chooseDuplicate(mockEntry, mockEntry);
}).toThrow(
new DuplicateEntriesError(`Duplicate stories with id: ${mockEntry.id}`, [
mockEntry,
mockEntry,
])
);
});
});
});

Expand Down
16 changes: 15 additions & 1 deletion code/lib/core-server/src/utils/StoryIndexGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ type StoriesCacheEntry = {
type CacheEntry = false | StoriesCacheEntry | DocsCacheEntry;
type SpecifierStoriesCache = Record<Path, CacheEntry>;

export class DuplicateEntriesError extends Error {
entries: IndexEntry[];

constructor(message: string, entries: IndexEntry[]) {
super();
this.message = message;
this.entries = entries;
}
}

const makeAbsolute = (otherImport: Path, normalizedPath: Path, workingDir: Path) =>
otherImport.startsWith('.')
? slash(
Expand Down Expand Up @@ -352,7 +362,11 @@ export class StoryIndexGenerator {
const changeDocsName = 'Use `<Meta of={} name="Other Name">` to distinguish them.';

// This shouldn't be possible, but double check and use for typing
if (worseEntry.type === 'story') throw new Error(`Duplicate stories with id: ${firstEntry.id}`);
if (worseEntry.type === 'story')
throw new DuplicateEntriesError(`Duplicate stories with id: ${firstEntry.id}`, [
firstEntry,
secondEntry,
]);

if (betterEntry.type === 'story') {
const worseDescriptor = worseEntry.standalone
Expand Down