Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added fastify example #1991

Merged
merged 3 commits into from Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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()
})