From 8e96a20396745ba73eeca8c124a81537054fba4d Mon Sep 17 00:00:00 2001 From: Harlan Wilton Date: Mon, 6 Mar 2023 11:26:09 +1100 Subject: [PATCH] tests: allow dev server overrides --- packages/nuxi/src/commands/dev.ts | 7 +- .../treeshake-client.test.ts.snap | Bin 7081 -> 7121 bytes packages/test-utils/src/server.ts | 89 ++++++++++-------- packages/test-utils/src/setup/index.ts | 16 ++-- test/dev.test.ts | 28 ++++++ test/fixtures/basic/pages/index.vue | 1 + 6 files changed, 97 insertions(+), 44 deletions(-) create mode 100644 test/dev.test.ts 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 76ed86bfa42083749de3bd61bc1e9c61cde343c0..8ca4bc8af731d57177b6607d112cfb5505f7ff6f 100644 GIT binary patch delta 55 zcmZ2!e$jk_sD_S0MoCFQv6a4lS!PLUafx0^YMFj|X=X~QesNx6L2*WYiC#uYZqDR& Ke8C$t9Hjv|wG^)a delta 10 Rcmca;zS4Yx=tkQq(f}EA1S
(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 }}