How to avoid TypeErrors?
I'm using jasmine-node to test a REST API. My problem is that when a request errors out or something unexpected happens, I always get Javascript TypeErrors because I'm testing properties on object's that are null and the test process never terminates (it just hangs there after encountering the first TypeError error).
For example:
it('should get resource', function (done) {
client.get('/some/resouce', function (error, res) {
expect(error).toBeFalsy();
expect(res.status).toBe(200);
done();
}
}
The above code will return an error message TypeError: Cannot read property 'status' of undefined if there is an error and jasmine-node just hangs there and never gives me a test report.
I use the following command to run tests: ./node_modules/.bin/jasmine-node test/ --verbose --autotest --captureExceptions --color --watch ./server
Is there any way to avoid those errors?
Oh, I just realised I could do:
it('should get resource', function (done) {
client.get('/some/resouce', function (error, res) {
if (error) return done(error);
expect(res.status).toBe(200);
done();
}
}
This seems to solve my problem for now!