sadpandajoe commented on code in PR #44964: URL: https://github.com/apache/superset/pull/44964#discussion_r4236304812
########## superset-frontend/.storybook/setup-stories.ts: ########## @@ -0,0 +1,170 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +// Source: https://gist.github.com/AriPerkkio/99b9eedc7d8f71ff6e6770f9425a4be4 +import { + appendFileSync, + existsSync, + mkdirSync, + readFileSync, + writeFileSync, +} from 'node:fs'; +import { dirname, join, resolve } from 'node:path'; +import { EOL } from 'node:os'; +import { fileURLToPath } from 'node:url'; +import { type test } from '@playwright/test'; + +declare global { + var __storyResult: { status: string; errors: string[] }; +} + +interface Entry { + id: string; + title: string; + name: string; + type: 'story' | 'docs'; + tags?: string[]; +} + +interface Report { + type: string; + status: string; + result?: { violations?: { id: string; help: string }[] }; +} + +export interface StoryPage { + addInitScript(script: () => void): Promise<unknown>; + goto(url: string): Promise<unknown>; + waitForFunction<T>( + pageFunction: () => T, + ): Promise<{ jsonValue(): Promise<T> }>; +} + +const storybookURL = 'http://localhost:6006'; + +const __dirname = dirname(fileURLToPath(import.meta.url)); +const indexFile = resolve( + join(__dirname, '../node_modules/.cache/storybook-playwright/index.json'), +); + +export default async function globalSetup() { + const response = await fetch(`${storybookURL}/index.json`); + + if (!response.ok) { + throw new Error( + `Failed to fetch ${storybookURL}/index.json: ${response.status}`, + ); + } + + mkdirSync(dirname(indexFile), { recursive: true }); + writeFileSync(indexFile, await response.text()); + appendFileSync(indexFile, EOL); +} + +function captureStoryResult() { + const errors: string[] = []; + const serialize = (error: Error) => + error.stack ?? `${error.name}: ${error.message}`; + + let channel: any; + Object.defineProperty(globalThis, '__STORYBOOK_ADDONS_CHANNEL__', { + configurable: true, + get: () => channel, + set: value => { + channel = value; + channel.on( + 'storyErrored', + ({ title, description }: Record<string, string>) => + errors.push(`${title}\n${description}`), + ); + channel.on('storyThrewException', (error: Error) => + errors.push(serialize(error)), + ); + channel.on('playFunctionThrewException', (error: Error) => + errors.push(serialize(error)), + ); + channel.on('unhandledErrorsWhilePlaying', (unhandled: Error[]) => + errors.push(...unhandled.map(serialize)), + ); + channel.on('storyMissing', (id: string) => { Review Comment: The two non-success exits in this collector have no coverage: the `storyMissing` handler, which fires without a `storyFinished` when a story module fails to load, and the failed-reporter branch in `storyFinished` that turns a failed a11y report into `- id: help` lines. Deleting either would leave `setup-stories.test.ts` green, since its fixture always emits `storyFinished` with `reporters: []`. A story that fails to load would then hang until the Playwright timeout instead of failing with the story ID, and an a11y failure would lose its violation detail. Could the existing `createPage` fixture emit a lone `storyMissing` and a `storyFinished` carrying a failed `a11y` reporter, asserting the rejection text includes `is missing` plus the ID and the `a11y report failed` plus violation lines? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
