Skip to content

Commit

Permalink
Merge pull request #30086 from storybookjs/jeppe+ndelangen/fix-failin…
Browse files Browse the repository at this point in the history
…g-tests-filter

Manager: Keep failing stories in the sidebar, disregarding filters
  • Loading branch information
JReinhold authored Dec 17, 2024
2 parents 9e9543b + dfe06f6 commit dc462b8
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
59 changes: 59 additions & 0 deletions code/core/src/manager-api/lib/stories.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import { describe, expect, it } from 'vitest';

import type { API_PreparedStoryIndex, StoryIndexV2, StoryIndexV3 } from '@storybook/core/types';

import type { State } from '../root';
import { mockEntries } from '../tests/mockStoriesEntries';
import {
transformStoryIndexToStoriesHash,
transformStoryIndexV2toV3,
transformStoryIndexV3toV4,
transformStoryIndexV4toV5,
Expand Down Expand Up @@ -216,3 +218,60 @@ describe('transformStoryIndexV4toV5', () => {
`);
});
});

describe('transformStoryIndexToStoriesHash', () => {
it('does not apply filters to failing stories', () => {
// Arrange - set up an index with two stories, one of which has a failing status
const indexV5: API_PreparedStoryIndex = {
v: 5,
entries: {
'1': {
id: '1',
type: 'story',
title: 'Story 1',
name: 'Story 1',
importPath: './path/to/story-1.ts',
parameters: {},
tags: [],
},
'2': {
id: '2',
type: 'story',
title: 'Story 2',
name: 'Story 2',
importPath: './path/to/story-2.ts',
parameters: {},
tags: [],
},
},
};

const filters: State['filters'] = {
someFilter: () => false,
};

const status: State['status'] = {
'1': { someStatus: { status: 'error', title: 'broken', description: 'very bad' } },
'2': { someStatus: { status: 'success', title: 'perfect', description: 'nice' } },
};

const options = {
provider: {
getConfig: () => ({ sidebar: {} }),
} as any,
docsOptions: { docsMode: false },
filters,
status,
};

// Act - transform the index to hashes
const result = transformStoryIndexToStoriesHash(indexV5, options);

// Assert - the failing story is still present in the result, even though the filters remove all stories
expect(Object.keys(result)).toHaveLength(2);
expect(result['story-1']).toBeTruthy();
expect(result['1']).toBeTruthy();
expect(result['story-2']).toBeUndefined();
expect(result['2']).toBeUndefined();
});
});
8 changes: 7 additions & 1 deletion code/core/src/manager-api/lib/stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,17 @@ export const transformStoryIndexToStoriesHash = (
const entryValues = Object.values(index.entries).filter((entry: any) => {
let result = true;

// All stories with a failing status should always show up, regardless of the applied filters
const storyStatus = status[entry.id];
if (Object.values(storyStatus ?? {}).some(({ status: s }) => s === 'error')) {
return result;
}

Object.values(filters).forEach((filter: any) => {
if (result === false) {
return;
}
result = filter({ ...entry, status: status[entry.id] });
result = filter({ ...entry, status: storyStatus });
});

return result;
Expand Down

0 comments on commit dc462b8

Please sign in to comment.