Skip to content

Instantly share code, notes, and snippets.

@christopherhill christopherhill/controller.ts Secret
Created Mar 11, 2018

Embed
What would you like to do?
Sinon and Knex Issues
query = knex(tableName).modify(envelope, queryString);
// envelope conditionally calls .where, .select, etc. chainable methods on Knex to limit query based on query string
query.then(
(data) => responseMap.getAll(req, res, data),
(err) => responseMap.error(req, res, data)
);
getKnex = (tenantId) => {
if (!this.connections[tenantId]) {
logger.error('could not find database connection');
} else {
return this.connections[tenantId].knex; // an existing knex instance
}
}
it('should return a valid response', (done) => {
const knex: any = sinon.stub();
// these are the chainable methods that need to return a copy of the knex instance
knex.column = sinon.stub().returns(knex);
knex.from = sinon.stub().returns(knex);
knex.where = sinon.stub().returns(knex);
knex.select = sinon.stub().returns(knex);
knex.where = sinon.stub().returns(knex);
knex.modify = sinon.stub().returns(knex);
knex.onFirstCall().resolves(authMock.authorization); // this is for an authorization call (that comes ahead of the controller)
knex.onSecondCall().resolves(controllerMock.getAll);
const getKnex = sinon.stub(db, 'getKnex').returns(knex);
chai.request(app)
.get('/api/v1/route')
.set('Authorization', authToken)
.end((err, res) => {
// query.restore();
console.log('data: ', res.body.data[0]);
chai.expect(res.status).to.equal(200);
chai.expect(res.body.success).to.equal(true);
chai.expect(res.body.data).to.be.an('array');
done();
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.