异步错误后的jasmine清理

时间:2015-06-30 18:36:24

标签: jasmine

我正在使用selenium,它做了很多异步工作。如果抛出任何异步错误,Jasmine会立即退出并且不会运行相应的afterEach / afterAll。这是一个模拟示例,显示正确处理同步错误并继续执行。异步错误会停止所有执行。

var database = [];

describe('Some Feature', function () {

    beforeEach(function () {
        console.log('test data buildup');
        database.push({}, {}, {})
    });

    afterEach(function () {
        console.log('test data teardown');
        database.splice(0, database.length);
    });

    afterAll(function () {
        console.log('final test data cleanup');
        database.splice(0, database.length);
    });

    it('does something', function () {
        expect(database[1]).toBeDefined();
    });

    it('handles sync errors', function () {
        throw Error('sync error');
    });

    it('breaks on async errors', function (done) {
        setTimeout(function () {
            throw Error('async error');
            done();
        }, 1000);
    });

});


Some Feature
  does something ...
    test data buildup
    test data teardown
    Passed
  handles sync errors ...
    test data buildup
    test data teardown
    Failed
    Error: sync error
  breaks on async errors ...
    test data buildup

Error: async error
    at Error (<anonymous>)
    at null._onTimeout (spec.js:30:23)
    at Timer.listOnTimeout [as ontimeout] (timers.js:112:15)

>

请注意,test data buildup没有test data teardown,而final test data cleanup从未被调用过。

有没有办法处理异步错误,或者确保我可以在异步失败后清理测试数据/ selenium实例?

0 个答案:

没有答案