Skip to content

Commit f2def01

Browse files
RafaelGSSjsumners
andauthored
feat: add tests example (#19)
* feat: add tests example * update: cleanup dependencies * update: add use strict on top Co-authored-by: James Sumners <james@sumners.email> * refactor: split server to a separated file * refactor: use only fastify-mongodb * update: add use strict * update: use teardown to cleanup db * update: remove onClose hook Co-authored-by: James Sumners <james@sumners.email>
1 parent ff451cd commit f2def01

File tree

6 files changed

+114
-0
lines changed

6 files changed

+114
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Here a list of the projects with a description, search in this page what you are
1515
| [validation-messages] | `schema` `validation` | How you can customize the error messages of input schema validation |
1616
| [winston-logger] | `logger` | Example how to use winston as a custom logger |
1717
| [typescript decorators] | `typescript` | Example how to use typescript decorators to build application |
18+
| [tests] | `tests` | Example of how to test your fastify application |
1819

1920

2021
## External Projects

tests/app.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict'
2+
3+
const fastifyMongodb = require('fastify-mongodb')
4+
5+
function app(fastify, config, done) {
6+
fastify.register(fastifyMongodb, { url: config.mongodb.url })
7+
fastify.get('/', async function (_req, reply) {
8+
const db = this.mongo.client.db()
9+
const results = await db.collection('examples').find()
10+
return reply.send(results)
11+
})
12+
13+
done()
14+
}
15+
16+
module.exports = app
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use strict'
2+
3+
const Fastify = require('fastify')
4+
const fp = require('fastify-plugin')
5+
6+
const clean = require('mongo-clean')
7+
8+
const App = require('../app')
9+
10+
const url = 'mongodb://localhost/prod'
11+
const database = 'tests'
12+
13+
// Fill in this config with all the configurations
14+
// needed for testing the application
15+
function config () {
16+
return {
17+
mongodb: {
18+
url,
19+
}
20+
}
21+
}
22+
23+
// automatically build and tear down our instance
24+
function build (t) {
25+
const app = Fastify()
26+
app.register(fp(App), config())
27+
28+
t.teardown(async () => {
29+
await clean(app.mongo.client.db(database))
30+
await app.close()
31+
})
32+
return app
33+
}
34+
35+
module.exports = {
36+
build
37+
}
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const { test } = require('tap')
2+
const { build } = require('./mongodb-setup')
3+
4+
test('GET / • returns 200', (t) => {
5+
t.plan(2);
6+
const fastify = build(t);
7+
8+
fastify.inject(
9+
{
10+
method: 'GET',
11+
url: '/',
12+
},
13+
(err, response) => {
14+
t.error(err);
15+
t.strictEqual(response.statusCode, 200);
16+
},
17+
);
18+
});

tests/package.json

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "database-connection",
3+
"version": "1.0.0",
4+
"description": "example of tests using database",
5+
"main": "mongodb-test.js",
6+
"scripts": {
7+
"test:database": "tap ./database-connection/mongodb.test.js"
8+
},
9+
"author": "Fastify",
10+
"license": "MIT",
11+
"dependencies": {
12+
"fastify": "^2.3.0",
13+
"fastify-mongodb": "^4.1.0",
14+
"fastify-plugin": "^3.0.0"
15+
},
16+
"devDependencies": {
17+
"mongo-clean": "^2.0.0",
18+
"tap": "^14.11.0"
19+
}
20+
}

tests/server.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict'
2+
3+
const Fastify = require('fastify')
4+
const fp = require('fastify-plugin')
5+
const app = require('./app')
6+
7+
async function server() {
8+
const fastify = Fastify({ logger: true })
9+
const database = process.env.MONGO_URI || 'mongodb://localhost/prod'
10+
fastify.register(
11+
fp(app),
12+
{
13+
mongodb: {
14+
url: database
15+
}
16+
}
17+
)
18+
19+
fastify.listen(3000)
20+
}
21+
22+
server()

0 commit comments

Comments
 (0)