Skip to content

Commit 3a7fa37

Browse files
authored
Revert 00ca06b; closes #3414 (#3715)
- Updates events test to match correct event order - Add test for retries-and-bail case - Split `--invert` test case into its own file
1 parent 21ba5ce commit 3a7fa37

12 files changed

+165
-72
lines changed

lib/runner.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -876,15 +876,11 @@ Runner.prototype.run = function(fn) {
876876
if (self._delay) {
877877
self.emit(constants.EVENT_DELAY_END);
878878
}
879-
Runner.immediately(function() {
880-
self.emit(constants.EVENT_RUN_BEGIN);
881-
});
879+
self.emit(constants.EVENT_RUN_BEGIN);
882880

883881
self.runSuite(rootSuite, function() {
884882
debug('finished running');
885-
Runner.immediately(function() {
886-
self.emit(constants.EVENT_RUN_END);
887-
});
883+
self.emit(constants.EVENT_RUN_END);
888884
});
889885
}
890886

test/assertions.js

+18
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,24 @@ exports.mixinMochaAssertions = function(expect) {
100100
code: expect.it('to be greater than', 0)
101101
});
102102
})
103+
.addAssertion(
104+
'<RawRunResult|RawResult> [not] to have failed (with|having) output <any>',
105+
function(expect, result, output) {
106+
expect(result, '[not] to satisfy', {
107+
code: expect.it('to be greater than', 0),
108+
output: output
109+
});
110+
}
111+
)
112+
.addAssertion(
113+
'<RawRunResult|RawResult> [not] to have passed (with|having) output <any>',
114+
function(expect, result, output) {
115+
expect(result, '[not] to satisfy', {
116+
code: 0,
117+
output: output
118+
});
119+
}
120+
)
103121
.addAssertion('<JSONRunResult> [not] to have test count <number>', function(
104122
expect,
105123
result,

test/integration/events.spec.js

+19
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,23 @@ describe('event order', function() {
7575
});
7676
});
7777
});
78+
79+
describe('--retries and --bail test case', function() {
80+
it('should assert --retries event order', function(done) {
81+
runMochaJSON(
82+
'runner/events-bail-retries.fixture.js',
83+
['--retries', '1', '--bail'],
84+
function(err, res) {
85+
if (err) {
86+
done(err);
87+
return;
88+
}
89+
expect(res, 'to have failed with error', 'error test A')
90+
.and('to have failed test count', 1)
91+
.and('to have passed test count', 0);
92+
done();
93+
}
94+
);
95+
});
96+
});
7897
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
'use strict';
2+
var Runner = require('../../../../lib/runner.js');
3+
var assert = require('assert');
4+
var constants = Runner.constants;
5+
var EVENT_HOOK_BEGIN = constants.EVENT_HOOK_BEGIN;
6+
var EVENT_HOOK_END = constants.EVENT_HOOK_END;
7+
var EVENT_RUN_BEGIN = constants.EVENT_RUN_BEGIN;
8+
var EVENT_RUN_END = constants.EVENT_RUN_END;
9+
var EVENT_SUITE_BEGIN = constants.EVENT_SUITE_BEGIN;
10+
var EVENT_SUITE_END = constants.EVENT_SUITE_END;
11+
var EVENT_TEST_BEGIN = constants.EVENT_TEST_BEGIN;
12+
var EVENT_TEST_END = constants.EVENT_TEST_END;
13+
var EVENT_TEST_FAIL = constants.EVENT_TEST_FAIL;
14+
var EVENT_TEST_RETRY = constants.EVENT_TEST_RETRY;
15+
16+
var emitOrder = [
17+
EVENT_RUN_BEGIN,
18+
EVENT_SUITE_BEGIN,
19+
EVENT_SUITE_BEGIN,
20+
EVENT_HOOK_BEGIN,
21+
EVENT_HOOK_END,
22+
EVENT_TEST_BEGIN,
23+
EVENT_HOOK_BEGIN,
24+
EVENT_HOOK_END,
25+
EVENT_TEST_RETRY,
26+
EVENT_HOOK_BEGIN,
27+
EVENT_HOOK_END,
28+
EVENT_TEST_BEGIN,
29+
EVENT_HOOK_BEGIN,
30+
EVENT_HOOK_END,
31+
EVENT_TEST_FAIL,
32+
EVENT_TEST_END,
33+
EVENT_HOOK_BEGIN,
34+
EVENT_HOOK_END,
35+
EVENT_HOOK_BEGIN,
36+
EVENT_HOOK_END,
37+
EVENT_SUITE_END,
38+
EVENT_SUITE_END,
39+
EVENT_RUN_END
40+
];
41+
42+
var realEmit = Runner.prototype.emit;
43+
Runner.prototype.emit = function(event, ...args) {
44+
// console.log(`emit: ${event}`);
45+
assert.strictEqual(event, emitOrder.shift());
46+
return realEmit.call(this, event, ...args);
47+
};
48+
49+
describe('suite A', function() {
50+
before('before', function() {});
51+
beforeEach('beforeEach', function() {});
52+
it('test A', function() {
53+
throw new Error('error test A');
54+
});
55+
afterEach('afterEach', function() {});
56+
after('after', function() {});
57+
});

