Skip to content

Commit 63e10ba

Browse files
authored
Added a test helper (#103)
1 parent b2935f0 commit 63e10ba

File tree

3 files changed

+44
-16
lines changed

3 files changed

+44
-16
lines changed

app_template/test/helper.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict'
2+
3+
// This file contains code that we reuse
4+
// between our tests.
5+
6+
const Fastify = require('fastify')
7+
const fp = require('fastify-plugin')
8+
const App = require('../app')
9+
10+
// Fill in this config with all the configurations
11+
// needed for testing the application
12+
function config () {
13+
return {}
14+
}
15+
16+
// automatically build and tear down our instance
17+
function build (t) {
18+
const app = Fastify()
19+
20+
// fastify-plugin ensures that all decorators
21+
// are exposed for testing purposes, this is
22+
// different from the production setup
23+
app.register(fp(App), config())
24+
25+
// tear down our app after we are done
26+
t.tearDown(app.close.bind(app))
27+
28+
return app
29+
}
30+
31+
module.exports = {
32+
config,
33+
build
34+
}

app_template/test/services/example.test.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
'use strict'
22

33
const { test } = require('tap')
4-
const Fastify = require('fastify')
5-
const App = require('../../app')
4+
const { build } = require('../helper')
65

76
test('example is loaded', (t) => {
87
t.plan(2)
9-
const fastify = Fastify()
10-
fastify.register(App)
8+
const app = build(t)
119

12-
fastify.inject({
10+
app.inject({
1311
url: '/example'
1412
}, (err, res) => {
1513
t.error(err)
@@ -20,10 +18,9 @@ test('example is loaded', (t) => {
2018
// It you prefer async/await, use the following
2119
//
2220
// test('example is loaded', async (t) => {
23-
// const fastify = Fastify()
24-
// fastify.register(App)
21+
// const app = build(t)
2522
//
26-
// const res = await fastify.inject({
23+
// const res = await app.inject({
2724
// url: '/example'
2825
// })
2926
// t.equal(res.payload, 'this is an example')

app_template/test/services/root.test.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
'use strict'
22

33
const { test } = require('tap')
4-
const Fastify = require('fastify')
5-
const App = require('../../app')
4+
const { build } = require('../helper')
65

76
test('default root route', (t) => {
87
t.plan(2)
9-
const fastify = Fastify()
10-
fastify.register(App)
8+
const app = build(t)
119

12-
fastify.inject({
10+
app.inject({
1311
url: '/'
1412
}, (err, res) => {
1513
t.error(err)
@@ -20,10 +18,9 @@ test('default root route', (t) => {
2018
// It you prefer async/await, use the following
2119
//
2220
// test('default root route', async (t) => {
23-
// const fastify = Fastify()
24-
// fastify.register(App)
21+
// const app = build(t)
2522
//
26-
// const res = await fastify.inject({
23+
// const res = await app.inject({
2724
// url: '/'
2825
// })
2926
// t.deepEqual(JSON.parse(res.payload), { root: true })

0 commit comments

Comments
 (0)