Skip to content

Commit 796fbcb

Browse files
committed
feat(report): storybook v6 support
MINOR
1 parent 62ce13c commit 796fbcb

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ const lhScoresDir = path.join(process.cwd(), process.env.LH_SCORES_DIR || 'lh-sc
100100
const reportDir = path.join(process.cwd(), process.env.LH_REPORT_DIR || 'lighthouse')
101101
const htmlFilePath = path.join(reportDir, 'index.html')
102102

103+
// use stories.json instead of index.json for storybook v6
104+
// make sure to set buildStoriesJson to true in storybook main.js feature section
103105
const stories = storybookPlaywright.getStories('./storybook-static/index.json', (story) => {
104106
// skip docs, etc
105107
if (story.type !== 'story') {
@@ -233,10 +235,11 @@ export default globalSetup
233235
import { lighthousePlaywrightTeardown, buildAverageCsv } from 'lighthouse-reporting'
234236

235237
const lhScoresDir = path.join(process.cwd(), process.env.LH_SCORES_DIR || 'lh-scores')
238+
const reportDir = path.join(process.cwd(), 'lighthouse')
236239

237240
async function globalTeardown() {
238241
await lighthousePlaywrightTeardown()
239-
await buildAverageCsv(lhScoresDir)
242+
await buildAverageCsv(lhScoresDir, reportDir)
240243
}
241244

242245
export default globalTeardown

src/lighthouseReports.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,18 @@ export const writeScoresToJson = async (lhScoresDir: string, name: string, score
141141
}
142142

143143
/**
144-
* Generate average csv file. Make sure to use writeScoresToJson in your test!
144+
* Generate average csv file. Make sure to use `writeScoresToJson` in your test!
145+
* @param lhScoresDir path to folder with lighthouse json files. See `writeScoresToJson`.
146+
* @param reportDir folder where `_AVERAGE_.json` will be generated
145147
*/
146-
export const buildAverageCsv = async (lhScoresDir: string) => {
148+
export const buildAverageCsv = async (lhScoresDir: string, reportDir: string) => {
147149
const files = await fse.readdir(lhScoresDir)
148150
const jsonFiles = files.filter((f) => f.endsWith('.json'))
149151

152+
if (jsonFiles.length === 0) {
153+
return
154+
}
155+
150156
// sum all the scores
151157
const scores: Record<string, number> = {}
152158
for (const fileName of jsonFiles) {
@@ -166,5 +172,5 @@ export const buildAverageCsv = async (lhScoresDir: string) => {
166172
scores[k] = v / jsonFiles.length
167173
})
168174

169-
await writeCsvResult(lhScoresDir, '_AVERAGE_', scores)
175+
await writeCsvResult(reportDir, '_AVERAGE_', scores)
170176
}

src/storybookPlaywright.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@ import { BrowserContext, Locator, Page, expect } from '@playwright/test'
44
interface StorybookIndexJSON {
55
v: number
66
entries: Record<string, StorybookIndexStory>
7+
stories: Record<string, StorybookStoriesStory>
78
}
89

10+
/**
11+
* Storybook v7
12+
*/
913
export interface StorybookIndexStory {
1014
id: string // composite-typography--variants
1115
title: string // Composite/Typography
@@ -15,7 +19,24 @@ export interface StorybookIndexStory {
1519
type: 'story' | 'docs'
1620
}
1721

18-
type StoriesFilterFn = (story: StorybookIndexStory) => boolean
22+
/**
23+
* Storybook v6
24+
*/
25+
export interface StorybookStoriesStory {
26+
id: string // composite-typography--variants
27+
title: string // Composite/Typography
28+
name: string // Variants
29+
importPath: string // ./components/Typography/Typography.stories.tsx
30+
kind: string
31+
story: string
32+
parameters: {
33+
__id: string
34+
docsOnly: boolean
35+
fileName: string
36+
}
37+
}
38+
39+
type StoriesFilterFn = <V7 = true>(story: V7 extends true ? StorybookIndexStory : StorybookStoriesStory) => boolean
1940

2041
export const storybookPlaywright = {
2142
getStories: (pathToStorybookIndexJson: string, storyFilterFn: StoriesFilterFn) => {
@@ -25,7 +46,9 @@ export const storybookPlaywright = {
2546
}
2647
const storybookIndexJson: StorybookIndexJSON = fse.readJsonSync(pathToStorybookIndexJson)
2748

28-
const stories = Object.values(storybookIndexJson.entries).filter(storyFilterFn)
49+
const storyObject = storybookIndexJson.entries || storybookIndexJson.stories
50+
51+
const stories = Object.values(storyObject).filter(storyFilterFn)
2952

3053
return stories
3154
},

0 commit comments

Comments
 (0)