问题
The following test causes Jasmine (2.3.4, run in browser via Karma) to crash and not run any subsequent tests
it('should report as failure and continue testing', function (done) {
setTimeout(function () {
throw new SyntaxError('some error');
done();
}, 1000);
});
How can I have this test correctly report itself as a failure and carry on with subsequent tests?
回答1:
Mocking the clock will give you the expected result. Mocking the clock in general is a best practice for testing timeouts.
describe('foo', function () {
beforeEach(function () {
timerCallback = jasmine.createSpy("timerCallback");
jasmine.clock().install();
});
afterEach(function () {
jasmine.clock().uninstall();
});
it('should report as failure and continue testing', function (done) {
setTimeout(function () {
throw new SyntaxError('some error');
done();
}, 1000);
jasmine.clock().tick(1001);
});
});
来源:https://stackoverflow.com/questions/31252888/how-to-deal-with-thrown-errors-in-async-code-with-jasmine