Vasu Rangpariya
Vasu Rangpariya

Reputation: 33

How to write effective test cases with fastify using tap?

I tried to writing test cases with fastify using Tap npm package. But I have some question about my implementation.Here is the my test case file code.

server.test.js will look like


const tap = require('tap')
const mongoose = require('mongoose')
const buildServer = require('../server')

tap.test('requests the `/health-check` route', async function (t) {
  const fastify = buildServer()

  t.teardown(() => {
    fastify.close()
    
  })

  const response = await fastify.inject({
    method: 'GET',
    url: 'health-check'
  })
  t.equal(200, response.statusCode)
})

When I write test cases for different modules available in my app I will create new test files according to the module. Let's say module A. here is the code for module A test case.


const { test } = require('tap')
const { faker } = require('@faker-js/faker')
const buildServer = require('../../../server')
const mongoose = require('mongoose')
const { redisClient } = require('../../../plugins/redis')


test('It will test artist module', async (t) => {
  const fastify = buildServer()
  t.teardown(() => {
    fastify.close()
    redisClient.quit()
    mongoose.connection.close()
  })


  t.test('It should return data by Id', async () => {
    const response = await fastify.inject({
      method: 'GET',
      url: '/api/v1/artist/',
      query: {
        id: "1"
      }
    })
    const payload = response.json()
    console.log('payload', payload)

    t.equal(200, response.statusCode)
  })
})


My concern is I need to buildServer server in every test case file and It will eventually create db connection.So is this good way to buildServer in every test file? And how to avoid duplicate imports between those test files? and one more issue I facing is I need to manually close db connection in every test case.

It it the correct way that I am trying to implement test cases ? if not then please provide some example with tap using fastify

Upvotes: 1

Views: 542

Answers (1)

Melroy van den Berg
Melroy van den Berg

Reputation: 3166

Personally I'm using Jest, but it should be the same idea for tap.

I use beforeAll() with Jest so my Fastify Instance is created once for these tests within the same file. Tap should have a before() method and should do the same thing: t.before(async () => {})

My example in Jest & TS using ESM:

import { FastifyInstance } from 'fastify'
import { Server } from '../src/server.js'

describe('Test my API calls', () => {
  let app: FastifyInstance

  beforeAll(async () => {
    // I have static bootstrap method which creates the Fastify() and register the plugins etc.
    const server = await Server.bootstrap()
    // app variable is my FastifyInstance object
    app = server.app
  })

  afterAll(async () => {
    // Don't forget to call close again on your Fastify Instance!
    app.close()
  })


  test('retrieve all users', async () => {
    const response = await app.inject({
      method: 'GET',
      url: '/api/v1/users'
    })
  })
})

However, if this is not what you want, then consider extending tap: https://github.com/PacktPublishing/Accelerating-Server-Side-Development-with-Fastify/blob/main/Chapter%209/test/helper.js#L24-L31

Again also don't forget to close the app in your teardown

Upvotes: 0

Related Questions