diff --git a/src/core/context.ts b/src/core/context.ts index cb44ff9d9..2c29bad3c 100644 --- a/src/core/context.ts +++ b/src/core/context.ts @@ -1,5 +1,6 @@ import { resolve } from 'node:path' import { defu } from 'defu' +import { withTrailingSlash } from 'ufo' import type { TestContext, TestOptions } from './types' let currentContext: TestContext | undefined @@ -20,6 +21,12 @@ export function createTestContext(options: Partial): TestContext { }, } satisfies Partial) + // Disable build and server if endpoint is provided + if (_options.host) { + _options.build = false + _options.server = false + } + if (process.env.VITEST === 'true') { _options.runner ||= 'vitest' } @@ -29,6 +36,7 @@ export function createTestContext(options: Partial): TestContext { return setTestContext({ options: _options as TestOptions, + url: withTrailingSlash(_options.host), }) } diff --git a/src/core/nuxt.ts b/src/core/nuxt.ts index a7c0ea85b..7f8988687 100644 --- a/src/core/nuxt.ts +++ b/src/core/nuxt.ts @@ -53,19 +53,22 @@ export async function loadFixture() { }) } - ctx.nuxt = await kit.loadNuxt({ - cwd: ctx.options.rootDir, - dev: ctx.options.dev, - overrides: ctx.options.nuxtConfig, - configFile: ctx.options.configFile, - }) + // TODO: share Nuxt instance with running Nuxt if possible + if (ctx.options.build) { + ctx.nuxt = await kit.loadNuxt({ + cwd: ctx.options.rootDir, + dev: ctx.options.dev, + overrides: ctx.options.nuxtConfig, + configFile: ctx.options.configFile, + }) - const buildDir = ctx.nuxt.options.buildDir - // avoid creating / deleting build dirs that already exist - avoids misconfiguration deletes - if (!existsSync(buildDir)) { - await fsp.mkdir(buildDir, { recursive: true }) - ctx.teardown = ctx.teardown || [] - ctx.teardown.push(() => fsp.rm(buildDir, { recursive: true, force: true })) + const buildDir = ctx.nuxt.options.buildDir + // avoid creating / deleting build dirs that already exist - avoids misconfiguration deletes + if (!existsSync(buildDir)) { + await fsp.mkdir(buildDir, { recursive: true }) + ctx.teardown = ctx.teardown || [] + ctx.teardown.push(() => fsp.rm(buildDir, { recursive: true, force: true })) + } } } diff --git a/src/core/server.ts b/src/core/server.ts index 9db68daf2..95ed22ce8 100644 --- a/src/core/server.ts +++ b/src/core/server.ts @@ -4,7 +4,7 @@ import type { FetchOptions } from 'ofetch' import { $fetch as _$fetch, fetch as _fetch } from 'ofetch' import * as _kit from '@nuxt/kit' import { resolve } from 'pathe' - +import { joinURL } from 'ufo' import { useTestContext } from './context' // @ts-expect-error type cast kit default export @@ -19,7 +19,7 @@ export async function startServer(options: StartServerOptions = {}) { await stopServer() const host = '127.0.0.1' const port = ctx.options.port || await getRandomPort(host) - ctx.url = `http://${host}:${port}` + ctx.url = `http://${host}:${port}/` if (ctx.options.dev) { const nuxiCLI = await kit.resolvePath('nuxi/cli') ctx.serverProcess = execa(nuxiCLI, ['_dev'], { @@ -91,5 +91,5 @@ export function url(path: string) { if (path.startsWith(ctx.url)) { return path } - return ctx.url + path + return joinURL(ctx.url, path) } diff --git a/src/core/types.ts b/src/core/types.ts index 7dfaa2eb7..35ec10147 100644 --- a/src/core/types.ts +++ b/src/core/types.ts @@ -24,6 +24,7 @@ export interface TestOptions { launch?: LaunchOptions } server: boolean + host?: string port?: number env?: StartServerOptions['env'] }