mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-11 08:33:53 +00:00
11626eea4f
Co-authored-by: Daniel Roe <daniel@roe.dev>
51 lines
1.1 KiB
TypeScript
51 lines
1.1 KiB
TypeScript
import { getBrowser, url, useTestContext } from '@nuxt/test-utils'
|
|
import { expect } from 'vitest'
|
|
|
|
export async function renderPage (path = '/') {
|
|
const ctx = useTestContext()
|
|
if (!ctx.options.browser) {
|
|
return
|
|
}
|
|
|
|
const browser = await getBrowser()
|
|
const page = await browser.newPage({})
|
|
const pageErrors = []
|
|
const consoleLogs = []
|
|
|
|
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 === 'warn')
|
|
|
|
expect(pageErrors).toEqual([])
|
|
expect(consoleLogErrors).toEqual([])
|
|
expect(consoleLogWarnings).toEqual([])
|
|
}
|