I'm making an app in express.js that returns some views, with the filtered user data.
the route I have in express is this:
router.get('/user/name/:name', (req, res) => {
return serverUtil.getUserByName(req.params.name).then(success =>{
res.render('partials/users/user', {user: success});
}).catch(err =>{
res.render('partials/users/user', {error: err});
});
});
As you can see above, I'm sending a res.render
so I don't know how to test this in my Mocha test.
I currently have it like this:
it('Should post to user/name/:name', function(done){
chai.request('http://localhost:5050')
.get('/user/name/Britney')
.end((err, res) => {
console.log("res ", res.body); // Me llega vacío
exp(res).to.be.a('object');
done();
});
});
instead if in my route I change it to this:
router.get('/user/name/:name', (req, res) => {
return serverUtil.getUserByName(req.params.name).then(success =>{
res.send({user: success});
}).catch(err =>{
res.render('partials/users/user', {error: err});
});
});
If I have the data in the test, why am I returning data.
How can I test the route with the res.render and obviously the data that I am passing in user?
update :
I tried to use cheerio, but for some reason in the test I get res.body
empty.
I finally came up with the solution. For this it is necessary to use cheerio, and in my case I was looking in
res.body
but the HTML is inres.text
.So the code looks like this: