nuxt / test-utils

🧪 Test utilities for Nuxt
http://nuxt.com/docs/getting-started/testing
MIT License
313 stars 81 forks source link

[nuxt] instance is not available in server utils unit tests with useRuntimeConfig() #949

Open alexcroox opened 1 week ago

alexcroox commented 1 week ago

Environment

Reproduction

https://stackblitz.com/edit/github-gu1sj1-si6cpn?file=server%2Futils%2Fexample-util.nuxt.spec.ts&startScript=start

Run -> npm run test

Describe the bug

After some recent dependancy upgrades around Nuxt I'm getting the [nuxt] instance unavailable error on tests that call functions in server/utils that use useRuntimeConfig.

See the reproduction link for an example. Thanks.

Additional context

No response

Logs

No response

stackblitz[bot] commented 1 week ago

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

danielroe commented 1 week ago

The runtime environment for nuxt test utils is for testing client-side/browser code, rather than server code. (See https://github.com/nuxt/test-utils/issues/531.) So I wouldn't expect it to work.

Does code using useRuntimeConfig work fine when outside the server/ folder?

alexcroox commented 1 week ago

ah that's a real shame. It used to work before compatibility version 4 was introduced, here is a repro with it working fine in the old folder structure: https://stackblitz.com/edit/github-gu1sj1-xnyzbk?file=server%2Futils%2Fexample-util.nuxt.spec.ts&startScript=start

and yes useRuntimeConfig works outside the server folder in v4