-
Notifications
You must be signed in to change notification settings - Fork 116
feat(e2e): add screenshots for failing tests with vitest runner #1597
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
base: main
Are you sure you want to change the base?
Changes from all commits
3f91167
a8e8e37
c795fb7
310349c
827384d
9f50e2c
6173706
f1a51a5
92b9341
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| import type { Browser, BrowserContextOptions, Page, Response } from 'playwright-core' | ||
| import { useTestContext } from './context.ts' | ||
| import { url } from './server.ts' | ||
| import { useTestContext } from './context' | ||
| import { url } from './server' | ||
| import { join, dirname } from 'node:path' | ||
|
|
||
| export async function createBrowser() { | ||
| const ctx = useTestContext() | ||
|
|
@@ -43,9 +44,24 @@ export interface NuxtPage extends Omit<Page, 'goto'> { | |
| } | ||
|
|
||
| export async function createPage(path?: string, options?: BrowserContextOptions): Promise<NuxtPage> { | ||
| const ctx = useTestContext() | ||
| const browser = await getBrowser() | ||
| const page = await browser.newPage(options) as unknown as NuxtPage | ||
|
|
||
| if (ctx.options.runner === 'vitest') { | ||
| const vitest = await import('vitest') | ||
| vitest.onTestFailed(async (test) => { | ||
| try { | ||
| await page.screenshot({ | ||
| path: join(dirname(test.task.file.filepath), '__screenshots__', `failed-${test.task.fullTestName}.png`), | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. π§© Analysis chainπ Script executed: # First, let's look at the file structure and the specific line in question
cat -n src/e2e/browser.ts | head -70 | tail -20Repository: nuxt/test-utils Length of output: 799 π Script executed: # Run the verification commands from the review
rg -nP --type=ts -C3 'fullTestName.*\.png|__screenshots__' srcRepository: nuxt/test-utils Length of output: 446 π Script executed: # Check for any existing sanitization functions
rg -nP --type=ts -C2 'sanitize|slug|replace\(.+[<>:"/\\|?*]' src/e2eRepository: nuxt/test-utils Length of output: 41 π Script executed: # Check if there are other places where test names are used as filenames
rg -nP --type=ts 'test\.task\.' src/e2e | grep -E '\.(png|jpg|jpeg|txt|json)'Repository: nuxt/test-utils Length of output: 195 π Script executed: # Verify that test names can indeed contain problematic characters
# by checking if there are any test name patterns in config or other files
rg -nP --type=ts 'fullTestName' srcRepository: nuxt/test-utils Length of output: 195 π Script executed: # Let's verify what characters are actually allowed in filenames across platforms
# and understand the severity of this issue by checking if there are any existing test names
# that might trigger this problem
rg -nP --type=ts 'describe\(|it\(' src/e2e | head -20Repository: nuxt/test-utils Length of output: 261 π Script executed: # Let's look at actual test files to see what test names might look like
find src -name "*.test.ts" -o -name "*.spec.ts" | head -5Repository: nuxt/test-utils Length of output: 41 π Script executed: # Also check if there are any other usages of screenshot or similar file operations
rg -nP --type=ts 'screenshot|__screenshots__' srcRepository: nuxt/test-utils Length of output: 249 Sanitize Line 56 builds the filename from π‘ Proposed fix+function sanitizeScreenshotName(name: string): string {
+ return name
+ .replace(/[<>:"/\\|?*\u0000-\u001F]/g, '_')
+ .replace(/\s+/g, ' ')
+ .trim()
+ .slice(0, 180)
+}
+
export async function createPage(path?: string, options?: BrowserContextOptions): Promise<NuxtPage> {
const ctx = useTestContext()
const browser = await getBrowser()
const page = await browser.newPage(options) as unknown as NuxtPage
@@
vitest.onTestFailed(async (test) => {
try {
+ const fileName = `failed-${sanitizeScreenshotName(test.task.fullTestName)}.png`
await page.screenshot({
- path: join(dirname(test.task.file.filepath), '__screenshots__', `failed-${test.task.fullTestName}.png`),
+ path: join(dirname(test.task.file.filepath), '__screenshots__', fileName),
})
}π€ Prompt for AI Agents |
||
| }) | ||
| } | ||
| catch { | ||
| // noop | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| const _goto = page.goto.bind(page) | ||
| page.goto = async (url, options): Promise<Response | null> => { | ||
| const waitUntil = options?.waitUntil | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.