Peu importe ce que mon serveur retourne réellement, Chai me donne toujours cette exception lorsque j'affirme la réponse.
Uncaught AssertionError: {} devrait égaler profondément 'test'
Même si la réponse réelle du serveur est "test", pas {}:
Voici mon test:
const chai = require('chai');
const chaiHttp = require('chai-http');
const server = require('./test-server');
const should = chai.should();
chai.use(chaiHttp);
describe('GET /test', () => {
it('it should give test result', (done) => {
chai.request(server)
.get('/test')
.end((err, res) => {
console.log(err); // outputs null
console.log(res); // outputs normal-looking response
res.body.should.be.eql('test');
done();
});
});
});
Voici mon serveur (test-server.js):
const http = require('http');
const server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.end('test');
});
module.exports = server;
server.listen(process.env.PORT || 8000);
console.log("Server running at http://localhost:8000/");
Que fais-je de mal?
application/json
res.body
est renseigné en fonction de l'en-tête Content-Type
test-server.js
const http = require('http');
const server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "application/json"});
var b = JSON.stringify({
name: 'asad',
class: 'paewe'
});
response.end(b);
});
module.exports = server;
server.listen(process.env.PORT || 8000);
console.log("Server running at http://localhost:8000/");
Le res.body
contiendra l'objet analysé
test.js
const chai = require('chai');
const chaiHttp = require('chai-http');
const server = require('./test-server');
const should = chai.should();
chai.use(chaiHttp);
describe('GET /test', () => {
it('it should give test result', (done) => {
chai.request(server)
.get('/test')
.end((err, res) => {
console.log(err); // outputs null
console.log(res); // outputs normal-looking response
console.log(res.body) // { name: 'asad', class: 'paewe' }
var checkObj = {
name: 'asad',
class: 'paewe'
}
res.body.should.be.eql(checkObj); // passes test
done();
});
});
});
text/plain
Si l'en-tête Content-Type est text/plain
, le corps de la réponse ne sera pas analysé comme quoi que ce soit, mais res.text
contiendra les données sous forme de chaîne
test-server.js
const http = require('http');
const server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.end('test');
});
module.exports = server;
server.listen(process.env.PORT || 8000);
console.log("Server running at http://localhost:8000/");
test.js
const chai = require('chai');
const chaiHttp = require('chai-http');
const server = require('./test-server');
const should = chai.should();
chai.use(chaiHttp);
describe('GET /test', () => {
it('it should give test result', (done) => {
chai.request(server)
.get('/test')
.end((err, res) => {
console.log(err); // outputs null
console.log(res); // outputs normal-looking response
console.log(res.body) // {}
res.text.should.be.eql('test'); // passes test
done();
});
});
});
Quelques références