Last active
June 27, 2024 17:20
-
-
Save NotWoods/07d10905f405e3ff12ba25943bdf7b74 to your computer and use it in GitHub Desktop.
Storybook Test Runner screenshot generation
This file contains 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
// .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