实习生功能测试失败后如何清理?

How to clean up after failed Intern functional tests?

我有一些 运行 使用 Intern (3) 的功能测试,测试的最后一步是做一些清理,包括清除远程浏览器上的 localStorage,然后切换回原来的 window 通过立即存储 window 句柄并在测试结束前切换回它(因此任何后续测试都不会失败,因为它们试图 运行 在关闭的 window 如果之前的测试结束于不同的测试)。但是,如果某些 chaijs 断言在 .then() 中失败,最后的清理代码将被跳过。有没有更好的方法来清理即使某些断言失败仍会 运行 的功能测试?

this.leadfoot.remote
    .get(require.toUrl(url))
    .execute(function() { return document.querySelector('#welcome').textContent})
    .then(welcome => {
        assert.equal(welcome, 'hello world', 'properly greeted');
    })
    .execute(function() {
        localStorage.clear();
    });

如果断言失败,它永远不会在最后清除 localStorage,如果下一次测试 运行s 期望 localStorage 为空,它也会失败。功能测试后有没有更好的清理方法?

使用afterEach方法。

afterEach() {
    return this.remote
        .execute(function () {
            localStorage.clear();
        });
},

'my test'() {
    return this.remote
        .get(...)
        .execute(...)
        .then(welcome => {
            assert.equal(welcome, ...);
        });
}

在我们的项目中,我们是这样做的:

afterEach: function() {
        var command = this.remote;
        if(intern.variables.currentCase && !intern.variables.currentCase.hasPassed) {
            command = command.execute(function () {
               localStorage.clear();
            });
        }
        return command;
    },

和本地Storage.clear()只有在测试失败时才会执行: