Repository navigation
test(bulma-ui): Storybook smoke gate — every story, light + dark, no console errors #315
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import type { TestRunnerConfig } from '@storybook/test-runner'; | ||
| import type { Page } from 'playwright'; | ||
|
|
||
| // Smoke-pass hooks for `test-storybook` (#283): every story must render | ||
| // without throwing and without console errors. Run with | ||
| // STORYBOOK_THEME=dark for a second pass with `data-theme="dark"` stamped | ||
| // on the root — dark-mode-only CSS bugs are invisible to jsdom, so this is | ||
| // the only automated surface that can catch them. | ||
| const dark = process.env.STORYBOOK_THEME === 'dark'; | ||
|
|
||
| // One error sink per Playwright page; pages are reused across stories in a | ||
| // worker, so the sink is emptied in preVisit instead of re-registering | ||
| // listeners. | ||
| const consoleErrors = new WeakMap<Page, string[]>(); | ||
|
|
||
| const config: TestRunnerConfig = { | ||
| async preVisit(page) { | ||
| if (dark) { | ||
| await page.evaluate(() => | ||
| document.documentElement.setAttribute('data-theme', 'dark') | ||
| ); | ||
| } | ||
| const sink = consoleErrors.get(page); | ||
| if (sink) { | ||
| sink.length = 0; | ||
| } else { | ||
| const fresh: string[] = []; | ||
| consoleErrors.set(page, fresh); | ||
| page.on('console', msg => { | ||
| if (msg.type() !== 'error') return; | ||
| const url = msg.location()?.url ?? ''; | ||
| // Stories that demo broken-image fallbacks use RFC 2606 `.invalid` | ||
| // hosts (e.g. Avatar's https://example.invalid/missing.jpg) — those | ||
| // resource errors are the story working as intended. | ||
| if (/^https?:\/\/[^/]*\.invalid(\/|$)/.test(url)) return; | ||
| fresh.push(url ? `${msg.text()} (${url})` : msg.text()); | ||
| }); | ||
| page.on('pageerror', err => fresh.push(String(err))); | ||
| } | ||
| }, | ||
|
|
||
| async postVisit(page, context) { | ||
| const errors = consoleErrors.get(page) ?? []; | ||
| if (errors.length > 0) { | ||
| throw new Error( | ||
| `Story ${context.id} logged console errors${dark ? ' (dark theme)' : ''}:\n` + | ||
| errors.map(e => ` - ${e}`).join('\n') | ||
| ); | ||
| } | ||
| }, | ||
| }; | ||
|
|
||
| export default config; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| #!/usr/bin/env node | ||
| // Serve the built storybook (storybook-static/) on a local port and run the | ||
| // test-runner smoke pass against it twice: default (light) and dark theme | ||
| // (STORYBOOK_THEME=dark, see .storybook/test-runner.ts). Uses node's http | ||
| // module rather than adding http-server/wait-on/concurrently dependencies. | ||
| import { createServer } from 'node:http'; | ||
| import { readFile, stat } from 'node:fs/promises'; | ||
| import { spawn } from 'node:child_process'; | ||
| import { extname, join, normalize, resolve } from 'node:path'; | ||
| import { fileURLToPath } from 'node:url'; | ||
|
|
||
| const root = resolve(fileURLToPath(new URL('.', import.meta.url)), '..'); | ||
| const staticDir = join(root, 'storybook-static'); | ||
|
|
||
| try { | ||
| await stat(join(staticDir, 'index.html')); | ||
| } catch { | ||
| console.error( | ||
| 'storybook-static/index.html not found — run `pnpm build-storybook` first.' | ||
| ); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| const MIME = { | ||
| '.html': 'text/html', | ||
| '.js': 'text/javascript', | ||
| '.mjs': 'text/javascript', | ||
| '.css': 'text/css', | ||
| '.json': 'application/json', | ||
| '.svg': 'image/svg+xml', | ||
| '.png': 'image/png', | ||
| '.jpg': 'image/jpeg', | ||
| '.gif': 'image/gif', | ||
| '.woff': 'font/woff', | ||
| '.woff2': 'font/woff2', | ||
| '.ttf': 'font/ttf', | ||
| '.eot': 'application/vnd.ms-fontobject', | ||
| '.map': 'application/json', | ||
| '.txt': 'text/plain', | ||
| }; | ||
|
|
||
| const server = createServer(async (req, res) => { | ||
| const urlPath = decodeURIComponent(new URL(req.url, 'http://x').pathname); | ||
| // normalize() collapses any ../ so requests can't escape staticDir | ||
| let filePath = join(staticDir, normalize(urlPath).replace(/^([/\\])+/, '')); | ||
| try { | ||
| if ((await stat(filePath)).isDirectory()) { | ||
| filePath = join(filePath, 'index.html'); | ||
| } | ||
| const body = await readFile(filePath); | ||
| res.writeHead(200, { | ||
| 'content-type': MIME[extname(filePath)] ?? 'application/octet-stream', | ||
| }); | ||
| res.end(body); | ||
| } catch { | ||
| res.writeHead(404).end('not found'); | ||
| } | ||
| }); | ||
|
|
||
| await new Promise(ok => server.listen(0, '127.0.0.1', ok)); | ||
| const url = `http://127.0.0.1:${server.address().port}`; | ||
| console.log(`Serving ${staticDir} at ${url}`); | ||
|
|
||
| function runPass(theme) { | ||
| return new Promise(done => { | ||
| const label = theme === 'dark' ? 'dark' : 'light'; | ||
| console.log(`\n=== Storybook smoke pass (${label}) ===`); | ||
| // Build the env explicitly both ways so an ambient STORYBOOK_THEME can't | ||
| // silently turn the light pass into a second dark pass. | ||
| const env = { ...process.env }; | ||
| if (theme === 'dark') env.STORYBOOK_THEME = 'dark'; | ||
| else delete env.STORYBOOK_THEME; | ||
| const child = spawn( | ||
| 'pnpm', | ||
| ['exec', 'test-storybook', '--url', url, '--maxWorkers', '50%'], | ||
| { cwd: root, stdio: 'inherit', env } | ||
| ); | ||
| child.on('close', code => done(code ?? 1)); | ||
| // A failed spawn (e.g. ENOENT) emits 'error' without 'close' — resolve | ||
| // instead of hanging the CI job forever. | ||
| child.on('error', err => { | ||
| console.error(`Failed to start test-storybook (${label}):`, err); | ||
| done(1); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| const lightCode = await runPass('light'); | ||
| const darkCode = await runPass('dark'); | ||
| server.close(); | ||
| process.exit(lightCode || darkCode); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.