diff --git a/packages/nuxi/src/commands/dev.ts b/packages/nuxi/src/commands/dev.ts index 139d69e629..93c3a6de93 100644 --- a/packages/nuxi/src/commands/dev.ts +++ b/packages/nuxi/src/commands/dev.ts @@ -46,9 +46,14 @@ export default defineNuxtCommand({ const { loadNuxt, loadNuxtConfig, buildNuxt } = await loadKit(rootDir) + console.log('overrides', ...(process.env.NUXT_CONFIG_OVERRIDES ? JSON.parse(process.env.NUXT_CONFIG_OVERRIDES) : {})) const config = await loadNuxtConfig({ cwd: rootDir, - overrides: { dev: true } + overrides: { + dev: true, + // used for testing + ...(process.env.NUXT_CONFIG_OVERRIDES ? JSON.parse(process.env.NUXT_CONFIG_OVERRIDES) : {}) + } }) const listener = await listen(serverHandler, { diff --git a/packages/nuxt/test/__snapshots__/treeshake-client.test.ts.snap b/packages/nuxt/test/__snapshots__/treeshake-client.test.ts.snap index 76ed86bfa4..8ca4bc8af7 100644 Binary files a/packages/nuxt/test/__snapshots__/treeshake-client.test.ts.snap and b/packages/nuxt/test/__snapshots__/treeshake-client.test.ts.snap differ diff --git a/packages/test-utils/src/server.ts b/packages/test-utils/src/server.ts index d37b4e62b1..7857553232 100644 --- a/packages/test-utils/src/server.ts +++ b/packages/test-utils/src/server.ts @@ -9,49 +9,64 @@ import { useTestContext } from './context' // @ts-ignore type cast const kit: typeof _kit = _kit.default || _kit +export async function startDevServer () { + const ctx = useTestContext() + await stopServer() + const port = await getRandomPort() + const nuxiCLI = await kit.resolvePath('nuxi/cli') + ctx.serverProcess = execa(nuxiCLI, ['dev'], { + cwd: ctx.nuxt!.options.rootDir, + env: { + ...process.env, + PORT: String(port), + NITRO_PORT: String(port), + NODE_ENV: 'development', + NUXT_CONFIG_OVERRIDES: JSON.stringify(ctx.options.nuxtConfig) + } + }) + // wait until the server process displays the listening path + ctx.url = await new Promise(resolve => { + // @ts-expect-error untyoed + ctx.serverProcess.stdout?.on('data', (data: Buffer) => { + console.log(data.toString()) + if (data.toString().includes('Local:')) { + console.log('resolving') + resolve(data.toString().split('Local:')[1].trim()) + } + }) + }) + await waitForPort(port, { retries: 32 }) + for (let i = 0; i < 50; i++) { + await new Promise(resolve => setTimeout(resolve, 100)) + try { + const res = await $fetch(ctx.url) + if (!res.includes('__NUXT_LOADING__')) { + return + } + } catch {} + } + ctx.serverProcess.kill() + throw new Error('Timeout waiting for dev server!') +} + + export async function startServer () { const ctx = useTestContext() await stopServer() const port = await getRandomPort() ctx.url = 'http://127.0.0.1:' + port - if (ctx.options.dev) { - const nuxiCLI = await kit.resolvePath('nuxi/cli') - ctx.serverProcess = execa(nuxiCLI, ['dev'], { - cwd: ctx.nuxt!.options.rootDir, - stdio: 'inherit', - env: { - ...process.env, - PORT: String(port), - NITRO_PORT: String(port), - NODE_ENV: 'development' - } - }) - await waitForPort(port, { retries: 32 }) - for (let i = 0; i < 50; i++) { - await new Promise(resolve => setTimeout(resolve, 100)) - try { - const res = await $fetch(ctx.nuxt!.options.app.baseURL) - if (!res.includes('__NUXT_LOADING__')) { - return - } - } catch {} + ctx.serverProcess = execa('node', [ + resolve(ctx.nuxt!.options.nitro.output!.dir!, 'server/index.mjs') + ], { + stdio: 'inherit', + env: { + ...process.env, + PORT: String(port), + NITRO_PORT: String(port), + NODE_ENV: 'test' } - ctx.serverProcess.kill() - throw new Error('Timeout waiting for dev server!') - } else { - ctx.serverProcess = execa('node', [ - resolve(ctx.nuxt!.options.nitro.output!.dir!, 'server/index.mjs') - ], { - stdio: 'inherit', - env: { - ...process.env, - PORT: String(port), - NITRO_PORT: String(port), - NODE_ENV: 'test' - } - }) - await waitForPort(port, { retries: 8 }) - } + }) + await waitForPort(port, { retries: 8 }) } export async function stopServer () { diff --git a/packages/test-utils/src/setup/index.ts b/packages/test-utils/src/setup/index.ts index aa0b368cbb..8d22436a33 100644 --- a/packages/test-utils/src/setup/index.ts +++ b/packages/test-utils/src/setup/index.ts @@ -1,6 +1,6 @@ import { createTestContext, setTestContext } from '../context' import { loadFixture, buildFixture } from '../nuxt' -import { startServer, stopServer } from '../server' +import {startDevServer, startServer, stopServer} from '../server' import { createBrowser } from '../browser' import type { TestHooks, TestOptions } from '../types' import setupJest from './jest' @@ -41,12 +41,16 @@ export function createTest (options: Partial): TestHooks { await loadFixture() } - if (ctx.options.build) { - await buildFixture() - } + if (ctx.options.dev) { + await startDevServer() + } else { + if (ctx.options.build) { + await buildFixture() + } - if (ctx.options.server) { - await startServer() + if (ctx.options.server) { + await startServer() + } } if (ctx.options.waitFor) { diff --git a/test/dev.test.ts b/test/dev.test.ts new file mode 100644 index 0000000000..804f8eba25 --- /dev/null +++ b/test/dev.test.ts @@ -0,0 +1,28 @@ +import { fileURLToPath } from 'node:url' +// eslint-disable-next-line import/order +import { setup, $fetch } from '@nuxt/test-utils' +import { describe, it, expect } from 'vitest' + +await setup({ + rootDir: fileURLToPath(new URL('./fixtures/basic', import.meta.url)), + dev: true, + nuxtConfig: { + app: { + baseURL: '/test' + } + } +}) +describe('dev tests', () => { + it('should just work', async () => { + expect(await $fetch('/')).toMatchInlineSnapshot(` + " + + + + - Fixture +
Extended layout from foo

[...slug].vue

catchall at > Network: http:
Middleware ran: true
+ " + `) + }) +}) + diff --git a/test/fixtures/basic/pages/index.vue b/test/fixtures/basic/pages/index.vue index ae4af0270c..e1ffda1d0d 100644 --- a/test/fixtures/basic/pages/index.vue +++ b/test/fixtures/basic/pages/index.vue @@ -5,6 +5,7 @@

Hello Nuxt 3!

RuntimeConfig | testConfig: {{ config.testConfig }}
+
RuntimeConfig | baseURL: {{ config.baseURL }}
Composable | foo: {{ foo }}
Composable | bar: {{ bar }}
Composable | template: {{ templateAutoImport }}