From dd366aa494b1e4c8640108bfe4f05beda55be93a Mon Sep 17 00:00:00 2001 From: Harlan Wilton Date: Mon, 6 Mar 2023 13:02:11 +1100 Subject: [PATCH] chore: progress --- packages/nuxi/src/commands/dev.ts | 14 +++++++------- packages/test-utils/src/nuxt.ts | 24 ++++++++++-------------- packages/test-utils/src/server.ts | 3 ++- packages/test-utils/src/setup/index.ts | 13 ++++++++----- test/dev.test.ts | 26 +++++++++++++++----------- 5 files changed, 42 insertions(+), 38 deletions(-) diff --git a/packages/nuxi/src/commands/dev.ts b/packages/nuxi/src/commands/dev.ts index a656a873c9..4ce09b36bc 100644 --- a/packages/nuxi/src/commands/dev.ts +++ b/packages/nuxi/src/commands/dev.ts @@ -46,14 +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 overrides = { + dev: true, + // used for testing + ...(process.env.NUXT_CONFIG_OVERRIDES ? JSON.parse(process.env.NUXT_CONFIG_OVERRIDES) : {}) + } const config = await loadNuxtConfig({ cwd: rootDir, - overrides: { - dev: true, - // used for testing - ...(process.env.NUXT_CONFIG_OVERRIDES ? JSON.parse(process.env.NUXT_CONFIG_OVERRIDES) : {}) - } + overrides }) const listener = await listen(serverHandler, { @@ -93,7 +93,7 @@ export default defineNuxtCommand({ await distWatcher.close() } - currentNuxt = await loadNuxt({ rootDir, dev: true, ready: false }) + currentNuxt = await loadNuxt({ rootDir, overrides, dev: true, ready: false }) currentNuxt.hooks.hookOnce('restart', async (options) => { if (options?.hard && process.send) { diff --git a/packages/test-utils/src/nuxt.ts b/packages/test-utils/src/nuxt.ts index f6d267afd1..71cac5a147 100644 --- a/packages/test-utils/src/nuxt.ts +++ b/packages/test-utils/src/nuxt.ts @@ -16,7 +16,7 @@ const isNuxtApp = (dir: string) => { ) } -const resolveRootDir = () => { +export const resolveRootDir = () => { const { options } = useTestContext() const dirs = [ @@ -37,20 +37,16 @@ const resolveRootDir = () => { export async function loadFixture () { const ctx = useTestContext() - ctx.options.rootDir = resolveRootDir() - - if (!ctx.options.dev) { - const randomId = Math.random().toString(36).slice(2, 8) - const buildDir = resolve(ctx.options.rootDir, '.nuxt', randomId) - Object.assign(ctx.options.nuxtConfig, { - buildDir, - nitro: { - output: { - dir: resolve(buildDir, 'output') - } + const randomId = Math.random().toString(36).slice(2, 8) + const buildDir = resolve(ctx.options.rootDir, '.nuxt', randomId) + Object.assign(ctx.options.nuxtConfig, { + buildDir, + nitro: { + output: { + dir: resolve(buildDir, 'output') } - }) - } + } + }) ctx.nuxt = await kit.loadNuxt({ cwd: ctx.options.rootDir, diff --git a/packages/test-utils/src/server.ts b/packages/test-utils/src/server.ts index 7857553232..05067c467d 100644 --- a/packages/test-utils/src/server.ts +++ b/packages/test-utils/src/server.ts @@ -15,7 +15,7 @@ export async function startDevServer () { const port = await getRandomPort() const nuxiCLI = await kit.resolvePath('nuxi/cli') ctx.serverProcess = execa(nuxiCLI, ['dev'], { - cwd: ctx.nuxt!.options.rootDir, + cwd: ctx.options.rootDir, env: { ...process.env, PORT: String(port), @@ -24,6 +24,7 @@ export async function startDevServer () { NUXT_CONFIG_OVERRIDES: JSON.stringify(ctx.options.nuxtConfig) } }) + console.log('boot dev server', ctx.options.rootDir, JSON.stringify(ctx.options.nuxtConfig)) // wait until the server process displays the listening path ctx.url = await new Promise(resolve => { // @ts-expect-error untyoed diff --git a/packages/test-utils/src/setup/index.ts b/packages/test-utils/src/setup/index.ts index 8d22436a33..5c63178883 100644 --- a/packages/test-utils/src/setup/index.ts +++ b/packages/test-utils/src/setup/index.ts @@ -1,5 +1,5 @@ -import { createTestContext, setTestContext } from '../context' -import { loadFixture, buildFixture } from '../nuxt' +import {createTestContext, setTestContext, useTestContext} from '../context' +import {loadFixture, buildFixture, resolveRootDir} from '../nuxt' import {startDevServer, startServer, stopServer} from '../server' import { createBrowser } from '../browser' import type { TestHooks, TestOptions } from '../types' @@ -37,13 +37,16 @@ export function createTest (options: Partial): TestHooks { } const setup = async () => { - if (ctx.options.fixture) { - await loadFixture() - } + const ctx = useTestContext() + ctx.options.rootDir = resolveRootDir() if (ctx.options.dev) { await startDevServer() } else { + if (ctx.options.fixture) { + await loadFixture() + } + if (ctx.options.build) { await buildFixture() } diff --git a/test/dev.test.ts b/test/dev.test.ts index 804f8eba25..57f6eb1b8d 100644 --- a/test/dev.test.ts +++ b/test/dev.test.ts @@ -4,25 +4,29 @@ import { setup, $fetch } from '@nuxt/test-utils' import { describe, it, expect } from 'vitest' await setup({ - rootDir: fileURLToPath(new URL('./fixtures/basic', import.meta.url)), + rootDir: fileURLToPath(new URL('./fixtures/minimal', import.meta.url)), dev: true, nuxtConfig: { app: { baseURL: '/test' + }, + runtimeConfig: { + public: { + foo: 'bar', + } } } }) 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
- " - `) + it('config is overriden', async () => { + const html = await $fetch('/') + const expr = /window.__NUXT__=({.*})<\/script>/.exec(html)![1] + expect(expr).toMatchInlineSnapshot('"{data:{},state:{},_errors:{},serverRendered:true,config:{public:{foo:\\"bar\\"},app:{baseURL:\\"\\\\u002Ftest\\",buildAssetsDir:\\"\\\\u002F_nuxt\\\\u002F\\",cdnURL:\\"\\"}}}"') + + // expr is a javascript object string, but not valid JSON, we need to turn it a real object + const obj = JSON.parse(expr.replace(/(\w+):/g, '"$1":')) + expect(obj.config.public.foo).toBe('bar') + expect(obj.config.app.baseURL).toBe('/test') }) })