Skip to content

Commit

Permalink
Allow to retry failed test from test context for mochajs#1773
Browse files Browse the repository at this point in the history
  • Loading branch information
jegsar committed Oct 26, 2015
1 parent 20ebec8 commit d1e7fb2
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,21 @@ Context.prototype.skip = function() {
return this;
};

/**
* Allow a number of retires on failed tests
*
* @api private
* @param {number} n
* @return {Context} self
*/
Context.prototype.retries = function(n) {
if (!arguments.length) {
return this.runnable().retries();
}
this.runnable().retries(n);
return this;
};

/**
* Inspect the context void of `._runnable`.
*
Expand Down
7 changes: 7 additions & 0 deletions lib/interfaces/bdd.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,12 @@ module.exports = function(suite) {
context.xit = context.xspecify = context.it.skip = function(title) {
context.it(title);
};

/**
* Number of attempts to retry.
*/
context.it.retries = function(n) {
context.retries(n);
};
});
};
9 changes: 9 additions & 0 deletions lib/interfaces/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ module.exports = function(suites, context) {
*/
skip: function(title) {
context.test(title);
},

/**
* Number of retry attempts
*
* @param {string} n
*/
retries: function(n) {
context.retries(n);
}
}
};
Expand Down
1 change: 1 addition & 0 deletions lib/interfaces/qunit.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,6 @@ module.exports = function(suite) {
};

context.test.skip = common.test.skip;
context.test.retries = common.test.retries;
});
};
1 change: 1 addition & 0 deletions lib/interfaces/tdd.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,6 @@ module.exports = function(suite) {
};

context.test.skip = common.test.skip;
context.test.retries = common.test.retries;
});
};
19 changes: 19 additions & 0 deletions lib/runnable.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ function Runnable(title, fn) {
this._enableTimeouts = true;
this.timedOut = false;
this._trace = new Error('done() called multiple times');
this._retries = -1;
}

/**
Expand Down Expand Up @@ -128,6 +129,20 @@ Runnable.prototype.skip = function() {
throw new Pending();
};

/**
* Set number of retires.
*
* @api private
*/
Runnable.prototype.retries = function(n) {
if (!arguments.length) {
return this._retries;
}
if (this._retries === -1){
this._retries = n;
}
};

/**
* Return the full title generated by recursively concatenating the parent's
* full title.
Expand Down Expand Up @@ -230,6 +245,10 @@ Runnable.prototype.run = function(fn) {

// finished
function done(err) {
if (err&&self._retries) {
self._retries--;
return self.run(fn);
}
var ms = self.timeout();
if (self.timedOut) {
return;
Expand Down
23 changes: 23 additions & 0 deletions test/runnable.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ describe('Runnable(title, fn)', function(){
})
})

describe('#retries(n)', function(){
it('should set the number of retries', function(){
var run = new Runnable;
run.retries(1);
run.retries().should.equal(1);
})
})

describe('.run(fn)', function(){
describe('when .pending', function(){
it('should not invoke the callback', function(done){
Expand All @@ -101,6 +109,21 @@ describe('Runnable(title, fn)', function(){
})
})

describe('when ._retries', function(){
it('should retry until out of retries', function(done){
var test = new Runnable('foo', function(){
throw new Error('should invoke the callback');
});

test.retries(2);
test.run(function(err){
test.retries().should.equal(0);
err.message.should.equal('should invoke the callback');
done();
})
})
})

describe('when sync', function(){
describe('without error', function(){
it('should invoke the callback', function(done){
Expand Down

0 comments on commit d1e7fb2

Please sign in to comment.