jbrumwell / mock-knex

A mock knex adapter for simulating a database during testing
MIT License
239 stars 71 forks source link

Not getting response from tracker with bookshelf and mock-knex #62

Closed dagda1 closed 7 years ago

dagda1 commented 7 years ago

I am not getting the response from tracker with mock-knex:

I initalise mock-knex in my bookshelf.js file like this:

const config = require('./config/config').config;
const bcrypt = require('bookshelf-bcrypt');

const knex = require('knex');

const connection = knex(config.database);

if(process.env.NODE_ENV === 'test') {
  const mockKnex = require('mock-knex');
  mockKnex.mock(connection, 'knex@0.11');
}

const bookshelf = require('bookshelf')(connection);
const ModelBase = require('bookshelf-modelbase')(bookshelf);

bookshelf.plugin(['registry', 'bookshelf-camelcase']);
bookshelf.plugin(bcrypt);

module.exports.Bookshelf = bookshelf;

module.exports.ModelBase = ModelBase;

And the config.js looks like this:


const env = process.env.NODE_ENV || 'development';
const config = {};

switch(env) {
  case 'development':
    config.database = {
      client: 'postgresql',
      connection: {
        host: '127.0.0.1',
        port: 5432,
        user: 'paulcowan',
        database: 'managers',
        charset: 'utf8'
      },
      migrations: {
        tableName: 'knex_migrations'
      }
    }
  case 'test':
    config.database = {
      dialect: 'sqlite3',
      connection: {
        filename: ':memory:'
      },
      createTableIfNotExists: true,
      useNullAsDefault: true
    };
};

config.token = 'secret-jwt-token'
config.site = {
  port: 5000
};

module.exports.config = config;

I then have the following test that creates the tracker:

'use strict';

import should from 'should';
import app from '../../server';
import supertest from 'supertest-as-promised';

import mockKnex from 'mock-knex';

const tracker = mockKnex.getTracker();

tracker.install();

const request = supertest.agent(app.listen());

context('Admin', () => {
  describe('POST /admin', () => {
    before(() => {
      tracker.on('query', (query, step) => {
        query.response({
          id: 1,
          username: 'admin',
          name: 'Paul Cowan',
          password: 'password'
        });
      });
    });

    it('should log a user in', async () => {
      await request
        .post('/auth/')
        .send({username: 'admin', password: 'password'})
        .expect(201)
        .then((err, res) => {
          console.dir(res.body);
        });
    });
  });
});

I am using User.findOne in a passport local method below but if I log the result:


passport.use('local', new Strategy({
  usernameField: 'username',
  passwordField: 'password'
}, async (username, password, done) => {
  try {
    const user = await User.findOne({
      username
    });

    console.dir(user);

    if (!user) {
      return done(null, false)
    }

    try {
      const isMatch = await user.validatePassword(password);

      if (!isMatch) {
        return done(null, false)
      }

      done(null, user)
    } catch (err) {
      done(err)
    }

  } catch (err) {
    return done(err)
  }
}));

It comes back with this:

ModelBase {
  attributes: { username: 'admin' },
  _previousAttributes: { username: 'admin' },
  changed: {},
  relations: {},
  cid: 'c2',
  _knex: null }

So the attributes only has the username. I am confused as to how to properly set this up.