Skip to content

Commit c4df41d

Browse files
authored
feat(e2e): allow running e2e tests against existing server (#803)
1 parent f310a05 commit c4df41d

File tree

4 files changed

+27
-15
lines changed

4 files changed

+27
-15
lines changed

src/core/context.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { resolve } from 'node:path'
22
import { defu } from 'defu'
3+
import { withTrailingSlash } from 'ufo'
34
import type { TestContext, TestOptions } from './types'
45

56
let currentContext: TestContext | undefined
@@ -20,6 +21,12 @@ export function createTestContext(options: Partial<TestOptions>): TestContext {
2021
},
2122
} satisfies Partial<TestOptions>)
2223

24+
// Disable build and server if endpoint is provided
25+
if (_options.host) {
26+
_options.build = false
27+
_options.server = false
28+
}
29+
2330
if (process.env.VITEST === 'true') {
2431
_options.runner ||= 'vitest'
2532
}
@@ -29,6 +36,7 @@ export function createTestContext(options: Partial<TestOptions>): TestContext {
2936

3037
return setTestContext({
3138
options: _options as TestOptions,
39+
url: withTrailingSlash(_options.host),
3240
})
3341
}
3442

src/core/nuxt.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,22 @@ export async function loadFixture() {
5353
})
5454
}
5555

56-
ctx.nuxt = await kit.loadNuxt({
57-
cwd: ctx.options.rootDir,
58-
dev: ctx.options.dev,
59-
overrides: ctx.options.nuxtConfig,
60-
configFile: ctx.options.configFile,
61-
})
56+
// TODO: share Nuxt instance with running Nuxt if possible
57+
if (ctx.options.build) {
58+
ctx.nuxt = await kit.loadNuxt({
59+
cwd: ctx.options.rootDir,
60+
dev: ctx.options.dev,
61+
overrides: ctx.options.nuxtConfig,
62+
configFile: ctx.options.configFile,
63+
})
6264

63-
const buildDir = ctx.nuxt.options.buildDir
64-
// avoid creating / deleting build dirs that already exist - avoids misconfiguration deletes
65-
if (!existsSync(buildDir)) {
66-
await fsp.mkdir(buildDir, { recursive: true })
67-
ctx.teardown = ctx.teardown || []
68-
ctx.teardown.push(() => fsp.rm(buildDir, { recursive: true, force: true }))
65+
const buildDir = ctx.nuxt.options.buildDir
66+
// avoid creating / deleting build dirs that already exist - avoids misconfiguration deletes
67+
if (!existsSync(buildDir)) {
68+
await fsp.mkdir(buildDir, { recursive: true })
69+
ctx.teardown = ctx.teardown || []
70+
ctx.teardown.push(() => fsp.rm(buildDir, { recursive: true, force: true }))
71+
}
6972
}
7073
}
7174

src/core/server.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { FetchOptions } from 'ofetch'
44
import { $fetch as _$fetch, fetch as _fetch } from 'ofetch'
55
import * as _kit from '@nuxt/kit'
66
import { resolve } from 'pathe'
7-
7+
import { joinURL } from 'ufo'
88
import { useTestContext } from './context'
99

1010
// @ts-expect-error type cast kit default export
@@ -19,7 +19,7 @@ export async function startServer(options: StartServerOptions = {}) {
1919
await stopServer()
2020
const host = '127.0.0.1'
2121
const port = ctx.options.port || await getRandomPort(host)
22-
ctx.url = `http://${host}:${port}`
22+
ctx.url = `http://${host}:${port}/`
2323
if (ctx.options.dev) {
2424
const nuxiCLI = await kit.resolvePath('nuxi/cli')
2525
ctx.serverProcess = execa(nuxiCLI, ['_dev'], {
@@ -91,5 +91,5 @@ export function url(path: string) {
9191
if (path.startsWith(ctx.url)) {
9292
return path
9393
}
94-
return ctx.url + path
94+
return joinURL(ctx.url, path)
9595
}

src/core/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export interface TestOptions {
2424
launch?: LaunchOptions
2525
}
2626
server: boolean
27+
host?: string
2728
port?: number
2829
env?: StartServerOptions['env']
2930
}

0 commit comments

Comments
 (0)