0
0
mirror of https://github.com/PostHog/posthog.git synced 2024-11-29 03:04:16 +01:00
posthog/.storybook/test-runner.ts

188 lines
7.8 KiB
TypeScript
Raw Normal View History

import { toMatchImageSnapshot } from 'jest-image-snapshot'
import { OptionsParameter } from '@storybook/addons'
import { getStoryContext, TestRunnerConfig, TestContext } from '@storybook/test-runner'
import type { Locator, Page, LocatorScreenshotOptions } from 'playwright-core'
import type { Mocks } from '~/mocks/utils'
import { StoryContext } from '@storybook/react'
type SupportedBrowserName = 'chromium' | 'firefox' | 'webkit'
// Extend Storybook interface `Parameters` with Chromatic parameters
declare module '@storybook/react' {
interface Parameters {
options?: OptionsParameter
layout?: 'padded' | 'fullscreen' | 'centered'
testOptions?: {
/**
* Whether the test should be a no-op (doesn't jest.skip as @storybook/test-runner doesn't allow that).
* @default false
*/
skip?: boolean
/**
test(frontend): Improve reliability of visual regression tests (#14422) * test(frontend): Ensure 100% reliability of visual regression tests * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (2) * Use OptiPNG in CI * Update UI snapshots for `chromium` (1) * Don't rely on pre-commit hook for image compression * Allow waiting for selector instead of constant delay * Update UI snapshots for `webkit` (2) * Update UI snapshots for `chromium` (1) * Update UI snapshots for `chromium` (2) * Update UI snapshots for `webkit` (2) * Add extra insight rendering conditions * Set strict timeouts for visual regression test jobs * Optimize OptiPNG step * Fix histogram selector * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (2) * Ensure non-blank canvas * Fix blank canvas detection and auto-detect `waitForLoadersToDisappear` * Use `Spinner` for loading state in `MonacoEditor` * Make timeout limits less strict * Update UI snapshots for `chromium` (2) * Improve blank canvas detection further * Update UI snapshots for `webkit` (2) * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (2) * Update UI snapshots for `webkit` (2) * Update UI snapshots for `webkit` (2) * Update UI snapshots for `chromium` (2) * Tune selectors * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (1) * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (1) * Update UI snapshots for `chromium` (1) * Update UI snapshots for `chromium` (2) * Try a different way of ensuring graphs are rendered * Update UI snapshots for `webkit` (2) * Increase `waitForLoadersToDisappear` initial UI wait * Update UI snapshots for `webkit` (2) * Make it easier to see what file wasn't pushed * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (1) * Update UI snapshots for `chromium` (1) * Fix `timeout-minutes` * Update UI snapshots for `chromium` (2) * Update UI snapshots for `webkit` (2) * Update UI snapshots for `chromium` (1) * Update UI snapshots for `chromium` (1) * Add extra wait for canvases * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (2) --------- Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
2023-03-02 22:11:03 +01:00
* Whether we should wait for all loading indicators to disappear before taking a snapshot.
*
* This is on by default for stories that have a layout of 'fullscreen', and off otherwise.
* Override that behavior by setting this to `true` or `false` manually.
*
* You can also provide a selector string instead of a boolean - in that case we'll wait
* for a matching element to be be visible once all loaders are gone.
*/
test(frontend): Improve reliability of visual regression tests (#14422) * test(frontend): Ensure 100% reliability of visual regression tests * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (2) * Use OptiPNG in CI * Update UI snapshots for `chromium` (1) * Don't rely on pre-commit hook for image compression * Allow waiting for selector instead of constant delay * Update UI snapshots for `webkit` (2) * Update UI snapshots for `chromium` (1) * Update UI snapshots for `chromium` (2) * Update UI snapshots for `webkit` (2) * Add extra insight rendering conditions * Set strict timeouts for visual regression test jobs * Optimize OptiPNG step * Fix histogram selector * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (2) * Ensure non-blank canvas * Fix blank canvas detection and auto-detect `waitForLoadersToDisappear` * Use `Spinner` for loading state in `MonacoEditor` * Make timeout limits less strict * Update UI snapshots for `chromium` (2) * Improve blank canvas detection further * Update UI snapshots for `webkit` (2) * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (2) * Update UI snapshots for `webkit` (2) * Update UI snapshots for `webkit` (2) * Update UI snapshots for `chromium` (2) * Tune selectors * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (1) * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (1) * Update UI snapshots for `chromium` (1) * Update UI snapshots for `chromium` (2) * Try a different way of ensuring graphs are rendered * Update UI snapshots for `webkit` (2) * Increase `waitForLoadersToDisappear` initial UI wait * Update UI snapshots for `webkit` (2) * Make it easier to see what file wasn't pushed * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (1) * Update UI snapshots for `chromium` (1) * Fix `timeout-minutes` * Update UI snapshots for `chromium` (2) * Update UI snapshots for `webkit` (2) * Update UI snapshots for `chromium` (1) * Update UI snapshots for `chromium` (1) * Add extra wait for canvases * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (2) --------- Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
2023-03-02 22:11:03 +01:00
waitForLoadersToDisappear?: boolean | string
/**
* Whether navigation (sidebar + topbar) should be excluded from the snapshot.
* Warning: Fails if enabled for stories in which navigation is not present.
*/
excludeNavigationFromSnapshot?: boolean
/**
* The test will always run for all the browers, but snapshots are only taken in Chromium by default.
* Override this to take snapshots in other browsers too.
* @default ['chromium']
*/
snapshotBrowsers?: SupportedBrowserName[]
}
mockDate?: string | number | Date
msw?: {
mocks?: Mocks
}
[name: string]: any
}
}
const RETRY_TIMES = 5
const LOADER_SELECTORS = ['.ant-skeleton', '.Spinner', '.LemonSkeleton', '.LemonTableLoader']
const customSnapshotsDir = `${process.cwd()}/frontend/__snapshots__`
module.exports = {
setup() {
expect.extend({ toMatchImageSnapshot })
jest.retryTimes(RETRY_TIMES, { logErrorsBeforeRetry: true })
},
async postRender(page, context) {
test(frontend): Improve reliability of visual regression tests (#14422) * test(frontend): Ensure 100% reliability of visual regression tests * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (2) * Use OptiPNG in CI * Update UI snapshots for `chromium` (1) * Don't rely on pre-commit hook for image compression * Allow waiting for selector instead of constant delay * Update UI snapshots for `webkit` (2) * Update UI snapshots for `chromium` (1) * Update UI snapshots for `chromium` (2) * Update UI snapshots for `webkit` (2) * Add extra insight rendering conditions * Set strict timeouts for visual regression test jobs * Optimize OptiPNG step * Fix histogram selector * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (2) * Ensure non-blank canvas * Fix blank canvas detection and auto-detect `waitForLoadersToDisappear` * Use `Spinner` for loading state in `MonacoEditor` * Make timeout limits less strict * Update UI snapshots for `chromium` (2) * Improve blank canvas detection further * Update UI snapshots for `webkit` (2) * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (2) * Update UI snapshots for `webkit` (2) * Update UI snapshots for `webkit` (2) * Update UI snapshots for `chromium` (2) * Tune selectors * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (1) * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (1) * Update UI snapshots for `chromium` (1) * Update UI snapshots for `chromium` (2) * Try a different way of ensuring graphs are rendered * Update UI snapshots for `webkit` (2) * Increase `waitForLoadersToDisappear` initial UI wait * Update UI snapshots for `webkit` (2) * Make it easier to see what file wasn't pushed * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (1) * Update UI snapshots for `chromium` (1) * Fix `timeout-minutes` * Update UI snapshots for `chromium` (2) * Update UI snapshots for `webkit` (2) * Update UI snapshots for `chromium` (1) * Update UI snapshots for `chromium` (1) * Add extra wait for canvases * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (2) --------- Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
2023-03-02 22:11:03 +01:00
const browserContext = page.context()
const storyContext = await getStoryContext(page, context)
const { skip = false, snapshotBrowsers = ['chromium'] } = storyContext.parameters?.testOptions ?? {}
test(frontend): Improve reliability of visual regression tests (#14422) * test(frontend): Ensure 100% reliability of visual regression tests * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (2) * Use OptiPNG in CI * Update UI snapshots for `chromium` (1) * Don't rely on pre-commit hook for image compression * Allow waiting for selector instead of constant delay * Update UI snapshots for `webkit` (2) * Update UI snapshots for `chromium` (1) * Update UI snapshots for `chromium` (2) * Update UI snapshots for `webkit` (2) * Add extra insight rendering conditions * Set strict timeouts for visual regression test jobs * Optimize OptiPNG step * Fix histogram selector * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (2) * Ensure non-blank canvas * Fix blank canvas detection and auto-detect `waitForLoadersToDisappear` * Use `Spinner` for loading state in `MonacoEditor` * Make timeout limits less strict * Update UI snapshots for `chromium` (2) * Improve blank canvas detection further * Update UI snapshots for `webkit` (2) * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (2) * Update UI snapshots for `webkit` (2) * Update UI snapshots for `webkit` (2) * Update UI snapshots for `chromium` (2) * Tune selectors * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (1) * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (1) * Update UI snapshots for `chromium` (1) * Update UI snapshots for `chromium` (2) * Try a different way of ensuring graphs are rendered * Update UI snapshots for `webkit` (2) * Increase `waitForLoadersToDisappear` initial UI wait * Update UI snapshots for `webkit` (2) * Make it easier to see what file wasn't pushed * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (1) * Update UI snapshots for `chromium` (1) * Fix `timeout-minutes` * Update UI snapshots for `chromium` (2) * Update UI snapshots for `webkit` (2) * Update UI snapshots for `chromium` (1) * Update UI snapshots for `chromium` (1) * Add extra wait for canvases * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (2) --------- Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
2023-03-02 22:11:03 +01:00
browserContext.setDefaultTimeout(1000) // Reduce the default timeout from 30 s to 1 s to pre-empt Jest timeouts
if (!skip) {
test(frontend): Improve reliability of visual regression tests (#14422) * test(frontend): Ensure 100% reliability of visual regression tests * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (2) * Use OptiPNG in CI * Update UI snapshots for `chromium` (1) * Don't rely on pre-commit hook for image compression * Allow waiting for selector instead of constant delay * Update UI snapshots for `webkit` (2) * Update UI snapshots for `chromium` (1) * Update UI snapshots for `chromium` (2) * Update UI snapshots for `webkit` (2) * Add extra insight rendering conditions * Set strict timeouts for visual regression test jobs * Optimize OptiPNG step * Fix histogram selector * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (2) * Ensure non-blank canvas * Fix blank canvas detection and auto-detect `waitForLoadersToDisappear` * Use `Spinner` for loading state in `MonacoEditor` * Make timeout limits less strict * Update UI snapshots for `chromium` (2) * Improve blank canvas detection further * Update UI snapshots for `webkit` (2) * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (2) * Update UI snapshots for `webkit` (2) * Update UI snapshots for `webkit` (2) * Update UI snapshots for `chromium` (2) * Tune selectors * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (1) * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (1) * Update UI snapshots for `chromium` (1) * Update UI snapshots for `chromium` (2) * Try a different way of ensuring graphs are rendered * Update UI snapshots for `webkit` (2) * Increase `waitForLoadersToDisappear` initial UI wait * Update UI snapshots for `webkit` (2) * Make it easier to see what file wasn't pushed * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (1) * Update UI snapshots for `chromium` (1) * Fix `timeout-minutes` * Update UI snapshots for `chromium` (2) * Update UI snapshots for `webkit` (2) * Update UI snapshots for `chromium` (1) * Update UI snapshots for `chromium` (1) * Add extra wait for canvases * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (2) --------- Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
2023-03-02 22:11:03 +01:00
const currentBrowser = browserContext.browser()!.browserType().name() as 'chromium' | 'firefox' | 'webkit'
if (snapshotBrowsers.includes(currentBrowser)) {
await expectStoryToMatchSnapshot(page, context, storyContext, currentBrowser)
}
}
},
} as TestRunnerConfig
async function expectStoryToMatchSnapshot(
page: Page,
context: TestContext,
storyContext: StoryContext,
browser: SupportedBrowserName
): Promise<void> {
test(frontend): Improve reliability of visual regression tests (#14422) * test(frontend): Ensure 100% reliability of visual regression tests * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (2) * Use OptiPNG in CI * Update UI snapshots for `chromium` (1) * Don't rely on pre-commit hook for image compression * Allow waiting for selector instead of constant delay * Update UI snapshots for `webkit` (2) * Update UI snapshots for `chromium` (1) * Update UI snapshots for `chromium` (2) * Update UI snapshots for `webkit` (2) * Add extra insight rendering conditions * Set strict timeouts for visual regression test jobs * Optimize OptiPNG step * Fix histogram selector * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (2) * Ensure non-blank canvas * Fix blank canvas detection and auto-detect `waitForLoadersToDisappear` * Use `Spinner` for loading state in `MonacoEditor` * Make timeout limits less strict * Update UI snapshots for `chromium` (2) * Improve blank canvas detection further * Update UI snapshots for `webkit` (2) * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (2) * Update UI snapshots for `webkit` (2) * Update UI snapshots for `webkit` (2) * Update UI snapshots for `chromium` (2) * Tune selectors * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (1) * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (1) * Update UI snapshots for `chromium` (1) * Update UI snapshots for `chromium` (2) * Try a different way of ensuring graphs are rendered * Update UI snapshots for `webkit` (2) * Increase `waitForLoadersToDisappear` initial UI wait * Update UI snapshots for `webkit` (2) * Make it easier to see what file wasn't pushed * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (1) * Update UI snapshots for `chromium` (1) * Fix `timeout-minutes` * Update UI snapshots for `chromium` (2) * Update UI snapshots for `webkit` (2) * Update UI snapshots for `chromium` (1) * Update UI snapshots for `chromium` (1) * Add extra wait for canvases * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (2) --------- Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
2023-03-02 22:11:03 +01:00
const {
waitForLoadersToDisappear = storyContext.parameters?.layout === 'fullscreen',
excludeNavigationFromSnapshot = false,
} = storyContext.parameters?.testOptions ?? {}
let check: (page: Page, context: TestContext, browser: SupportedBrowserName) => Promise<void>
if (storyContext.parameters?.layout === 'fullscreen') {
if (excludeNavigationFromSnapshot) {
check = expectStoryToMatchSceneSnapshot
} else {
check = expectStoryToMatchFullPageSnapshot
}
} else {
check = expectStoryToMatchComponentSnapshot
}
test(frontend): Improve reliability of visual regression tests (#14422) * test(frontend): Ensure 100% reliability of visual regression tests * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (2) * Use OptiPNG in CI * Update UI snapshots for `chromium` (1) * Don't rely on pre-commit hook for image compression * Allow waiting for selector instead of constant delay * Update UI snapshots for `webkit` (2) * Update UI snapshots for `chromium` (1) * Update UI snapshots for `chromium` (2) * Update UI snapshots for `webkit` (2) * Add extra insight rendering conditions * Set strict timeouts for visual regression test jobs * Optimize OptiPNG step * Fix histogram selector * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (2) * Ensure non-blank canvas * Fix blank canvas detection and auto-detect `waitForLoadersToDisappear` * Use `Spinner` for loading state in `MonacoEditor` * Make timeout limits less strict * Update UI snapshots for `chromium` (2) * Improve blank canvas detection further * Update UI snapshots for `webkit` (2) * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (2) * Update UI snapshots for `webkit` (2) * Update UI snapshots for `webkit` (2) * Update UI snapshots for `chromium` (2) * Tune selectors * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (1) * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (1) * Update UI snapshots for `chromium` (1) * Update UI snapshots for `chromium` (2) * Try a different way of ensuring graphs are rendered * Update UI snapshots for `webkit` (2) * Increase `waitForLoadersToDisappear` initial UI wait * Update UI snapshots for `webkit` (2) * Make it easier to see what file wasn't pushed * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (1) * Update UI snapshots for `chromium` (1) * Fix `timeout-minutes` * Update UI snapshots for `chromium` (2) * Update UI snapshots for `webkit` (2) * Update UI snapshots for `chromium` (1) * Update UI snapshots for `chromium` (1) * Add extra wait for canvases * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (2) --------- Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
2023-03-02 22:11:03 +01:00
// Wait for story to load
test(frontend): Improve reliability of visual regression tests (#14422) * test(frontend): Ensure 100% reliability of visual regression tests * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (2) * Use OptiPNG in CI * Update UI snapshots for `chromium` (1) * Don't rely on pre-commit hook for image compression * Allow waiting for selector instead of constant delay * Update UI snapshots for `webkit` (2) * Update UI snapshots for `chromium` (1) * Update UI snapshots for `chromium` (2) * Update UI snapshots for `webkit` (2) * Add extra insight rendering conditions * Set strict timeouts for visual regression test jobs * Optimize OptiPNG step * Fix histogram selector * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (2) * Ensure non-blank canvas * Fix blank canvas detection and auto-detect `waitForLoadersToDisappear` * Use `Spinner` for loading state in `MonacoEditor` * Make timeout limits less strict * Update UI snapshots for `chromium` (2) * Improve blank canvas detection further * Update UI snapshots for `webkit` (2) * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (2) * Update UI snapshots for `webkit` (2) * Update UI snapshots for `webkit` (2) * Update UI snapshots for `chromium` (2) * Tune selectors * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (1) * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (1) * Update UI snapshots for `chromium` (1) * Update UI snapshots for `chromium` (2) * Try a different way of ensuring graphs are rendered * Update UI snapshots for `webkit` (2) * Increase `waitForLoadersToDisappear` initial UI wait * Update UI snapshots for `webkit` (2) * Make it easier to see what file wasn't pushed * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (1) * Update UI snapshots for `chromium` (1) * Fix `timeout-minutes` * Update UI snapshots for `chromium` (2) * Update UI snapshots for `webkit` (2) * Update UI snapshots for `chromium` (1) * Update UI snapshots for `chromium` (1) * Add extra wait for canvases * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (2) --------- Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
2023-03-02 22:11:03 +01:00
await page.waitForSelector('.sb-show-preparing-story', { state: 'detached' })
await page.evaluate(() => {
// Stop all animations for consistent snapshots
document.body.classList.add('storybook-test-runner')
})
if (waitForLoadersToDisappear) {
test(frontend): Improve reliability of visual regression tests (#14422) * test(frontend): Ensure 100% reliability of visual regression tests * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (2) * Use OptiPNG in CI * Update UI snapshots for `chromium` (1) * Don't rely on pre-commit hook for image compression * Allow waiting for selector instead of constant delay * Update UI snapshots for `webkit` (2) * Update UI snapshots for `chromium` (1) * Update UI snapshots for `chromium` (2) * Update UI snapshots for `webkit` (2) * Add extra insight rendering conditions * Set strict timeouts for visual regression test jobs * Optimize OptiPNG step * Fix histogram selector * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (2) * Ensure non-blank canvas * Fix blank canvas detection and auto-detect `waitForLoadersToDisappear` * Use `Spinner` for loading state in `MonacoEditor` * Make timeout limits less strict * Update UI snapshots for `chromium` (2) * Improve blank canvas detection further * Update UI snapshots for `webkit` (2) * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (2) * Update UI snapshots for `webkit` (2) * Update UI snapshots for `webkit` (2) * Update UI snapshots for `chromium` (2) * Tune selectors * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (1) * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (1) * Update UI snapshots for `chromium` (1) * Update UI snapshots for `chromium` (2) * Try a different way of ensuring graphs are rendered * Update UI snapshots for `webkit` (2) * Increase `waitForLoadersToDisappear` initial UI wait * Update UI snapshots for `webkit` (2) * Make it easier to see what file wasn't pushed * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (1) * Update UI snapshots for `chromium` (1) * Fix `timeout-minutes` * Update UI snapshots for `chromium` (2) * Update UI snapshots for `webkit` (2) * Update UI snapshots for `chromium` (1) * Update UI snapshots for `chromium` (1) * Add extra wait for canvases * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (2) --------- Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
2023-03-02 22:11:03 +01:00
await page.waitForTimeout(300) // Wait for initial UI to load
await Promise.all(LOADER_SELECTORS.map((selector) => page.waitForSelector(selector, { state: 'detached' })))
if (typeof waitForLoadersToDisappear === 'string') {
await page.waitForSelector(waitForLoadersToDisappear)
}
}
await page.waitForTimeout(100) // Just a bit of extra delay for things to settle
await check(page, context, browser)
}
async function expectStoryToMatchFullPageSnapshot(
page: Page,
context: TestContext,
browser: SupportedBrowserName
): Promise<void> {
await expectLocatorToMatchStorySnapshot(page, context, browser)
}
async function expectStoryToMatchSceneSnapshot(
page: Page,
context: TestContext,
browser: SupportedBrowserName
): Promise<void> {
await expectLocatorToMatchStorySnapshot(page.locator('.main-app-content'), context, browser)
}
async function expectStoryToMatchComponentSnapshot(
page: Page,
context: TestContext,
browser: SupportedBrowserName
): Promise<void> {
await page.evaluate(() => {
const rootEl = document.getElementById('root')
if (!rootEl) {
throw new Error('Could not find root element')
}
// Make the root element (which is the screenshot reference) hug the component
rootEl.style.display = 'inline-block'
// If needed, expand the root element so that all popovers are visible in the screenshot
document.querySelectorAll('.Popover').forEach((popover) => {
const currentRootBoundingClientRect = rootEl.getBoundingClientRect()
const popoverBoundingClientRect = popover.getBoundingClientRect()
if (popoverBoundingClientRect.right > currentRootBoundingClientRect.right) {
rootEl.style.width = `${popoverBoundingClientRect.right}px`
}
if (popoverBoundingClientRect.bottom > currentRootBoundingClientRect.bottom) {
rootEl.style.height = `${popoverBoundingClientRect.bottom}px`
}
if (popoverBoundingClientRect.top < currentRootBoundingClientRect.top) {
rootEl.style.height = `${-popoverBoundingClientRect.top + currentRootBoundingClientRect.bottom}px`
}
if (popoverBoundingClientRect.left < currentRootBoundingClientRect.left) {
rootEl.style.width = `${-popoverBoundingClientRect.left + currentRootBoundingClientRect.right}px`
}
})
// Make the body transparent to take the screenshot without background
document.body.style.background = 'transparent'
})
await expectLocatorToMatchStorySnapshot(page.locator('#root'), context, browser, { omitBackground: true })
}
async function expectLocatorToMatchStorySnapshot(
locator: Locator | Page,
context: TestContext,
browser: SupportedBrowserName,
options?: LocatorScreenshotOptions
): Promise<void> {
const image = await locator.screenshot({ timeout: 3000, ...options })
let customSnapshotIdentifier = context.id
if (browser !== 'chromium') {
customSnapshotIdentifier += `--${browser}`
}
expect(image).toMatchImageSnapshot({
customSnapshotsDir,
customSnapshotIdentifier,
// Compare structural similarity instead of raw pixels - reducing false positives
// See https://github.com/americanexpress/jest-image-snapshot#recommendations-when-using-ssim-comparison
comparisonMethod: 'ssim',
failureThreshold: 0.0003,
failureThresholdType: 'percent',
})
}