I'm getting the following error when unit testing for a single item using knex. I'm also referencing this https://mherman.org/blog/test-driven-development-with-node/
It renders api/users/1 successfully. So not sure why it says null.
GET /api/users/:id Should return a single show : Uncaught TypeError: Cannot read property 'should' of null at should (tests/users.spec.js:59:26)
users.spec.js
import chai from 'chai';
import { expect } from 'chai';
import chaiHttp from 'chai-http';
import { assert } from 'assert'
import users from '../routes/users';
import request from 'supertest';
describe('GET /api/users/:id ', () =>{
it('Should return a single show ', (done)=> {
chai.request(users)
.get('/users/1')
.set('Accept', 'application/json')
.end((response) => {
// error begins here
response.should.have.status(200);
response.should.be.json;
response.body.should.be.a('object');
response.body.should.have.property('id');
response.body.name.should.equal(1);
done()
})
});
})
routes/users.js
users.get('/users/:id', (req, res) => {
return knex('users').where({id: req.params.id})
.then( (user) => {
res.json({
user: user
})
});
})
You're importing expect
from chai
instead of should
. You can either change:
response.should.have.status(200);
to expect(response).to.have.status(200);
(do this for all your should statements)
or import should by changing:
import { expect } from 'chai';
to import { should } from 'chai';
Hope that helps!