如何在Jasmine的afterEach中获得测试结果



我正在编写一个全局afterEach到console.log,只有在测试失败时才会记录一些日志。

jasmine.getEnv().topSuite().afterEach({
fn: function () {
if(*test failed */){
console.log('print some logs');
}
}
});

我搜索了一下,发现了这个答案

https://stackoverflow.com/a/39882823/4324176

但根据Jasmine的文档,这个是不正确的https://jasmine.github.io/2.1/custom_reporter.html

当it及其关联的beforeEach和afterEach函数已运行。

所以基本上我在dractor.conf.js onPrepare((中的代码是

jasmine.getEnv().addReporter({
specStarted(result) {
jasmine.getEnv().currentSpec = result;
},
specDone() {
jasmine.getEnv().currentSpec = null;
}
});
jasmine.getEnv().topSuite().afterEach({
fn: function () {
const currentSpec = jasmine.getEnv().currentSpec;
if(currentSpec.failedExpectations.length){
console.log('print some logs');
}
}
});

但问题是currentSpec.failedExperts总是[],因为结果尚未更新。

事实上,我刚刚发现我的解决方案正在运行,currentSpec.failedExperts在测试通过时返回[],但当测试失败时,我实际上得到了一个带有失败消息和所有内容的大json响应。

我会把问题/答案放在这里,也许对其他人有帮助。

最新更新