如何在 Jasmine 的 afterEach 中获取测试结果
how to get the result of a test in the afterEach in Jasmine
我正在写一个全局的 afterEach 到 console.log 一些仅当测试失败时的日志记录。
jasmine.getEnv().topSuite().afterEach({
fn: function () {
if(\*test failed */){
console.log('print some logs');
}
}
});
我搜索了一下,找到了这个答案
但是根据这里的 Jasmine 文档,这个不正确
https://jasmine.github.io/2.1/custom_reporter.html
specDone is invoked when an it and its associated beforeEach and
afterEach functions have been run.
所以基本上我在 protractor.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.failedExpectations 总是 [] 因为结果尚未更新。
实际上我刚刚发现我的解决方案有效,currentSpec.failedExpectations returns [] 当测试通过但当测试失败时我实际上得到了一个很大的 json 响应失败消息和一切。
我会把 question/answer 放在这里,可能对其他人有帮助。
我正在写一个全局的 afterEach 到 console.log 一些仅当测试失败时的日志记录。
jasmine.getEnv().topSuite().afterEach({
fn: function () {
if(\*test failed */){
console.log('print some logs');
}
}
});
我搜索了一下,找到了这个答案
但是根据这里的 Jasmine 文档,这个不正确 https://jasmine.github.io/2.1/custom_reporter.html
specDone is invoked when an it and its associated beforeEach and afterEach functions have been run.
所以基本上我在 protractor.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.failedExpectations 总是 [] 因为结果尚未更新。
实际上我刚刚发现我的解决方案有效,currentSpec.failedExpectations returns [] 当测试通过但当测试失败时我实际上得到了一个很大的 json 响应失败消息和一切。
我会把 question/answer 放在这里,可能对其他人有帮助。