Skip to content

Commit

Permalink
feat(e2e): allow running e2e tests against existing server (#803)
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasdiez committed Jul 26, 2024
1 parent f310a05 commit c4df41d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
8 changes: 8 additions & 0 deletions src/core/context.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -20,6 +21,12 @@ export function createTestContext(options: Partial<TestOptions>): TestContext {
},
} satisfies Partial<TestOptions>)

// 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'
}
Expand All @@ -29,6 +36,7 @@ export function createTestContext(options: Partial<TestOptions>): TestContext {

return setTestContext({
options: _options as TestOptions,
url: withTrailingSlash(_options.host),
})
}

Expand Down
27 changes: 15 additions & 12 deletions src/core/nuxt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }))
}
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/core/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'], {
Expand Down Expand Up @@ -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)
}
1 change: 1 addition & 0 deletions src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface TestOptions {
launch?: LaunchOptions
}
server: boolean
host?: string
port?: number
env?: StartServerOptions['env']
}
Expand Down

0 comments on commit c4df41d

Please sign in to comment.