Testing

Testing is one of the most important parts of developing an application. Fastify is very flexible when it comes to testing and is compatible with most testing frameworks (such as Tap, which is used in the examples below).

Testing with http injection

Fastify comes with built-in support for fake http injection thanks to light-my-request.

To inject a fake http request, use the inject method:

  1. fastify.inject({
  2. method: String,
  3. url: String,
  4. payload: Object,
  5. headers: Object
  6. }, (error, response) => {
  7. // your tests
  8. })

or in the promisified version

  1. fastify
  2. .inject({
  3. method: String,
  4. url: String,
  5. payload: Object,
  6. headers: Object
  7. })
  8. .then(response => {
  9. // your tests
  10. })
  11. .catch(err => {
  12. // handle error
  13. })

Async await is supported as well!

  1. try {
  2. const res = await fastify.inject({ method: String, url: String, payload: Object, headers: Object })
  3. // your tests
  4. } catch (err) {
  5. // handle error
  6. }

Example:

app.js

  1. const Fastify = require('fastify')
  2. function buildFastify () {
  3. const fastify = Fastify()
  4. fastify.get('/', function (request, reply) {
  5. reply.send({ hello: 'world' })
  6. })
  7. return fastify
  8. }
  9. module.exports = buildFastify

test.js

  1. const tap = require('tap')
  2. const buildFastify = require('./app')
  3. tap.test('GET `/` route', t => {
  4. t.plan(4)
  5. const fastify = buildFastify()
  6. // At the end of your tests it is highly recommended to call `.close()`
  7. // to ensure that all connections to external services get closed.
  8. t.tearDown(() => fastify.close())
  9. fastify.inject({
  10. method: 'GET',
  11. url: '/'
  12. }, (err, response) => {
  13. t.error(err)
  14. t.strictEqual(response.statusCode, 200)
  15. t.strictEqual(response.headers['content-type'], 'application/json; charset=utf-8')
  16. t.deepEqual(JSON.parse(response.payload), { hello: 'world' })
  17. })
  18. })

Testing with a running server

Fastify can also be tested after starting the server with fastify.listen() or after initializing routes and plugins with fastify.ready().

Example:

Uses app.js from the previous example.

test-listen.js (testing with Request)

  1. const tap = require('tap')
  2. const request = require('request')
  3. const buildFastify = require('./app')
  4. tap.test('GET `/` route', t => {
  5. t.plan(5)
  6. const fastify = buildFastify()
  7. t.tearDown(() => fastify.close())
  8. fastify.listen(0, (err) => {
  9. t.error(err)
  10. request({
  11. method: 'GET',
  12. url: 'http://localhost:' + fastify.server.address().port
  13. }, (err, response, body) => {
  14. t.error(err)
  15. t.strictEqual(response.statusCode, 200)
  16. t.strictEqual(response.headers['content-type'], 'application/json; charset=utf-8')
  17. t.deepEqual(JSON.parse(body), { hello: 'world' })
  18. })
  19. })
  20. })

test-ready.js (testing with SuperTest)

  1. const tap = require('tap')
  2. const supertest = require('supertest')
  3. const buildFastify = require('./app')
  4. tap.test('GET `/` route', async (t) => {
  5. const fastify = buildFastify()
  6. t.tearDown(() => fastify.close())
  7. await fastify.ready()
  8. const response = await supertest(fastify.server)
  9. .get('/')
  10. .expect(200)
  11. .expect('Content-Type', 'application/json; charset=utf-8')
  12. t.deepEqual(response.body, { hello: 'world' })
  13. })

How to inspect tap tests

  • Isolate your test by passing the {only: true} option
  1. test('should ...', {only: true}, t => ...)
  • Run tap using npx
  1. > npx tap -O -T --node-arg=--inspect-brk test/<test-file.test.js>
  • -O specifies to run tests with the only option enabled
  • -T specifies not to timeout (while you're debugging)
  • —node-arg=—inspect-brk will launch the node debugger
  • In VS Code, create and launch a Node.js: Attach debug configuration. No modification should be necessary.
    Now you should be able to step through your test file (and the rest of fastify) in your code editor.

原文: https://www.fastify.io/docs/v2.0.x/Testing