test: add benchmark for vite client build (#31118)

This commit is contained in:
Daniel Roe 2025-02-26 17:06:45 +00:00
parent 25cd5bfff2
commit bbae4d74a4
No known key found for this signature in database
GPG Key ID: CBC814C393D93268
6 changed files with 109 additions and 3 deletions

View File

@ -235,6 +235,9 @@ jobs:
name: dist
path: packages
- name: Prepare test fixtures
run: pnpm test:prepare
- name: Run benchmarks
uses: CodSpeedHQ/action@63ae6025a0ffee97d7736a37c9192dbd6ed4e75f # v3.4.0
with:

View File

@ -0,0 +1,40 @@
import { fileURLToPath } from 'node:url'
import { rm } from 'node:fs/promises'
import { afterAll, beforeAll, bench, describe } from 'vitest'
import { join, normalize } from 'pathe'
import { withoutTrailingSlash } from 'ufo'
import { build, loadNuxt } from 'nuxt'
import type { Nuxt } from '@nuxt/schema'
const basicTestFixtureDir = withoutTrailingSlash(normalize(fileURLToPath(new URL('../../../test/fixtures/basic', import.meta.url))))
describe('build', () => {
let nuxt: Nuxt
beforeAll(async () => {
await rm(join(basicTestFixtureDir, 'node_modules/build-plugins/.nuxt'), { recursive: true, force: true })
nuxt = await loadNuxt({
cwd: basicTestFixtureDir,
ready: true,
overrides: {
buildDir: join(basicTestFixtureDir, 'node_modules/build-plugins/.nuxt'),
ssr: false,
sourcemap: false,
hooks: {
'build:done': () => {
throw new Error('bypass nitro build')
},
},
},
})
})
afterAll(() => nuxt?.close())
bench('initial production build in the basic test fixture', async () => {
await build(nuxt).catch((e) => {
if (!e?.toString().includes('bypass nitro build')) {
throw e
}
})
})
})

View File

@ -9,7 +9,7 @@ const basicTestFixtureDir = withoutTrailingSlash(normalize(fileURLToPath(new URL
describe('build', () => {
beforeAll(async () => {
await rm(join(basicTestFixtureDir, '.nuxt'), { recursive: true, force: true })
await rm(join(basicTestFixtureDir, 'node_modules/build/.nuxt'), { recursive: true, force: true })
})
bench('initial dev server build in the basic test fixture', async () => {
@ -18,6 +18,7 @@ describe('build', () => {
ready: true,
overrides: {
dev: true,
buildDir: join(basicTestFixtureDir, 'node_modules/build/.nuxt'),
sourcemap: false,
builder: {
bundle: () => Promise.resolve(),

View File

@ -11,8 +11,8 @@ const basicTestFixtureDir = withoutTrailingSlash(normalize(fileURLToPath(new URL
describe('loadNuxt', () => {
beforeAll(async () => {
await Promise.all([
rm(join(emptyDir, '.nuxt'), { recursive: true, force: true }),
rm(join(basicTestFixtureDir, '.nuxt'), { recursive: true, force: true }),
rm(join(emptyDir, 'node_modules/load-nuxt/.nuxt'), { recursive: true, force: true }),
rm(join(basicTestFixtureDir, 'node_modules/load-nuxt/.nuxt'), { recursive: true, force: true }),
])
})
@ -20,6 +20,9 @@ describe('loadNuxt', () => {
const nuxt = await loadNuxt({
cwd: emptyDir,
ready: true,
overrides: {
buildDir: join(emptyDir, 'node_modules/load-nuxt/.nuxt'),
},
})
await nuxt.close()
})
@ -28,6 +31,9 @@ describe('loadNuxt', () => {
const nuxt = await loadNuxt({
cwd: basicTestFixtureDir,
ready: true,
overrides: {
buildDir: join(basicTestFixtureDir, 'node_modules/load-nuxt/.nuxt'),
},
})
await nuxt.close()
})

View File

@ -0,0 +1,15 @@
// @ts-expect-error untyped
import '#nitro-internal-pollyfills'
import type { NitroApp } from 'nitro/types'
// @ts-expect-error untyped
import { useNitroApp } from 'nitropack/runtime'
const nitroApp = useNitroApp()
async function renderIndex () {
const text = await (nitroApp as NitroApp).localFetch('/', {}).then(r => r.text())
// eslint-disable-next-line
console.log(text)
}
renderIndex()

View File

@ -0,0 +1,41 @@
import { fileURLToPath } from 'node:url'
import { rm } from 'node:fs/promises'
import { beforeAll, bench, describe, expect } from 'vitest'
import { join, normalize } from 'pathe'
import { withoutTrailingSlash } from 'ufo'
import { build, loadNuxt } from 'nuxt'
import { x } from 'tinyexec'
const basicTestFixtureDir = withoutTrailingSlash(normalize(fileURLToPath(new URL('../../../test/fixtures/basic', import.meta.url))))
const outputDir = fileURLToPath(new URL('../../../node_modules/.test/render', import.meta.url))
describe.todo('render', () => {
beforeAll(async () => {
await rm(join(basicTestFixtureDir, 'node_modules/render/.nuxt'), { recursive: true, force: true })
const nuxt = await loadNuxt({
cwd: basicTestFixtureDir,
ready: true,
overrides: {
buildDir: join(basicTestFixtureDir, 'node_modules/render/.nuxt'),
nitro: {
output: {
dir: outputDir,
},
},
sourcemap: false,
},
})
nuxt.hook('nitro:build:before', (nitro) => {
nitro.options.entry = fileURLToPath(new URL('./nitro/render-index', import.meta.url))
})
await build(nuxt)
await nuxt.close()
}, 200_000)
bench('index route in the basic test fixture', async () => {
const res = await x('node', [join(outputDir, 'server/index.mjs')], {
nodeOptions: { stdio: 'pipe' },
})
expect(res.stdout).toContain('Hello Nuxt 3!')
})
})