knex mocking code example

Example: knex mocking

import 'mocha'
import bcrypt from 'bcrypt'
import chai from 'chai'
import knexMock from 'mock-knex'
import httpMock from 'node-mocks-http'
import db from '../../lib/db'
import { login } from '../../controllers/auth/login'
import { mockingData } from '../mock-data/mock.data'

let req, res, tracker, hashPassword

describe('[Unit Testing] - Server API V1', function () {
	beforeEach(function () {
		// create model mock
		tracker = knexMock.getTracker()
		tracker.install()
		knexMock.mock(db)

		// create express request and response mock
		req = httpMock.createRequest()
		res = httpMock.createResponse()
	})

	afterEach(function () {
		// reset mock after every each test finish
		tracker.uninstall()
		knexMock.unmock(db)
	})

	it('[POST] - /auth/login - Should be login username/password wrong', async function () {
		req.body.email = mockingData.login.active.email
		req.body.password = mockingData.login.active.password

		hashPassword = bcrypt.hashSync(mockingData.login.active.password, 12)
		mockingData.login.active.password = hashPassword

		tracker.on('query', (query) => {
			chai.expect(query.method).to.equal('first')
			query.response(undefined)
		})

		await login(req, res)
		const data = res._getJSONData()
		chai.expect(res._isEndCalled()).to.be.true
		chai.expect(res._getStatusCode()).to.be.equal(403)
		chai.expect(res._getHeaders()).to.be.contain({ 'content-type': 'application/json' })
		chai.expect(data.message).to.be.equal('Invalid email or password.')
	})

	it('[POST] - /auth/login - Should be login username/password is not active', async function () {
		req.body.email = mockingData.login.noActive.email
		req.body.password = mockingData.login.noActive.password

		hashPassword = bcrypt.hashSync(mockingData.login.noActive.password, 12)
		mockingData.login.noActive.password = hashPassword

		tracker.on('query', (query) => {
			chai.expect(query.method).to.equal('first')
			query.response(mockingData.login.noActive)
		})

		await login(req, res)
		const data = res._getJSONData()

		chai.expect(res._isEndCalled()).to.be.true
		chai.expect(res._getStatusCode()).to.be.equal(403)
		chai.expect(res._getHeaders()).to.be.contain({ 'content-type': 'application/json' })
		chai.expect(data.message).to.be.equal('Account is not active')
	})

	it('[POST] - /auth/login - Should be login successfully', async function () {
		req.body.email = mockingData.login.active.email
		req.body.password = mockingData.login.active.password

		hashPassword = bcrypt.hashSync(mockingData.login.active.password, 12)
		mockingData.login.active.password = hashPassword

		tracker.on('query', (query) => {
			chai.expect(query.method).to.equal('first')
			query.response(mockingData.login.active)
		})

		await login(req, res)
		const data = res._getJSONData()
		chai.expect(res._isEndCalled()).to.be.true
		chai.expect(res._getStatusCode()).to.be.equal(200)
		chai.expect(res._getHeaders()).to.be.contain({ 'content-type': 'application/json' })
		chai.expect(data.status).to.be.equal('Ok')
	})
})

Tags:

Misc Example