Skip to content

Commit a995e33

Browse files
authored
Fix: programmatic API cannot access retried test objects (#4181)
1 parent ac12f2c commit a995e33

File tree

5 files changed

+42
-12
lines changed

5 files changed

+42
-12
lines changed

lib/runner.js

+5
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,11 @@ function Runner(suite, delay) {
135135
this.total = suite.total();
136136
this.failures = 0;
137137
this.on(constants.EVENT_TEST_END, function(test) {
138+
if (test.retriedTest() && test.parent) {
139+
var idx =
140+
test.parent.tests && test.parent.tests.indexOf(test.retriedTest());
141+
if (idx > -1) test.parent.tests[idx] = test;
142+
}
138143
self.checkGlobals(test);
139144
});
140145
this.on(constants.EVENT_HOOK_END, function(hook) {

lib/test.js

+13
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,26 @@ function Test(title, fn) {
3636
*/
3737
utils.inherits(Test, Runnable);
3838

39+
/**
40+
* Set or get retried test
41+
*
42+
* @private
43+
*/
44+
Test.prototype.retriedTest = function(n) {
45+
if (!arguments.length) {
46+
return this._retriedTest;
47+
}
48+
this._retriedTest = n;
49+
};
50+
3951
Test.prototype.clone = function() {
4052
var test = new Test(this.title, this.fn);
4153
test.timeout(this.timeout());
4254
test.slow(this.slow());
4355
test.enableTimeouts(this.enableTimeouts());
4456
test.retries(this.retries());
4557
test.currentRetry(this.currentRetry());
58+
test.retriedTest(this.retriedTest() || this);
4659
test.globals(this.globals());
4760
test.parent = this.parent;
4861
test.file = this.file;
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
'use strict';
2+
const assert = require('assert');
23

34
describe('retries', function () {
45
this.retries(1);
56
var times = 0;
7+
var self = this;
68

79
it('should pass after 1 retry', function () {
810
times++;
911
if (times !== 2) {
1012
throw new Error('retry error ' + times);
1113
}
1214
});
15+
16+
it('check for updated `suite.tests`', function() {
17+
assert.equal(self.tests[0]._currentRetry, 1);
18+
assert.ok(self.tests[0]._retriedTest);
19+
assert.equal(self.tests[0].state, 'passed');
20+
})
1321
});

test/integration/retries.spec.js

+10-12
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
var assert = require('assert');
44
var helpers = require('./helpers');
5+
var runJSON = helpers.runMochaJSON;
56
var args = [];
67
var bang = require('../../lib/reporters/base').symbols.bang;
78

@@ -59,25 +60,22 @@ describe('retries', function() {
5960
});
6061

6162
it('should exit early if test passes', function(done) {
62-
helpers.runMochaJSON('retries/early-pass.fixture.js', args, function(
63-
err,
64-
res
65-
) {
63+
runJSON('retries/early-pass.fixture.js', args, function(err, res) {
6664
if (err) {
67-
done(err);
68-
return;
65+
return done(err);
6966
}
70-
assert.strictEqual(res.stats.passes, 1);
71-
assert.strictEqual(res.stats.failures, 0);
72-
assert.strictEqual(res.tests[0].currentRetry, 1);
73-
assert.strictEqual(res.stats.tests, 1);
74-
assert.strictEqual(res.code, 0);
67+
68+
expect(res, 'to have passed')
69+
.and('to have passed test count', 2)
70+
.and('to have failed test count', 0)
71+
.and('to have retried test', 'should pass after 1 retry', 1);
72+
7573
done();
7674
});
7775
});
7876

7977
it('should let test override', function(done) {
80-
helpers.runMochaJSON('retries/nested.fixture.js', args, function(err, res) {
78+
runJSON('retries/nested.fixture.js', args, function(err, res) {
8179
if (err) {
8280
done(err);
8381
return;

test/unit/test.spec.js

+6
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ describe('Test', function() {
4141
expect(this._test.clone().currentRetry(), 'to be', 1);
4242
});
4343

44+
it('should add/keep the retriedTest value', function() {
45+
var clone1 = this._test.clone();
46+
expect(clone1.retriedTest(), 'to be', this._test);
47+
expect(clone1.clone().retriedTest(), 'to be', this._test);
48+
});
49+
4450
it('should copy the globals value', function() {
4551
expect(this._test.clone().globals(), 'not to be empty');
4652
});

0 commit comments

Comments
 (0)