Unit Tests for Nuxt's /server Directory
Do you have knowledge about how to conduct unit tests for the /server part of Nuxt? Specifically, I'm interested in testing the defineEventHandler function.
But I can't find any documentation on the subject...
9 Replies
Good luck 😄
Alright, throwing a bunch of code in here isn't very nice. I'll try to explain...
This is an example for a unit test. It is running in the nuxt context, without a running dev environment.
I import the handler I want to test as getHandler, I feed it with a partial event object (like h3 does), in this case with a route parameter, and check the outcome. First one is good, the second one triggers an error.
Thanks for your code, you help me a lot with this! On the other hand I have an error, did you already get it?
I think it's in event intialization, something miss no ?
You probably missed the stub that replaces the function handling the body
This
const { email, password } = await readBody(event);
?yup
With something like that ?
I'm just guessing here, but yeah, that kinda looks good!
copilot said:
vitest.stubGlobal("readBody", (e: any) => e.body);
But I guess that depends on how you build the eventI get eventHandler is not defined
when using
import getHandler from './signed-url.post'
in my test