I made a very simple RESTful API that allows users to add, delete and update information about Hogwarts students. Mongo acts as the persistence layer.
A GET request to url/students
is supposed to return a list of all student objects. While writing the test for it I wrote
expect(res).to.equal('student list');
This is just for an initial check to make sure the test would fail but it isn't instead I get this error:
UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): AssertionError: expected { Object (domain, _events, ...) } to equal 'student list'
So it knows that the two values are different but instead of the test failing it's throwing an error. I've pasted the full test code below.
let chai = require('chai');
let chaiHttp = require('chai-http');
var MongoClient = require('mongodb').MongoClient;
chai.use(chaiHttp);
const expect = chai.expect;
describe('Students', async () => {
describe('/GET students', () => {
it('should GET all the students', async () => {
chai.request('http://localhost:5000')
.get('/students')
.then(function (res) {
try {
expect(res).to.equal('hola');
} catch (err) {
throw err;
}
})
.catch(err => {
throw err; //this gets thrown
})
});
});
});
And if you can show me the syntax of properly using async await for writing these tests please do that too.