Skip to content

Instantly share code, notes, and snippets.

@NotWoods
Last active June 27, 2024 17:20
Show Gist options
  • Save NotWoods/07d10905f405e3ff12ba25943bdf7b74 to your computer and use it in GitHub Desktop.
Save NotWoods/07d10905f405e3ff12ba25943bdf7b74 to your computer and use it in GitHub Desktop.
Storybook Test Runner screenshot generation
// .storybook/test-runner.js
import { join } from 'node:path';
import { getStoryContext, waitForPageReady } from '@storybook/test-runner';
const screenShotDestPath = 'dist/screenshots';
/** @type {import('@storybook/test-runner').TestRunnerConfig} */
const config = {
tags: {
exclude: ['no-tests']
},
async postVisit(page, context) {
const { tags, title, name } = await getStoryContext(page, context);
if (!tags.includes('no-screenshot')) {
await waitForPageReady(page);
// Set story category and name as prefix for screenshot name.
const ssNamePrefix = `${title}.${name}`.replace('/', '-').replace('\\', '-'); //INFO: '/' or "\\" in screenshot name creates a folder in screenshot location. Replacing with '-'
// The above line is taken from Storywright for consistency with the screenshot name.
await page.screenshot({
path: join(process.cwd(), screenShotDestPath, `${ssNamePrefix}.png`)
});
}
}
};
export default config;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment