Chapter 11: update example microservice

This commit is contained in:
Beth Griggs
2020-11-15 01:11:41 +00:00
parent 791aac76d7
commit cb0e52e541
11 changed files with 146 additions and 9 deletions
@@ -0,0 +1,34 @@
'use strict'
// This file contains code that we reuse
// between our tests.
const Fastify = require('fastify')
const fp = require('fastify-plugin')
const App = require('../app')
// Fill in this config with all the configurations
// needed for testing the application
function config () {
return {}
}
// automatically build and tear down our instance
function build (t) {
const app = Fastify()
// fastify-plugin ensures that all decorators
// are exposed for testing purposes, this is
// different from the production setup
app.register(fp(App), config())
// tear down our app after we are done
t.tearDown(app.close.bind(app))
return app
}
module.exports = {
config,
build
}
@@ -0,0 +1,26 @@
'use strict'
const { test } = require('tap')
const Fastify = require('fastify')
const Support = require('../../plugins/support')
test('support works standalone', async (t) => {
const fastify = Fastify()
fastify.register(Support)
await fastify.ready()
t.equal(fastify.someSupport(), 'hugs')
})
// You can also use plugin with opts in fastify v2
//
// test('support works standalone', (t) => {
// t.plan(2)
// const fastify = Fastify()
// fastify.register(Support)
//
// fastify.ready((err) => {
// t.error(err)
// t.equal(fastify.someSupport(), 'hugs')
// })
// })
@@ -0,0 +1,27 @@
'use strict'
const { test } = require('tap')
const { build } = require('../helper')
test('example is loaded', async (t) => {
const app = build(t)
const res = await app.inject({
url: '/example'
})
t.equal(res.payload, 'this is an example')
})
// inject callback style:
//
// test('example is loaded', (t) => {
// t.plan(2)
// const app = build(t)
//
// app.inject({
// url: '/example'
// }, (err, res) => {
// t.error(err)
// t.equal(res.payload, 'this is an example')
// })
// })
@@ -0,0 +1,27 @@
'use strict'
const { test } = require('tap')
const { build } = require('../helper')
test('default root route', async (t) => {
const app = build(t)
const res = await app.inject({
url: '/'
})
t.deepEqual(JSON.parse(res.payload), { root: true })
})
// inject callback style:
//
// test('default root route', (t) => {
// t.plan(2)
// const app = build(t)
//
// app.inject({
// url: '/'
// }, (err, res) => {
// t.error(err)
// t.deepEqual(JSON.parse(res.payload), { root: true })
// })
// })