Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/vitest-dev/vitest. Next generation testing framework powered by Vite. vitest.dev
test testing-tools vite
Something went wrong. Try again.
TypeScript
123456789101112131415161718192021222324252627282930313233343536373839404142import supertest from 'supertest'import { afterAll, expect, test } from 'vitest'import { usersData } from '../mockData'import app from '../src/app'
test('with HTTP injection', async () => { const response = await app.inject({ method: 'GET', url: '/users', })
expect(response.statusCode).toBe(200) expect(JSON.parse(response.payload)).toHaveLength(4) expect(JSON.parse(response.payload)).toStrictEqual(usersData)})
test('with a running server', async () => { await app.ready()
const response = await supertest(app.server).get('/users').expect(200)
expect(response.body).toHaveLength(4) expect(response.body).toStrictEqual(usersData)})
test('with fetch', async () => { await app.listen() await app.ready()
const address = app.server.address() const port = typeof address === 'string' ? address : address?.port
const response = await fetch(`http://localhost:${port}/users`).then((r) => r.json())
expect(response).toHaveLength(4) expect(response).toStrictEqual(usersData)})
afterAll(async () => { await app.close()})