Fastify KnexJS Mock
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.eslintrc
.gitignore
.npmignore
.travis.yml
LICENSE
README.md
index.js
package-lock.json
package.json
test.js

README.md

Fastify KnexJS Mock Plugin

JavaScript Style Guide Build Status

NPM

Installation

npm install fastify-knexjs-mock --save

Usage

fastify.register(require('fastify-knexjs-mock'), options, err => console.error(err))

fastify.get('/', (request, reply) => {
  console.log(fastify.knex) // Knex DB instance
  console.log(fastify.tracker) // Knex DB Mocked tracker
})

Options

KnexJS Mock DB configuration JSON object.

https://github.com/colonyamerican/mock-knex

Test example

const { test } = require('tap')
const { fastify } = require('./app')

fastify.ready(() => {
  fastify.tracker.install()

  test('GET 200 `/users` route', async t => {
    t.plan(2)

    fastify.tracker.on('query', query => query.response([{
      id: 1,
      name: 'Test',
      lastname: 'Test',
      email: 'test@example.com'
    }]))

    try {
      const { statusCode, payload } = await fastify.inject({
        method: 'GET',
        url: '/users'
      })

      t.equal(statusCode, 200)
      t.same(JSON.parse(payload)[0].email, 'test@example.com')
    } catch (err) {
      t.error(err)
    } finally {
      fastify.close(() => t.end())
    }
  })
})

Author

Nicolás Balduzzi

License

Licensed under MIT.