Skip to content

Commit 3a2dae8

Browse files
committed
helpful error when necessary suite callback omitted; closes #1744
- works for QUnit as well - renamed some files - fixed unclear "only" tests
1 parent e22c407 commit 3a2dae8

File tree

8 files changed

+81
-55
lines changed

8 files changed

+81
-55
lines changed

Diff for: lib/interfaces/common.js

+2
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ module.exports = function(suites, context, mocha) {
113113
if (typeof opts.fn === 'function') {
114114
opts.fn.call(suite);
115115
suites.shift();
116+
} else if (typeof opts.fn === 'undefined' && !suite.pending) {
117+
throw new Error('Suite "' + suite.fullTitle() + '" was defined but no callback was supplied. Supply a callback or explicitly skip the suite.');
116118
}
117119

118120
return suite;

Diff for: lib/interfaces/qunit.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ module.exports = function(suite) {
5050
}
5151
return common.suite.create({
5252
title: title,
53-
file: file
53+
file: file,
54+
fn: false
5455
});
5556
};
5657

@@ -64,7 +65,8 @@ module.exports = function(suite) {
6465
}
6566
return common.suite.only({
6667
title: title,
67-
file: file
68+
file: file,
69+
fn: false
6870
});
6971
};
7072

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
xdescribe('a pending suite with a callback', function () {});

Diff for: test/integration/only.spec.js

+32-24
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,44 @@ var run = require('./helpers').runMochaJSON;
22
var assert = require('assert');
33

44
describe('.only()', function() {
5-
it('should run only tests that marked as `only`', function(done) {
6-
run('options/only/bdd.fixture.js', ['--ui', 'bdd'], function(err, res) {
7-
assert(!err);
8-
assert.equal(res.stats.pending, 0);
9-
assert.equal(res.stats.passes, 11);
10-
assert.equal(res.stats.failures, 0);
11-
assert.equal(res.code, 0);
12-
done();
5+
describe('bdd', function() {
6+
it('should run only tests that marked as `only`', function(done) {
7+
run('options/only/bdd.fixture.js', ['--ui', 'bdd'], function(err, res) {
8+
assert(!err);
9+
assert.equal(res.stats.pending, 0);
10+
assert.equal(res.stats.passes, 11);
11+
assert.equal(res.stats.failures, 0);
12+
assert.equal(res.code, 0);
13+
done();
14+
});
1315
});
1416
});
1517

16-
it('should run only tests that marked as `only`', function(done) {
17-
run('options/only/tdd.fixture.js', ['--ui', 'tdd'], function(err, res) {
18-
assert(!err);
19-
assert.equal(res.stats.pending, 0);
20-
assert.equal(res.stats.passes, 8);
21-
assert.equal(res.stats.failures, 0);
22-
assert.equal(res.code, 0);
23-
done();
18+
describe('tdd', function() {
19+
it('should run only tests that marked as `only`', function(done) {
20+
run('options/only/tdd.fixture.js', ['--ui', 'tdd'], function(err, res) {
21+
assert(!err);
22+
assert.equal(res.stats.pending, 0);
23+
assert.equal(res.stats.passes, 8);
24+
assert.equal(res.stats.failures, 0);
25+
assert.equal(res.code, 0);
26+
done();
27+
});
2428
});
2529
});
2630

27-
it('should run only tests that marked as `only`', function(done) {
28-
run('options/only/qunit.fixture.js', ['--ui', 'qunit'], function(err, res) {
29-
assert(!err);
30-
assert.equal(res.stats.pending, 0);
31-
assert.equal(res.stats.passes, 5);
32-
assert.equal(res.stats.failures, 0);
33-
assert.equal(res.code, 0);
34-
done();
31+
describe('qunit', function() {
32+
it('should run only tests that marked as `only`', function(done) {
33+
run('options/only/qunit.fixture.js', ['--ui', 'qunit'], function(err, res) {
34+
console.log(err);
35+
36+
assert(!err);
37+
assert.equal(res.stats.pending, 0);
38+
assert.equal(res.stats.passes, 5);
39+
assert.equal(res.stats.failures, 0);
40+
assert.equal(res.code, 0);
41+
done();
42+
});
3543
});
3644
});
3745
});

Diff for: test/integration/suite.js

-29
This file was deleted.

Diff for: test/integration/suite.spec.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
var assert = require('assert');
2+
var run = require('./helpers').runMocha;
3+
var args = [];
4+
5+
describe('suite w/no callback', function() {
6+
this.timeout(1000);
7+
it('should throw a helpful error message when a callback for suite is not supplied', function(done) {
8+
run('suite/suite-no-callback.fixture.js', args, function(err, res) {
9+
assert(!err);
10+
var result = res.output.match(/no callback was supplied/) || [];
11+
assert.equal(result.length, 1);
12+
done();
13+
});
14+
});
15+
});
16+
17+
describe('skipped suite w/no callback', function() {
18+
this.timeout(1000);
19+
it('should not throw an error when a callback for skipped suite is not supplied', function(done) {
20+
run('suite/suite-skipped-no-callback.fixture.js', args, function(err, res) {
21+
assert(!err);
22+
pattern = new RegExp("Error", 'g');
23+
var result = res.output.match(pattern) || [];
24+
assert.equal(result.length, 0);
25+
done();
26+
});
27+
});
28+
});
29+
30+
31+
describe('skipped suite w/ callback', function() {
32+
this.timeout(1000);
33+
it('should not throw an error when a callback for skipped suite is supplied', function(done) {
34+
run('suite/suite-skipped-callback.fixture.js', args, function(err, res) {
35+
assert(!err);
36+
pattern = new RegExp("Error", 'g');
37+
var result = res.output.match(pattern) || [];
38+
assert.equal(result.length, 0);
39+
done();
40+
});
41+
});
42+
});

0 commit comments

Comments
 (0)