feat(test-utils)!: add support for running with env variables (#3742)

Co-authored-by: Anthony Fu <hi@antfu.me>
Co-authored-by: pooya parsa <pyapar@gmail.com>
This commit is contained in:
Daniel Roe 2022-03-17 21:17:54 +00:00 committed by GitHub
parent 6be6c3cf25
commit 2590ae39d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 4 deletions

View File

@ -4,8 +4,9 @@ import { getRandomPort, waitForPort } from 'get-port-please'
import { fetch as _fetch, $fetch as _$fetch, FetchOptions } from 'ohmyfetch' import { fetch as _fetch, $fetch as _$fetch, FetchOptions } from 'ohmyfetch'
import { useTestContext } from './context' import { useTestContext } from './context'
export async function listen () { export async function startServer () {
const ctx = useTestContext() const ctx = useTestContext()
await stopServer()
const port = await getRandomPort() const port = await getRandomPort()
ctx.url = 'http://localhost:' + port ctx.url = 'http://localhost:' + port
ctx.serverProcess = execa('node', [ ctx.serverProcess = execa('node', [
@ -13,6 +14,7 @@ export async function listen () {
resolve(ctx.nuxt.options.nitro.output.dir, 'server/index.mjs') resolve(ctx.nuxt.options.nitro.output.dir, 'server/index.mjs')
], { ], {
env: { env: {
...process.env,
PORT: String(port), PORT: String(port),
NODE_ENV: 'test' NODE_ENV: 'test'
} }
@ -20,6 +22,13 @@ export async function listen () {
await waitForPort(port, { retries: 8 }) await waitForPort(port, { retries: 8 })
} }
export async function stopServer () {
const ctx = useTestContext()
if (ctx.serverProcess) {
await ctx.serverProcess.kill()
}
}
export function fetch (path: string, options?: any) { export function fetch (path: string, options?: any) {
return _fetch(url(path), options) return _fetch(url(path), options)
} }

View File

@ -1,6 +1,6 @@
import { createTestContext, setTestContext } from '../context' import { createTestContext, setTestContext } from '../context'
import { loadFixture, buildFixture } from '../nuxt' import { loadFixture, buildFixture } from '../nuxt'
import { listen } from '../server' import { startServer, stopServer } from '../server'
import { createBrowser } from '../browser' import { createBrowser } from '../browser'
import type { TestHooks, TestOptions } from '../types' import type { TestHooks, TestOptions } from '../types'
import setupJest from './jest' import setupJest from './jest'
@ -24,7 +24,9 @@ export function createTest (options: Partial<TestOptions>): TestHooks {
const afterAll = async () => { const afterAll = async () => {
if (ctx.serverProcess) { if (ctx.serverProcess) {
ctx.serverProcess.kill() setTestContext(ctx)
await stopServer()
setTestContext(undefined)
} }
if (ctx.nuxt && ctx.nuxt.options.dev) { if (ctx.nuxt && ctx.nuxt.options.dev) {
await ctx.nuxt.close() await ctx.nuxt.close()
@ -44,7 +46,7 @@ export function createTest (options: Partial<TestOptions>): TestHooks {
} }
if (ctx.options.server) { if (ctx.options.server) {
await listen() await startServer()
} }
if (ctx.options.waitFor) { if (ctx.options.waitFor) {