Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
446 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
const usersData = [{ | ||
id: 1, | ||
name: 'John Snow', | ||
email: 'john@got.com', | ||
}, | ||
{ | ||
id: 2, | ||
name: 'Daenerys Targaryen', | ||
email: 'daenerys@got.com', | ||
}, | ||
{ | ||
id: 3, | ||
name: 'Arya Stark', | ||
email: 'arya@got.com', | ||
}, | ||
{ | ||
id: 4, | ||
name: 'Rhaenyra Targaryen', | ||
email: 'rhaenyra@hod.com', | ||
}, | ||
] | ||
export { usersData } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"name": "@vitest/example-fastify", | ||
"type": "module", | ||
"private": true, | ||
"license": "MIT", | ||
"main": "index.js", | ||
"scripts": { | ||
"dev": "NODE_ENV=development tsx watch src", | ||
"test": "vitest", | ||
"test:ui": "vitest --ui", | ||
"test:run": "vitest run" | ||
}, | ||
"devDependencies": { | ||
"@vitest/ui": "latest", | ||
"axios": "^0.26.1", | ||
"fastify": "4.5.3", | ||
"supertest": "6.2.4", | ||
"tsx": "^3.9.0", | ||
"vite": "latest", | ||
"vitest": "latest" | ||
}, | ||
"stackblitz": { | ||
"startCommand": "npm run test:ui" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import type { FastifyInstance } from 'fastify' | ||
import Fastify from 'fastify' | ||
import { usersData } from '../mockData' | ||
|
||
const app: FastifyInstance = Fastify({ | ||
logger: process.env.NODE_ENV === 'development', | ||
}) | ||
|
||
app.get('/users', async () => { | ||
return usersData | ||
}) | ||
|
||
export default app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import app from './app' | ||
|
||
const start = async () => { | ||
try { | ||
await app.listen({ port: 3000 }) | ||
} | ||
catch (err) { | ||
app.log.error(err) | ||
process.exit(1) | ||
} | ||
} | ||
|
||
start() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { afterAll, expect, test } from 'vitest' | ||
import supertest from 'supertest' | ||
import axios from 'axios' | ||
|
||
import app from '../src/app' | ||
import { usersData } from '../mockData' | ||
|
||
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 axios', async () => { | ||
await app.listen() | ||
await app.ready() | ||
|
||
const address = app.server.address() | ||
const port = typeof address === 'string' ? address : address?.port | ||
|
||
const response = await axios.get(`http://localhost:${port}/users`) | ||
|
||
expect(response.data).toHaveLength(4) | ||
expect(response.data).toStrictEqual(usersData) | ||
}) | ||
|
||
afterAll(async () => { | ||
await app.close() | ||
}) | ||
|
Oops, something went wrong.