Correct Vitest environment for server tests #1407
-
I just found out about test-utils' support for Vitest projects from the updated testing setup documentation at https://nuxt.com/docs/4.x/getting-started/testing#setup. The sample test organisation structure only covers stuff inside the Note, I consider end-to-end tests separate from all of these, as they test the application as a whole, and should assume no knowledge of the application internals, so a |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @benedictleejh, For anything in your I usually structure it like this: test/
├── nuxt/
│ └── server/
│ └── my-api.spec.ts # <-- 'nuxt' env
└── unit/
└── some-util.spec.ts # <-- 'node' env So for testing an API route, you can just do: // test/nuxt/server/my-api.spec.ts
import { describe, it, expect } from 'vitest'
it('works', async () => {
const result = await $fetch('/api/my-route')
expect(result).toBeDefined()
}) Basically, if the code needs any Nuxt magic (auto-imports, composables, etc.), it needs the |
Beta Was this translation helpful? Give feedback.
Hi @benedictleejh,
For anything in your
server/
dir, you'll want thenuxt
environment. Thenode
env is totally separate, knows nothing about Nuxt/Nitro. It's just for plain utils. And nope, there's no separatenitro
env, as far as I know. It's all part of thenuxt
one.I usually structure it like this:
So for testing an API route, you can just do:
Basically, if the code nee…