如何测试route.js?
import { describe, expect, it, vi } from 'vitest'
import { POST } from './route'
describe('[POST] /api/test', () => {
it('should respond with a `400` status code and error message', async () => {
const request = new Request('http://localhost:3000/api/test', {
method: 'POST'
})
const res = await POST(request)
const body = await res.json()
expect(res.status).toBe(400)
})
})
字符串
它在.json()
上失败,因为它在Request
对象上不存在
export const POST = async (request: Request) => {
const data = await request.json()
...
}
型
我使用vitest与jsdom
环境。
1条答案
按热度按时间v09wglhw1#
经过一番折腾,答案是用
NextRequest
PackageRequest
字符串
希望对你也有帮助