diff --git a/package.json b/package.json index 503f071c9..52dd36b8a 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,7 @@ "test:types": "tsc --noEmit", "test:knip": "knip", "test:dist": "pnpm -r test:dist", - "test:unit": "vitest --coverage" + "test:unit": "vitest --coverage --run && pnpm --filter nuxt-cli-playground test --run" }, "devDependencies": { "@antfu/eslint-config": "^4.16.2", diff --git a/packages/nuxi/src/dev/index.ts b/packages/nuxi/src/dev/index.ts index 994d3e663..116d24277 100644 --- a/packages/nuxi/src/dev/index.ts +++ b/packages/nuxi/src/dev/index.ts @@ -42,7 +42,7 @@ class IPC { const ipc = new IPC() -export async function initialize(devContext: NuxtDevContext, ctx: InitializeOptions = {}, listenOptions?: true | Partial) { +export async function initialize(devContext: NuxtDevContext, ctx: InitializeOptions = {}, _listenOptions?: true | Partial) { const devServerOverrides = resolveDevServerOverrides({ public: devContext.public, }) @@ -52,6 +52,11 @@ export async function initialize(devContext: NuxtDevContext, ctx: InitializeOpti https: devContext.proxy?.https, }, devContext.publicURLs) + // _PORT is used by `@nuxt/test-utils` to launch the dev server on a specific port + const listenOptions = _listenOptions === true || process.env._PORT + ? { port: process.env._PORT ?? 0, hostname: '127.0.0.1', showURL: false } + : _listenOptions + // Init Nuxt dev const devServer = await createNuxtDevServer({ cwd: devContext.cwd, @@ -61,7 +66,6 @@ export async function initialize(devContext: NuxtDevContext, ctx: InitializeOpti clear: !!devContext.args.clear, dotenv: { cwd: devContext.cwd, fileName: devContext.args.dotenv }, envName: devContext.args.envName, - port: process.env._PORT ?? undefined, devContext, }, listenOptions) diff --git a/playground/test/e2e/build.spec.ts b/playground/test/e2e/build.spec.ts new file mode 100644 index 000000000..64e1d2db1 --- /dev/null +++ b/playground/test/e2e/build.spec.ts @@ -0,0 +1,15 @@ +import { fileURLToPath } from 'node:url' +import { $fetch, setup } from '@nuxt/test-utils' +import { describe, expect, it } from 'vitest' + +await setup({ + rootDir: fileURLToPath(new URL('../..', import.meta.url)), +}) + +describe('built server', () => { + it('should start and return HTML', async () => { + const html = await $fetch('/') + + expect(html).toContain('Welcome to the Nuxt CLI playground') + }) +}) diff --git a/playground/test/e2e/dev.spec.ts b/playground/test/e2e/dev.spec.ts index bf40e708d..557391dec 100644 --- a/playground/test/e2e/dev.spec.ts +++ b/playground/test/e2e/dev.spec.ts @@ -1,14 +1,14 @@ -// import { fileURLToPath } from 'node:url' -// import { $fetch, setup } from '@nuxt/test-utils' +import { fileURLToPath } from 'node:url' +import { $fetch, setup } from '@nuxt/test-utils' import { describe, expect, it } from 'vitest' -// await setup({ -// rootDir: fileURLToPath(new URL('../..', import.meta.url)), -// dev: true, -// }) +await setup({ + rootDir: fileURLToPath(new URL('../..', import.meta.url)), + dev: true, +}) describe('dev server', () => { - it.skip('should start and return HTML', async () => { + it('should start and return HTML', async () => { const html = await $fetch('/') expect(html).toContain('Welcome to the Nuxt CLI playground') diff --git a/vitest.config.ts b/vitest.config.ts index ef90a2eea..32e1af244 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -1,9 +1,13 @@ import codspeed from '@codspeed/vitest-plugin' -import { defineConfig } from 'vitest/config' +import { defaultExclude, defineConfig } from 'vitest/config' export default defineConfig({ plugins: [codspeed()], test: { coverage: {}, + exclude: [ + ...defaultExclude, + 'playground/**', + ], }, })