Skip to content

Commit b693232

Browse files
authored
feat(e2e): support env option for startServer (#640)
1 parent fb306e0 commit b693232

File tree

5 files changed

+30
-6
lines changed

5 files changed

+30
-6
lines changed

examples/module/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"private": true,
3+
"name": "module",
34
"description": "My new Nuxt module",
45
"repository": "your-org/my-module",
56
"license": "MIT",

examples/module/test/basic.test.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { fileURLToPath } from 'node:url'
22
import { describe, expect, it } from 'vitest'
3-
import { $fetch, setup } from '@nuxt/test-utils/e2e'
3+
import { $fetch, setup, startServer } from '@nuxt/test-utils/e2e'
44

55
describe('ssr', async () => {
66
await setup({
@@ -10,6 +10,17 @@ describe('ssr', async () => {
1010
it('renders the index page', async () => {
1111
// Get response to a server-rendered page with `$fetch`.
1212
const html = await $fetch('/')
13-
expect(html).toContain('<div>basic</div>')
13+
expect(html).toContain('<div>basic <span>original value</span></div>')
14+
})
15+
16+
it('changes runtime config and restarts', async () => {
17+
await startServer({ env: { NUXT_PUBLIC_MY_VALUE: 'overwritten by test!' } })
18+
19+
const html = await $fetch('/')
20+
expect(html).toContain('<div>basic <span>overwritten by test!</span></div>')
21+
22+
await startServer()
23+
const htmlRestored = await $fetch('/')
24+
expect(htmlRestored).toContain('<div>basic <span>original value</span></div>')
1425
})
1526
})
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<template>
2-
<div>basic</div>
2+
<div>basic <span>{{ config.public.myValue }}</span></div>
33
</template>
44

55
<script setup>
6+
const config = useRuntimeConfig();
67
</script>

examples/module/test/fixtures/basic/nuxt.config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import MyModule from '../../../src/module'
22

33
export default defineNuxtConfig({
4+
runtimeConfig: {
5+
public: {
6+
myValue: 'original value',
7+
},
8+
},
49
modules: [
510
MyModule
611
]

src/core/server.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ import { useTestContext } from './context'
1010
// eslint-disable-next-line
1111
const kit: typeof _kit = _kit.default || _kit
1212

13-
export async function startServer () {
13+
export interface StartServerOptions {
14+
env?: Record<string, unknown>
15+
}
16+
17+
export async function startServer (options: StartServerOptions = {}) {
1418
const ctx = useTestContext()
1519
await stopServer()
1620
const host = '127.0.0.1'
@@ -26,7 +30,8 @@ export async function startServer () {
2630
_PORT: String(port), // Used by internal _dev command
2731
PORT: String(port),
2832
HOST: host,
29-
NODE_ENV: 'development'
33+
NODE_ENV: 'development',
34+
...options.env
3035
}
3136
})
3237
await waitForPort(port, { retries: 32, host }).catch(() => {})
@@ -53,7 +58,8 @@ export async function startServer () {
5358
...process.env,
5459
PORT: String(port),
5560
HOST: host,
56-
NODE_ENV: 'test'
61+
NODE_ENV: 'test',
62+
...options.env
5763
}
5864
})
5965
await waitForPort(port, { retries: 20, host })

0 commit comments

Comments
 (0)