mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-11 08:33:53 +00:00
e38e1de740
Co-authored-by: Pooya Parsa <pooya@pi0.io>
51 lines
1.2 KiB
TypeScript
51 lines
1.2 KiB
TypeScript
import { expect } from 'vitest'
|
|
import { getBrowser, url, useTestContext } from '@nuxt/test-utils'
|
|
|
|
export async function renderPage (path = '/') {
|
|
const ctx = useTestContext()
|
|
if (!ctx.options.browser) {
|
|
throw new Error('`renderPage` require `options.browser` to be set')
|
|
}
|
|
|
|
const browser = await getBrowser()
|
|
const page = await browser.newPage({})
|
|
const pageErrors: Error[] = []
|
|
const consoleLogs: { type:string, text:string }[] = []
|
|
|
|
page.on('console', (message) => {
|
|
consoleLogs.push({
|
|
type: message.type(),
|
|
text: message.text()
|
|
})
|
|
})
|
|
page.on('pageerror', (err) => {
|
|
pageErrors.push(err)
|
|
})
|
|
|
|
if (path) {
|
|
await page.goto(url(path), { waitUntil: 'networkidle' })
|
|
}
|
|
|
|
return {
|
|
page,
|
|
pageErrors,
|
|
consoleLogs
|
|
}
|
|
}
|
|
|
|
export async function expectNoClientErrors (path: string) {
|
|
const ctx = useTestContext()
|
|
if (!ctx.options.browser) {
|
|
return
|
|
}
|
|
|
|
const { pageErrors, consoleLogs } = (await renderPage(path))!
|
|
|
|
const consoleLogErrors = consoleLogs.filter(i => i.type === 'error')
|
|
const consoleLogWarnings = consoleLogs.filter(i => i.type === 'warning')
|
|
|
|
expect(pageErrors).toEqual([])
|
|
expect(consoleLogErrors).toEqual([])
|
|
expect(consoleLogWarnings).toEqual([])
|
|
}
|