nuxt / test-utils

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

Cant use utils/composables within tests #802

Closed MickL closed 3 months ago

MickL commented 3 months ago

Describe the feature

Within tests utils and composables (especially from the server folder) should be available, currently they are not:

https://stackblitz.com/edit/github-krtpdn-tvzxcc?file=test%2Fsomething.spec.ts

Usecase: I want to test server endpoints but for that I need to do some database queries. Within my application I use Drizzle for that which I wrapped in a util. I would like to reuse exactly this utils and possibly other composables.

Additional information

Final checks

danielroe commented 3 months ago

Currently only the client-side half of Nuxt is available within unit testing environment, which does include your composables in ~/composables and ~/utils. For support of server composables or server environment (such as database access), we can track in https://github.com/nuxt/test-utils/issues/531.

I note that you are using e2e tests - nothing in your Nuxt environment is exposed in an e2e environment. You would need to directly fetch from an api endpoint. You can run code within your app that tests for if (import.meta.test) if you want to run something conditionally only in a test env (it will be tree-shaken out when building).

MickL commented 3 months ago

Thanks for your answers! I am getting "somewhere" testing my server routes, if there wouldnt be this bug: https://github.com/nuxt/test-utils/issues/795 - So I am kinda working blind + every time I need to wait 15s for the next result.

danielroe commented 3 months ago

Yeah, that's a pain.