How to deal with thrown errors in async code with Jasmine?

本小妞迷上赌 提交于 2019-12-25 14:25:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!