-
Notifications
You must be signed in to change notification settings - Fork 104
feat(e2e): support env
option for startServer
#640
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
ae80876
b7355dc
4dd3e8e
fe1c8d3
cc5305d
ca54fc3
0e60b70
46df850
8bf8cc7
7985b70
28d8bc5
4ad32a6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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> |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { snakeCase } from 'scule' | ||
import { startServer } from './server' | ||
|
||
const NUXT_ENV_PREFIX = 'NUXT_' | ||
|
||
export function flattenObject(obj: Record<string, unknown> = {}) { | ||
const flattened: Record<string, unknown> = {} | ||
|
||
for (const key in obj) { | ||
if (!(key in obj)) continue | ||
|
||
const entry = obj[key] | ||
if (typeof entry !== 'object' || entry == null) { | ||
flattened[key] = obj[key] | ||
continue | ||
} | ||
const flatObject = flattenObject(entry as Record<string, unknown>) | ||
|
||
for (const x in flatObject) { | ||
if (!(x in flatObject)) continue | ||
|
||
flattened[key + '_' + x] = flatObject[x] | ||
} | ||
} | ||
|
||
return flattened | ||
} | ||
|
||
export function convertObjectToConfig(obj: Record<string, unknown>) { | ||
const makeEnvKey = (str: string) => `${NUXT_ENV_PREFIX}${snakeCase(str).toUpperCase()}` | ||
|
||
const env: Record<string, unknown> = {} | ||
const flattened = flattenObject(obj) | ||
for (const key in flattened) { | ||
env[makeEnvKey(key)] = flattened[key] | ||
} | ||
|
||
return env | ||
} | ||
|
||
export async function setRuntimeConfig(env: Record<string, unknown>) { | ||
const converted = convertObjectToConfig(env) | ||
await startServer(converted) | ||
|
||
// restore | ||
return async () => startServer() | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While not fully documented, it might be worth to not hardcode it (using it as default is fine) but rely on the nitro runtime config value. See nuxt/nuxt#23714
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I haven't seen that being used before, I've changed the function to accept an optional argument
envPrefix
.It would be nice if we could retrieve which prefix is being used in the running fixture, I'm not sure how to do that. An alternative would be checking if
nuxtConfig.runtimeConfig.nitro.envPrefix
is present in the test context and using that.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, I think the latter would be the easiest, defaulting to
NUXT_
otherwise. But an option/2nd optional arg is fine too for me