Chapter 4,6: prettier updates and web framework samples

This commit is contained in:
Beth Griggs
2020-05-31 19:31:42 +01:00
parent b43bf2c378
commit 0d8170c265
43 changed files with 4820 additions and 242 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', (t) => {
t.plan(2)
const fastify = Fastify()
fastify.register(Support)
fastify.ready((err) => {
t.error(err)
t.equal(fastify.someSupport(), 'hugs')
})
})
// If you prefer async/await, use the following
//
// test('support works standalone', async (t) => {
// const fastify = Fastify()
// fastify.register(Support)
//
// await fastify.ready()
// t.equal(fastify.someSupport(), 'hugs')
// })
@@ -0,0 +1,27 @@
'use strict'
const { test } = require('tap')
const { build } = require('../helper')
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')
})
})
// If you prefer async/await, use the following
//
// 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')
// })
@@ -0,0 +1,27 @@
'use strict'
const { test } = require('tap')
const { build } = require('../helper')
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 })
})
})
// If you prefer async/await, use the following
//
// test('default root route', async (t) => {
// const app = build(t)
//
// const res = await app.inject({
// url: '/'
// })
// t.deepEqual(JSON.parse(res.payload), { root: true })
// })