N
Nuxt4mo ago
Titan

How to "mock" an H3 request with query string

I'm writing some unit tests for my "useValidatedQuery/useValidatedBody" functions that take a yup schema and validate the request. I've got this working fine for the body with: ✅ this works
it('returns the body if it is valid', async () => {
const testSchema = object({
email: string().email().required()
})

const h3BodyEvent = {
method: 'POST',
node: { req: { headers: [], body: { email: '[email protected]', foo: 'bar' } } }
}

const result = await useValidatedBody(testSchema, h3Event)

expect(result).toEqual({ email: '[email protected]', foo: 'bar' })
})
it('returns the body if it is valid', async () => {
const testSchema = object({
email: string().email().required()
})

const h3BodyEvent = {
method: 'POST',
node: { req: { headers: [], body: { email: '[email protected]', foo: 'bar' } } }
}

const result = await useValidatedBody(testSchema, h3Event)

expect(result).toEqual({ email: '[email protected]', foo: 'bar' })
})
However I cannot get it work with the query string, I just can't work out the shape of the event, I've tried it in all the below places and none of them seem to work: ❌ none of these work
const h3QueryEvent = {
method: 'GET',
queryString: 'email=bar', <-- doesn't work here
url: 'https://test.com/?email=bar', <-- doesn't work here
node: { req: { headers: [], queryString: 'email=bar', search: 'email=bar', url: { search: 'email=bar' } } }, <-- doesn't work here
web: { request: { url: 'https://test.com/?email=bar' }, url: 'https://test.com/?email=bar' } <-- doesn't work here
}
const h3QueryEvent = {
method: 'GET',
queryString: 'email=bar', <-- doesn't work here
url: 'https://test.com/?email=bar', <-- doesn't work here
node: { req: { headers: [], queryString: 'email=bar', search: 'email=bar', url: { search: 'email=bar' } } }, <-- doesn't work here
web: { request: { url: 'https://test.com/?email=bar' }, url: 'https://test.com/?email=bar' } <-- doesn't work here
}
The function it gets passed into is the H3 getQuery function: const query = getQuery(event) currently returning empty object
1 Reply
Titan
TitanOP4mo ago
figured it out:
const h3Event = {
method: 'GET',
path: '?email=bar'
}
const h3Event = {
method: 'GET',
path: '?email=bar'
}
Want results from more Discord servers?
Add your server