问题
i'm using such code in order to stop the entire describe
upon an error in one of the it()
functions:
if(this.results_.failedCount > 0) {
//next step will finish the test
jasmine.Queue.prototype.next_ = function () {
// to instead skip to the end
this.onComplete();
}
}
i got it from: How can I make jasmine.js stop after a test failure?
however, if i have few describe blocks in the file, and the first describe fails, i would like it to continue to the next describe and not kill the entire test. how can i do it?
thanks
回答1:
Asher, I've used your code from the workaround inside afterEach and it seems to work. Thanks :) In case you want the other specs to fail (and not just skipped) add the following code inside the if:
if (currentIt && (currentIt.results_.failedCount !== 0)) {
var currentSuite = jasmine.getEnv().currentSpec.suite.id;
var currentSpec = jasmine.getEnv().currentSpec.id;
//get number of children (its)
var numChildren = jasmine.getEnv().currentSpec.suite.children().length;
//skip after last child
jasmine.getEnv().currentRunner().suites()[currentSuite].queue.index = numChildren-1;
//finish the current spec now (otherwise the spec will be reported after his siblings)
jasmine.getEnv().currentSpec.finish();
jasmine.getEnv().currentSpec.finishCallback = function(){};
//report the other sibilings as failures
for (var index = currentSpec+1; index < numChildren ; index++){
var spec = jasmine.getEnv().currentSpec.suite.children()[index];
jasmine.getEnv().currentSpec = spec;
(new AddIndexPlugin()).doBefore();
spec.results().failedCount = 1;
spec.results().totalCount = 1;
spec.results().skipped = true;
spec.results().getItems().push(new jasmine.ExpectationResult({actual:'skipping test because previous test failed',passed:false , message:'skipping test because previous test failed',trace:'skipping test because previous test failed'}));
spec.finish();
}
Now Jasmine is really a E2E testing framework and not just unit test framework.
回答2:
I've managed to come up with a simple workaround.
if(this.results_.failedCount > 0) {
//get suite id:
var currentSuite = jasmine.currentEnv_.currentSpec.suite.id;
//get number of children (its)
var numChildren = jasmine.currentEnv_.currentSpec.suite.children_.length;
//skip to last child
jasmine.currentEnv_.currentRunner_.suites_[currentSuite].queue.index = numChildren - 1;
}
if the test fails, i get the suite id, finds how many children it has (how many it
s) and then change the index in the runner's queue. it basically skips the rest of the it
s in the current describe and moves on to the next describe block.
来源:https://stackoverflow.com/questions/20858717/how-to-skip-to-next-describe-upon-error-in-previous-describe-jasmine-test