diff --git a/examples/module/package.json b/examples/module/package.json index 91fc7a6e5..f59d837ec 100644 --- a/examples/module/package.json +++ b/examples/module/package.json @@ -1,5 +1,6 @@ { "private": true, + "name": "module", "description": "My new Nuxt module", "repository": "your-org/my-module", "license": "MIT", diff --git a/examples/module/test/basic.test.ts b/examples/module/test/basic.test.ts index 2e3df6728..36c38ba47 100644 --- a/examples/module/test/basic.test.ts +++ b/examples/module/test/basic.test.ts @@ -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({ @@ -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('
basic
') + expect(html).toContain('
basic original value
') + }) + + it('changes runtime config and restarts', async () => { + await startServer({ env: { NUXT_PUBLIC_MY_VALUE: 'overwritten by test!' } }) + + const html = await $fetch('/') + expect(html).toContain('
basic overwritten by test!
') + + await startServer() + const htmlRestored = await $fetch('/') + expect(htmlRestored).toContain('
basic original value
') }) }) diff --git a/examples/module/test/fixtures/basic/app.vue b/examples/module/test/fixtures/basic/app.vue index 29a9c81fa..0e4c395fe 100644 --- a/examples/module/test/fixtures/basic/app.vue +++ b/examples/module/test/fixtures/basic/app.vue @@ -1,6 +1,7 @@ diff --git a/examples/module/test/fixtures/basic/nuxt.config.ts b/examples/module/test/fixtures/basic/nuxt.config.ts index 1bc2f7ccd..5b1ef6ef3 100644 --- a/examples/module/test/fixtures/basic/nuxt.config.ts +++ b/examples/module/test/fixtures/basic/nuxt.config.ts @@ -1,6 +1,11 @@ import MyModule from '../../../src/module' export default defineNuxtConfig({ + runtimeConfig: { + public: { + myValue: 'original value', + }, + }, modules: [ MyModule ] diff --git a/src/core/server.ts b/src/core/server.ts index 1305f7b5e..cbd267b36 100644 --- a/src/core/server.ts +++ b/src/core/server.ts @@ -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 +} + +export async function startServer (options: StartServerOptions = {}) { const ctx = useTestContext() await stopServer() const host = '127.0.0.1' @@ -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(() => {}) @@ -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 })