Skip to content

Commit

Permalink
fix: use restart for server config update
Browse files Browse the repository at this point in the history
  • Loading branch information
BobbieGoede committed Dec 21, 2023
1 parent 49383f1 commit 5aafee4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
7 changes: 4 additions & 3 deletions examples/module/test/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ describe('ssr', async () => {
})

it('changes runtime config', async () => {
await setRuntimeConfig({ public: { myValue: 'overwritten by test!' } })
expect(await $fetch('/')).toContain('<span id="runtime">original value</span></div>')

const html = await $fetch('/')
expect(html).toContain('<span id="runtime">overwritten by test!</span></div>')
await setRuntimeConfig({ public: { myValue: 'overwritten by test!' } }, { restart: true })

expect(await $fetch('/')).toContain('<span id="runtime">overwritten by test!</span></div>')
})
})
10 changes: 8 additions & 2 deletions src/core/runtime-config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { snakeCase } from 'scule'
import { useTestContext } from './context'
import { startServer } from './server'

export function flattenObject(obj: Record<string, unknown> = {}) {
const flattened: Record<string, unknown> = {}
Expand Down Expand Up @@ -36,10 +37,15 @@ export function convertObjectToConfig(obj: Record<string, unknown>, envPrefix: s
return env
}

export async function setRuntimeConfig(config: Record<string, unknown>, envPrefix = 'NUXT_') {
const env = convertObjectToConfig(config, envPrefix)
type SetRuntimeConfigOptions = { envPrefix?: string; restart?: boolean }
export async function setRuntimeConfig(config: Record<string, unknown>, options: SetRuntimeConfigOptions = {}) {
const env = convertObjectToConfig(config, options.envPrefix ?? 'NUXT_')
const ctx = useTestContext()

if (options.restart) {
return await startServer({ env })
}

let updatedConfig = false
ctx.serverProcess?.once('message', (msg: { type: string }) => {
if (msg.type === 'confirm:runtime-config') {
Expand Down

0 comments on commit 5aafee4

Please sign in to comment.