Skip to content

Commit

Permalink
feat(e2e): support env option for startServer (#640)
Browse files Browse the repository at this point in the history
  • Loading branch information
BobbieGoede authored Jan 17, 2024
1 parent fb306e0 commit b693232
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 6 deletions.
1 change: 1 addition & 0 deletions examples/module/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"private": true,
"name": "module",
"description": "My new Nuxt module",
"repository": "your-org/my-module",
"license": "MIT",
Expand Down
15 changes: 13 additions & 2 deletions examples/module/test/basic.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { fileURLToPath } from 'node:url'
import { describe, expect, it } from 'vitest'
import { $fetch, setup } from '@nuxt/test-utils/e2e'
import { $fetch, setup, startServer } from '@nuxt/test-utils/e2e'

describe('ssr', async () => {
await setup({
Expand All @@ -10,6 +10,17 @@ describe('ssr', async () => {
it('renders the index page', async () => {
// Get response to a server-rendered page with `$fetch`.
const html = await $fetch('/')
expect(html).toContain('<div>basic</div>')
expect(html).toContain('<div>basic <span>original value</span></div>')
})

it('changes runtime config and restarts', async () => {
await startServer({ env: { NUXT_PUBLIC_MY_VALUE: 'overwritten by test!' } })

const html = await $fetch('/')
expect(html).toContain('<div>basic <span>overwritten by test!</span></div>')

await startServer()
const htmlRestored = await $fetch('/')
expect(htmlRestored).toContain('<div>basic <span>original value</span></div>')
})
})
3 changes: 2 additions & 1 deletion examples/module/test/fixtures/basic/app.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<template>
<div>basic</div>
<div>basic <span>{{ config.public.myValue }}</span></div>
</template>

<script setup>
const config = useRuntimeConfig();
</script>
5 changes: 5 additions & 0 deletions examples/module/test/fixtures/basic/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import MyModule from '../../../src/module'

export default defineNuxtConfig({
runtimeConfig: {
public: {
myValue: 'original value',
},
},
modules: [
MyModule
]
Expand Down
12 changes: 9 additions & 3 deletions src/core/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import { useTestContext } from './context'
// eslint-disable-next-line
const kit: typeof _kit = _kit.default || _kit

export async function startServer () {
export interface StartServerOptions {
env?: Record<string, unknown>
}

export async function startServer (options: StartServerOptions = {}) {
const ctx = useTestContext()
await stopServer()
const host = '127.0.0.1'
Expand All @@ -26,7 +30,8 @@ export async function startServer () {
_PORT: String(port), // Used by internal _dev command
PORT: String(port),
HOST: host,
NODE_ENV: 'development'
NODE_ENV: 'development',
...options.env
}
})
await waitForPort(port, { retries: 32, host }).catch(() => {})
Expand All @@ -53,7 +58,8 @@ export async function startServer () {
...process.env,
PORT: String(port),
HOST: host,
NODE_ENV: 'test'
NODE_ENV: 'test',
...options.env
}
})
await waitForPort(port, { retries: 20, host })
Expand Down

0 comments on commit b693232

Please sign in to comment.