Skip to content

Commit

Permalink
feat: body validation vitest
Browse files Browse the repository at this point in the history
  • Loading branch information
sandros94 committed Nov 12, 2024
1 parent f7421cd commit 634f813
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 0 deletions.
60 changes: 60 additions & 0 deletions test/__snapshots__/body.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`useValidatedBody > doesn't throw 400 Bad Request if body does not match validation schema 1`] = `
{
"issues": [
{
"expected": "boolean",
"kind": "schema",
"message": "Invalid type: Expected boolean but received undefined",
"path": [
{
"input": {},
"key": "required",
"origin": "value",
"type": "object",
},
],
"received": "undefined",
"type": "boolean",
},
],
"output": {},
"success": false,
"typed": false,
}
`;

exports[`useValidatedBody > returns 200 OK if body matches validation schema 1`] = `
{
"required": true,
}
`;

exports[`useValidatedBody > throws 400 Bad Request if body does not match validation schema 1`] = `
{
"data": {
"issues": [
{
"expected": "boolean",
"kind": "schema",
"message": "Invalid type: Expected boolean but received undefined",
"path": [
{
"input": {},
"key": "required",
"origin": "value",
"type": "object",
},
],
"received": "undefined",
"type": "boolean",
},
],
"name": "ValiError",
},
"stack": [],
"statusCode": 400,
"statusMessage": "Bad Request",
}
`;
49 changes: 49 additions & 0 deletions test/body.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import type { App } from 'h3'
import type { Test } from 'supertest'
import type TestAgent from 'supertest/lib/agent'
import { createApp, eventHandler, toNodeListener } from 'h3'
import supertest from 'supertest'
import { beforeEach, describe, expect, it } from 'vitest'
import { useSafeValidatedBody, useValidatedBody, v } from '../src'

describe('useValidatedBody', () => {
let app: App
let request: TestAgent<Test>

beforeEach(() => {
app = createApp({ debug: false })
request = supertest(toNodeListener(app))
})

const bodySchema = v.object({
optional: v.optional(v.string()),
required: v.boolean(),
})

it('returns 200 OK if body matches validation schema', async () => {
app.use('/validate', eventHandler(event => useValidatedBody(event, bodySchema)))

const res = await request.post('/validate').send({ required: true })

expect(res.status).toEqual(200)
expect(res.body).toMatchSnapshot()
})

it('throws 400 Bad Request if body does not match validation schema', async () => {
app.use('/validate', eventHandler(event => useValidatedBody(event, bodySchema)))

const res = await request.post('/validate').send({})

expect(res.status).toEqual(400)
expect(res.body).toMatchSnapshot()
})

it('doesn\'t throw 400 Bad Request if body does not match validation schema', async () => {
app.use('/validate', eventHandler(event => useSafeValidatedBody(event, bodySchema)))

const res = await request.post('/validate').send({})

expect(res.status).toEqual(200)
expect(res.body).toMatchSnapshot()
})
})

0 comments on commit 634f813

Please sign in to comment.