Skip to content
This repository was archived by the owner on May 15, 2019. It is now read-only.

Testing endpoints with API integration test

Paul C edited this page Jul 18, 2018 · 1 revision

To iterate quickly, we need good testing in place so we don't have to be afraid of breaking things. Here's how we test for new endpoints.

  1. Make sure database seed files (found under /server/src/db/seeds) contains necessary data for all your test. For example, if your endpoint requires a user to be in database for the endpoint to work properly, you add it via:
$> knex seed:make <name>
/** Cats table seed file */

import * as Knex from 'knex'

exports.seed = async (knex: Knex): Promise<any> => {
  // Deletes ALL existing entries
  await knex('community_managers').del()
  // Inserts seed entries
  await knex('community_managers').insert([
    {
      name: 'steve',
      color: 'gold'
    },
    {
      name: 'prism',
      color: 'rainbow'
    },
  ])
}
  1. Create a new .spec.ts file under /server/test/route/.
/** /server/test/route/api/client/resources/cat.spec.ts */

import * as sinon from 'sinon'
import * as chai from 'chai'
import chaiHttp = require('chai-http')
import Knex from '~/db/connection'
import { createUserAuthorizationHeader } from 'test/utils'

import server from '~/index'

chai.use(chaiHttp)
const should = chai.should()

describe('route:api:client:cats', () => {
  let sandbox: sinon.SinonSandbox

  beforeEach(async () => {
    sandbox = sinon.createSandbox()
    await Knex.migrate.rollback()
    await Knex.migrate.latest()
    await Knex.seed.run()
  })
  afterEach(async () => {
    await Knex.migrate.rollback()
    sandbox.restore()
  })

  describe(`GET /api/client/v1/cats`, () => {
    it('should get 200 and return user info', async () => {
      const res = await chai
        .request(server)
        .get(`/api/client/v1/cats`)
        .send()
      
      res.should.have.status(200)
      res.body.data.should.include.key('cat')
    })
  })
})
  1. Run test!
$> yarn test

Clone this wiki locally