test/integration/fixtures/runner/events-bail.fixture.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ var EVENT_TEST_END = constants.EVENT_TEST_END;
1313
var EVENT_TEST_FAIL = constants.EVENT_TEST_FAIL;
1414

1515
var emitOrder = [
16-
EVENT_SUITE_BEGIN, // incorrect order
1716
EVENT_RUN_BEGIN,
1817
EVENT_SUITE_BEGIN,
18+
EVENT_SUITE_BEGIN,
1919
EVENT_HOOK_BEGIN,
2020
EVENT_HOOK_END,
2121
EVENT_TEST_BEGIN,

test/integration/fixtures/runner/events-basic.fixture.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ var EVENT_TEST_PENDING = constants.EVENT_TEST_PENDING;
1818
var EVENT_TEST_RETRY = constants.EVENT_TEST_RETRY;
1919

2020
var emitOrder = [
21-
EVENT_SUITE_BEGIN, // incorrect order
2221
EVENT_RUN_BEGIN,
2322
EVENT_SUITE_BEGIN,
23+
EVENT_SUITE_BEGIN,
2424
EVENT_HOOK_BEGIN,
2525
EVENT_HOOK_END,
2626
EVENT_TEST_BEGIN,

test/integration/fixtures/runner/events-delay.fixture.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ var EVENT_TEST_RETRY = constants.EVENT_TEST_RETRY;
2020
var emitOrder = [
2121
EVENT_DELAY_BEGIN,
2222
EVENT_DELAY_END,
23-
EVENT_SUITE_BEGIN, // incorrect order
2423
EVENT_RUN_BEGIN,
2524
EVENT_SUITE_BEGIN,
25+
EVENT_SUITE_BEGIN,
2626
EVENT_HOOK_BEGIN,
2727
EVENT_HOOK_END,
2828
EVENT_TEST_BEGIN,

test/integration/fixtures/runner/events-retries.fixture.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ var EVENT_TEST_FAIL = constants.EVENT_TEST_FAIL;
1414
var EVENT_TEST_RETRY = constants.EVENT_TEST_RETRY;
1515

1616
var emitOrder = [
17-
EVENT_SUITE_BEGIN, // incorrect order
1817
EVENT_RUN_BEGIN,
1918
EVENT_SUITE_BEGIN,
19+
EVENT_SUITE_BEGIN,
2020
EVENT_HOOK_BEGIN,
2121
EVENT_HOOK_END,
2222
EVENT_TEST_BEGIN,

test/integration/helpers.js

+12-5
Original file line numberDiff line numberDiff line change
@@ -86,22 +86,29 @@ module.exports = {
8686
return invokeMocha(
8787
args,
8888
function(err, res) {
89-
if (err) return fn(err);
89+
if (err) {
90+
return fn(err);
91+
}
9092

93+
var result;
9194
try {
92-
var result = toJSONRunResult(res);
93-
fn(null, result);
95+
// attempt to catch a JSON parsing error *only* here.
96+
// previously, the callback was called within this `try` block,
97+
// which would result in errors thrown from the callback
98+
// getting caught by the `catch` block below.
99+
result = toJSONRunResult(res);
94100
} catch (err) {
95-
fn(
101+
return fn(
96102
new Error(
97103
format(
98-
'Failed to parse JSON reporter output. Error:\n%O\nResponse:\n%O',
104+
'Failed to parse JSON reporter output. Error:\n%O\nResult:\n%O',
99105
err,
100106
res
101107
)
102108
)
103109
);
104110
}
111+
fn(null, result);
105112
},
106113
opts
107114
);

test/integration/options/compilers.spec.js

+12-14
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,18 @@ var helpers = require('../helpers');
44
var invokeMocha = helpers.invokeMocha;
55

66
describe('--compilers', function() {
7-
var args = [];
7+
it('should report deprecation', function(done) {
8+
invokeMocha(
9+
['--compilers', 'coffee:coffee-script/register'],
10+
function(err, res) {
11+
if (err) {
12+
return done(err);
13+
}
814

9-
before(function() {
10-
args = ['--compilers', 'coffee:coffee-script/register'];
11-
});
12-
13-
it('should fail', function(done) {
14-
invokeMocha(args, function(err, res) {
15-
if (err) {
16-
return done(err);
17-
}
18-
19-
expect(res.code, 'to be', 1);
20-
done();
21-
});
15+
expect(res, 'to have failed with output', /compilers is deprecated/i);
16+
done();
17+
},
18+
'pipe'
19+
);
2220
});
2321
});

test/integration/options/grep.spec.js

+15-43
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
11
'use strict';
22

3-
var path = require('path').posix;
43
var helpers = require('../helpers');
54
var runMocha = helpers.runMocha;
65
var runMochaJSON = helpers.runMochaJSON;
76

8-
describe('--grep', function() {
9-
var args = [];
10-
var fixture = path.join('options', 'grep');
11-
12-
afterEach(function() {
13-
args = [];
14-
});
7+
var FIXTURE = 'options/grep';
158

9+
describe('--grep', function() {
1610
it('should run specs matching a string', function(done) {
17-
args = ['--grep', 'match'];
18-
runMochaJSON(fixture, args, function(err, res) {
11+
runMochaJSON(FIXTURE, ['--grep', 'match'], function(err, res) {
1912
if (err) {
2013
return done(err);
2114
}
@@ -27,9 +20,8 @@ describe('--grep', function() {
2720
});
2821

2922
describe('should run specs matching a RegExp', function() {
30-
it('with RegExp-like strings (pattern follow by flag)', function(done) {
31-
args = ['--grep', '/match/i'];
32-
runMochaJSON(fixture, args, function(err, res) {
23+
it('with RegExp-like strings (pattern followed by flag)', function(done) {
24+
runMochaJSON(FIXTURE, ['--grep', '/match/i'], function(err, res) {
3325
if (err) {
3426
return done(err);
3527
}
@@ -41,8 +33,7 @@ describe('--grep', function() {
4133
});
4234

4335
it('with string as pattern', function(done) {
44-
args = ['--grep', '.*'];
45-
runMochaJSON(fixture, args, function(err, res) {
36+
runMochaJSON(FIXTURE, ['--grep', '.*'], function(err, res) {
4637
if (err) {
4738
return done(err);
4839
}
@@ -55,10 +46,9 @@ describe('--grep', function() {
5546
});
5647
});
5748

58-
describe('when --invert used', function() {
49+
describe('when used with --invert', function() {
5950
it('should run specs that do not match the pattern', function(done) {
60-
args = ['--grep', 'fail', '--invert'];
61-
runMochaJSON(fixture, args, function(err, res) {
51+
runMochaJSON(FIXTURE, ['--grep', 'fail', '--invert'], function(err, res) {
6252
if (err) {
6353
return done(err);
6454
}
@@ -68,40 +58,22 @@ describe('--grep', function() {
6858
done();
6959
});
7060
});
61+
});
7162

72-
it('should throw an error when option used in isolation', function(done) {
73-
var spawnOpts = {stdio: 'pipe'};
74-
args = ['--invert'];
63+
describe('when both --fgrep and --grep used together', function() {
64+
it('should report an error', function(done) {
7565
runMocha(
76-
fixture,
77-
args,
66+
FIXTURE,
67+
['--fgrep', 'first', '--grep', 'second'],
7868
function(err, res) {
7969
if (err) {
8070
return done(err);
8171
}
82-
expect(res, 'to satisfy', {
83-
code: 1,
84-
output: /--invert.*--grep <regexp>/
85-
});
72+
expect(res, 'to have failed with output', /mutually exclusive/i);
8673
done();
8774
},
88-
spawnOpts
75+
'pipe'
8976
);
9077
});
9178
});
92-
93-
describe('when both --fgrep and --grep used together', function() {
94-
it('should conflict', function(done) {
95-
// var fixture = 'uncaught.fixture.js';
96-
args = ['--fgrep', 'first', '--grep', 'second'];
97-
98-
runMocha(fixture, args, function(err, res) {
99-
if (err) {
100-
return done(err);
101-
}
102-
expect(res, 'to have failed');
103-
done();
104-
});
105-
});
106-
});
10779
});
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict';
2+
3+
var runMocha = require('../helpers').runMocha;
4+
5+
describe('--invert', function() {
6+
describe('when used without --fgrep or --grep', function() {
7+
it('it should report an error', function(done) {
8+
runMocha(
9+
'options/grep',
10+
['--invert'],
11+
function(err, res) {
12+
if (err) {
13+
return done(err);
14+
}
15+
expect(
16+
res,
17+
'to have failed with output',
18+
/--invert.*--grep <regexp>/
19+
);
20+
done();
21+
},
22+
'pipe'
23+
);
24+
});
25+
});
26+
});

0 commit comments

Comments
 (0)