Skip to content
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

feat!: read .env.test + allow overriding with nuxt.dotenv #655

Merged
merged 5 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/app-vitest-full/.env.test
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
NUXT_PUBLIC_TEST_VALUE=123
1 change: 1 addition & 0 deletions examples/app-vitest-full/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export default defineNuxtConfig({
runtimeConfig: {
public: {
hello: 'world',
testValue: 'default'
},
},
})
1 change: 1 addition & 0 deletions examples/app-vitest-full/tests/nuxt/config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ it('should return the runtimeConfig from nuxt.config', () => {
expect(config).toBeTypeOf('object')
expect(config?.public).toEqual({
hello: 'world',
testValue: 123,
})
})
4 changes: 1 addition & 3 deletions examples/app-vitest-full/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ export default defineVitestConfig({
environmentOptions: {
nuxt: {
rootDir: fileURLToPath(new URL('./', import.meta.url)),
domEnvironment:
(process.env.VITEST_DOM_ENV as 'happy-dom' | 'jsdom') ?? 'happy-dom',

domEnvironment: (process.env.VITEST_DOM_ENV as 'happy-dom' | 'jsdom') ?? 'happy-dom',
mock: {
indexedDb: true,
},
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@
"dependencies": {
"@nuxt/kit": "^3.8.2",
"@nuxt/schema": "^3.8.2",
"c12": "^1.5.1",
"consola": "^3.2.3",
"defu": "^6.1.3",
"destr": "^2.0.2",
"estree-walker": "^3.0.3",
"execa": "^8.0.1",
"fake-indexeddb": "^5.0.1",
Expand All @@ -53,6 +55,7 @@
"pathe": "^1.1.1",
"perfect-debounce": "^1.0.0",
"radix3": "^1.1.0",
"scule": "^1.1.1",
"std-env": "^3.6.0",
"ufo": "^1.3.2",
"unenv": "^1.8.0",
Expand Down
14 changes: 13 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 40 additions & 8 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,44 @@
import type { Nuxt, NuxtConfig } from '@nuxt/schema'
import type { InlineConfig as VitestConfig } from 'vitest'
import { defineConfig } from 'vite'
import { setupDotenv } from 'c12'
import type { DotenvOptions } from 'c12'
import type { InlineConfig } from 'vite'
import { defu } from 'defu'
import { createResolver } from '@nuxt/kit'

import { applyEnv } from './utils'

interface GetVitestConfigOptions {
nuxt: Nuxt
viteConfig: InlineConfig
}

interface LoadNuxtOptions {
dotenv?: Partial<DotenvOptions>
overrides?: Partial<NuxtConfig>
}

// https://github.com/nuxt/framework/issues/6496
async function startNuxtAndGetViteConfig(
rootDir = process.cwd(),
overrides?: Partial<NuxtConfig>
options: LoadNuxtOptions = {}
) {
const { loadNuxt, buildNuxt } = await import('@nuxt/kit')
const nuxt = await loadNuxt({
cwd: rootDir,
dev: false,
dotenv: defu(options.dotenv, {
cwd: rootDir,
fileName: '.env.test'
}),
overrides: defu(
{
ssr: false,
test: true,
modules: ['@nuxt/test-utils/module'],
},
overrides
options.overrides
),
})

Expand Down Expand Up @@ -61,14 +74,17 @@ const excludedPlugins = [

export async function getVitestConfigFromNuxt(
options?: GetVitestConfigOptions,
overrides?: NuxtConfig
loadNuxtOptions: LoadNuxtOptions = {}
): Promise<InlineConfig & { test: VitestConfig }> {
const { rootDir = process.cwd(), ..._overrides } = overrides || {}
const { rootDir = process.cwd(), ..._overrides } = loadNuxtOptions.overrides || {}

if (!options) {
options = await startNuxtAndGetViteConfig(rootDir, {
test: true,
..._overrides
dotenv: loadNuxtOptions.dotenv,
overrides: {
test: true,
..._overrides
}
})
}

Expand All @@ -83,7 +99,13 @@ export async function getVitestConfigFromNuxt(
test: {
dir: process.cwd(),
environmentOptions: {
nuxtRuntimeConfig: options.nuxt.options.runtimeConfig,
nuxtRuntimeConfig: applyEnv(structuredClone(options.nuxt.options.runtimeConfig), {
prefix: 'NUXT_',
env: await setupDotenv(defu(loadNuxtOptions.dotenv, {
cwd: rootDir,
fileName: '.env.test'
})),
}),
nuxtRouteRules: defu(
{},
options.nuxt.options.routeRules,
Expand Down Expand Up @@ -181,7 +203,10 @@ export function defineVitestConfig(config: InlineConfig & { test?: VitestConfig

return defu(
config,
await getVitestConfigFromNuxt(undefined, structuredClone(overrides)),
await getVitestConfigFromNuxt(undefined, {
dotenv: config.test?.environmentOptions?.nuxt?.dotenv,
overrides: structuredClone(overrides)
}),
)
})
}
Expand All @@ -195,6 +220,13 @@ declare module 'vitest' {
* @default {http://localhost:3000}
*/
url?: string
/**
* You can define how environment options are read when loading the Nuxt configuration.
*/
dotenv?: Partial<DotenvOptions>
/**
* Configuration that will override the values in your `nuxt.config` file.
*/
overrides?: NuxtConfig
/**
* The id of the root div to which the app should be mounted. You should also set `app.rootId` to the same value.
Expand Down
50 changes: 50 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// TODO: export these
// https://github.com/unjs/nitro/tree/main/src/runtime/utils.env.ts

import destr from 'destr'
import { snakeCase } from 'scule'

export type EnvOptions = {
env?: Record<string, any>
prefix?: string
altPrefix?: string
}

export function getEnv (key: string, opts: EnvOptions) {
const env = opts.env ?? process.env
const envKey = snakeCase(key).toUpperCase()
return destr(
env[opts.prefix + envKey] ?? env[opts.altPrefix + envKey]
)
}

function _isObject (input: unknown) {
return typeof input === 'object' && !Array.isArray(input)
}

export function applyEnv (obj: Record<string, any>, opts: EnvOptions, parentKey = '') {
for (const key in obj) {
const subKey = parentKey ? `${parentKey}_${key}` : key
const envValue = getEnv(subKey, opts)
if (_isObject(obj[key])) {
// Same as before
if (_isObject(envValue)) {
obj[key] = { ...obj[key], ...(envValue as object) }
applyEnv(obj[key], opts, subKey)
}
// If envValue is undefined
// Then proceed to nested properties
else if (envValue === undefined) {
applyEnv(obj[key], opts, subKey)
}
// If envValue is a primitive other than undefined
// Then set objValue and ignore the nested properties
else {
obj[key] = envValue ?? obj[key]
}
} else {
obj[key] = envValue ?? obj[key]
}
}
return obj
}
Loading