2022-04-07 11:28:04 +00:00
|
|
|
import { getBrowser, url, useTestContext } from '@nuxt/test-utils'
|
2022-04-05 18:38:23 +00:00
|
|
|
import { expect } from 'vitest'
|
|
|
|
|
|
|
|
export async function renderPage (path = '/') {
|
2022-04-07 11:28:04 +00:00
|
|
|
const ctx = useTestContext()
|
|
|
|
if (!ctx.options.browser) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-05 18:38:23 +00:00
|
|
|
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) {
|
2022-04-07 11:28:04 +00:00
|
|
|
const ctx = useTestContext()
|
|
|
|
if (!ctx.options.browser) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-05 18:38:23 +00:00
|
|
|
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([])
|
|
|
|
}
|