Reputation: 91
I'm trying to write a test for an node.js application using TDD but I can't figure out how to write a chai expect test for my Products Route get()
function.
// productsController.js
let items = [
{
id: 1,
name: 'Product 1',
description: 'Product1 description',
price: 19.00
}
];
module.exports = {
get(_, res) {
res.json({items});
}
};
I've read the docs a few times but i can't quite seem to figure out how I can test if the response object is expected to contain a key items
where the value is a array
of 'products'
with the schema like the mentioned above.
What I've tried :
// products.test.js
const { get, getById } = require('../../routes/productsController');
const res = {
jsonCalledWith: {},
json(arg) {
this.jsonCalledWith = arg
}
}
describe('Products Route', function() {
describe('get() function', function() {
it('should return an array of products ', function() {
get(req, res);
expect(res.jsonCalledWith).to.be.have.key('items').that.contains.something.like({
id: 1,
name: 'name',
description: 'description',
price: 18.99
});
});
});
});
However, I get this error:
AssertionError: expected { Object (items) } to be an array
Does anyone know how can I write this test successfully?
Upvotes: 1
Views: 8162
Reputation: 3270
I think you are receiving the error because the expected output is an array, to accomplish your tests you need:
it('Check route list all users', (done) => {
api.get('/usuarios')
.set('Accept', 'application/json; charset=utf-8')
.expect(200)
.end((err, res) => {
expect(res.body).to.be.an('array');
expect(res.body.length).to.equal(1);
done();
});
});
This is an example that returns an array as a json response.
Here is the same test for a object User
instance from the route:
it('Check get by id return 200', (done) => {
api.get('/usuarios/1')
.set('Accept', 'application/json; charset=utf-8')
.expect(200)
.end((err, res) =>{
expect(res.body).to.have.property('nome');
expect(res.body.nome).to.equal('abc');
expect(res.body).to.have.property('email');
expect(res.body.email).to.equal('[email protected]');
expect(res.body).to.have.property('criacao');
expect(res.body.criacao).to.not.equal(null);
expect(res.body).to.have.property('atualizado');
expect(res.body.atualizado).to.not.equal(null);
expect(res.body).to.have.property('datanascimento');
expect(res.body.datanascimento).to.not.equal(null);
expect(res.body).to.have.property('username');
expect(res.body.username).to.equal('abcdef');
expect(res.body).to.have.property('statusmsg');
expect(res.body.statusmsg).to.equal('status');
expect(res.body).to.have.property('genero');
expect(res.body.genero).to.equal('M');
expect(res.body).to.have.property('descricao');
expect(res.body.descricao).to.equal('descricao');
done();
});
});
Im my example I use mocha
and chai
and supertest
.
Hope it helps, if you need more clarifications, please let me know.
Upvotes: 2
Reputation: 91
I figured it out!
// products.test.js
const chai = require('chai');
chai.use(require('chai-json-schema'));
const expect = chai.expect;
const { get } = require('../../routes/productsController');
let req = {
body: {},
params: {},
};
const res = {
jsonCalledWith: {},
json(arg) {
this.jsonCalledWith = arg
}
}
let productSchema = {
title: 'productSchema',
type: 'object',
required: ['id', 'name', 'description', 'price'],
properties: {
id: {
type: 'number',
},
name: {
type: 'string'
},
description: {
type: 'string',
},
price: {
type: 'number',
},
}
};
describe('Products Route', function() {
describe('get() function', function() {
it('should return an array of products ', function() {
get(req, res);
expect(res.jsonCalledWith).to.be.have.key('items');
expect(res.jsonCalledWith.items).to.be.an('array');
res.jsonCalledWith.items.forEach(product => expect(product).to.be.jsonSchema(productSchema));
});
});
});
Turns out the plug-in chai-json-schema allows the validation of json objects to meet a predefined schema.
Upvotes: 3