Skip to content

Commit

Permalink
feat: added fastify example (#1991)
Browse files Browse the repository at this point in the history
  • Loading branch information
irvile committed Sep 8, 2022
1 parent 08feae1 commit e5cf1c1
Show file tree
Hide file tree
Showing 7 changed files with 446 additions and 33 deletions.
1 change: 1 addition & 0 deletions examples/README.md
@@ -1,6 +1,7 @@
| Example | Source | Playground |
|---|---|---|
| `basic` | [GitHub](https://github.com/vitest-dev/vitest/tree/main/examples/basic) | [Play Online](https://stackblitz.com/fork/github/vitest-dev/vitest/tree/main/examples/basic?initialPath=__vitest__) |
| `fastify` | [GitHub](https://github.com/vitest-dev/vitest/tree/main/examples/fastify) | [Play Online](https://stackblitz.com/fork/github/vitest-dev/vitest/tree/main/examples/fastify?initialPath=__vitest__) |
| `graphql` | [GitHub](https://github.com/vitest-dev/vitest/tree/main/examples/graphql) | [Play Online](https://stackblitz.com/fork/github/vitest-dev/vitest/tree/main/examples/graphql?initialPath=__vitest__) |
| `lit` | [GitHub](https://github.com/vitest-dev/vitest/tree/main/examples/lit) | [Play Online](https://stackblitz.com/fork/github/vitest-dev/vitest/tree/main/examples/lit?initialPath=__vitest__) |
| `mocks` | [GitHub](https://github.com/vitest-dev/vitest/tree/main/examples/mocks) | [Play Online](https://stackblitz.com/fork/github/vitest-dev/vitest/tree/main/examples/mocks?initialPath=__vitest__) |
Expand Down
22 changes: 22 additions & 0 deletions examples/fastify/mockData.ts
@@ -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 }
25 changes: 25 additions & 0 deletions examples/fastify/package.json
@@ -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"
}
}
13 changes: 13 additions & 0 deletions examples/fastify/src/app.ts
@@ -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
13 changes: 13 additions & 0 deletions examples/fastify/src/index.ts
@@ -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()
46 changes: 46 additions & 0 deletions examples/fastify/test/app.test.ts
@@ -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()
})

0 comments on commit e5cf1c1

Please sign in to comment.