Open vctrtvfrrr opened 2 months ago
To address the issue of overriding runtimeConfig
in your vitest.config.ts
for Nuxt3 server endpoint testing, ensure your configuration in vitest.config.ts
correctly specifies the overrides. Here's a refined approach:
vitest.config.ts
, ensure you're using the overrides
option to specify your custom configuration. This should directly target the settings you wish to modify for your tests, such as the database connection string.import { defineVitestConfig } from '@nuxt/test-utils/config';
export default defineVitestConfig({
test: {
environment: 'nuxt',
globals: true,
environmentOptions: {
nuxt: {
overrides: {
runtimeConfig: {
publicRuntimeConfig: {
databaseUrl: 'your_in-memory_database_connection_string',
},
},
},
},
},
},
});
.env.test
file for your test environment.export default defineVitestConfig({
test: {
environmentOptions: {
nuxt: {
dotenv: {
fileName: '.env.test',
},
overrides: {
// Your overrides here
},
},
},
},
});
useRuntimeConfig
in Tests: For more granular control in tests, you can mock the useRuntimeConfig
composable function directly in your test files using mockNuxtImport
from @nuxt/test-utils/runtime
. This allows you to define runtime configurations specifically for each test case.By ensuring your vitest.config.ts
is correctly set up to apply these overrides, you can effectively isolate your test environment and use an in-memory database or a specific test database, avoiding interference with your production or development environments.
To continue the conversation, mention @dosu.
Environment
Reproduction
https://github.com/vctrtvfrrr/hinario-reformado
Describe the bug
I can't test the Nuxt3 server endpoints. I'm using
vitest
and@nuxt/test-utils
to make API calls, but I haven't even been able to override the Nuxt runtime settings:Nuxt's official documentation doesn't cover server testing very well and Nitro/H3 doesn't even have testing documentation.
Additional context
No response
Logs
No response