Skip to content

Commit 8e740c5

Browse files
metalex9boneskull
authored andcommitted
#1577 Add "--exclude" Option (#3210)
* Add --exclude as option. * Add tests for --exclude option. * Implement --exclude. * Update --exclude description. * Remove old exclude fixture. * Add new exclude fixture. * Fix tests for --exclude option. * Add name to package.contributors. * Filter new files before they're added. * Allow multiple --exclude flags rather than using a comma-delimited list. * Use .fixture extension for exclude fixtures. * Use runMochaJSON (run) instead of of directInvoke.
1 parent ffd760e commit 8e740c5

File tree

7 files changed

+89
-1
lines changed

7 files changed

+89
-1
lines changed

bin/_mocha

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
const program = require('commander');
1111
const path = require('path');
1212
const fs = require('fs');
13+
const minimatch = require('minimatch');
1314
const resolve = path.resolve;
1415
const exists = fs.existsSync;
1516
const Mocha = require('../');
@@ -205,7 +206,8 @@ program
205206
.option('--allow-uncaught', 'enable uncaught errors to propagate')
206207
.option('--forbid-only', 'causes test marked with only to fail the suite')
207208
.option('--forbid-pending', 'causes pending tests and test marked with skip to fail the suite')
208-
.option('--file <file>', 'include a file to be ran during the suite', collect, []);
209+
.option('--file <file>', 'include a file to be ran during the suite', collect, [])
210+
.option('--exclude <file>', 'a file or glob pattern to ignore', collect, []);
209211

210212
program._name = 'mocha';
211213

@@ -494,6 +496,13 @@ args.forEach(arg => {
494496
throw err;
495497
}
496498

499+
if (typeof newFiles !== 'undefined') {
500+
if (typeof newFiles === 'string') {
501+
newFiles = [newFiles];
502+
}
503+
newFiles = newFiles.filter(fileName => program.exclude.every(pattern => !minimatch(fileName, pattern)));
504+
}
505+
497506
files = files.concat(newFiles);
498507
});
499508

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"Adrian Ludwig (https://github.com/adrian-ludwig)",
2020
"Ainthe Kitchen <[email protected]> (https://github.com/ainthek)",
2121
"ajaykodali (https://github.com/ajaykodali)",
22+
"Alex Bainter <[email protected]> (https://github.com/metalex9)",
2223
"Alex Early (https://github.com/aearly)",
2324
"Alex Pham <[email protected]> (https://github.com/thedark1337)",
2425
"amsul (https://github.com/amsul)",
@@ -314,6 +315,7 @@
314315
"glob": "7.1.2",
315316
"growl": "1.10.3",
316317
"he": "1.1.1",
318+
"minimatch": "^3.0.4",
317319
"mkdirp": "0.5.1",
318320
"supports-color": "4.4.0"
319321
},
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
describe('exclude test fail', function () {
4+
it('should not run this test', function () {
5+
throw new Error('should not run');
6+
});
7+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
describe('exclude test nested fail', function () {
4+
it('should not run this test', function () {
5+
throw new Error('should not run');
6+
});
7+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict';
2+
3+
describe('exclude test nested pass', function () {
4+
it('should find this test', function () {});
5+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict';
2+
3+
describe('exclude test pass', function () {
4+
it('should find this test', function () {});
5+
});

test/integration/options.spec.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,4 +450,57 @@ describe('options', function () {
450450
}, path.join(__dirname, 'fixtures', 'options', 'help'));
451451
});
452452
});
453+
454+
describe('--exclude', function () {
455+
/*
456+
* Runs mocha in {path} with the given args.
457+
* Calls handleResult with the result.
458+
*/
459+
function runMochaTest (fixture, args, handleResult, done) {
460+
run(fixture, args, function (err, res) {
461+
if (err) {
462+
done(err);
463+
return;
464+
}
465+
handleResult(res);
466+
done();
467+
});
468+
}
469+
470+
it('should exclude specific files', function (done) {
471+
runMochaTest('options/exclude/*.fixture.js', [
472+
'--exclude',
473+
'test/integration/fixtures/options/exclude/fail.fixture.js'
474+
], function (res) {
475+
assert.equal(res.stats.pending, 0);
476+
assert.equal(res.stats.passes, 1);
477+
assert.equal(res.stats.failures, 0);
478+
assert.equal(res.passes[0].title, 'should find this test');
479+
assert.equal(res.code, 0);
480+
}, done);
481+
});
482+
483+
it('should exclude globbed files', function (done) {
484+
runMochaTest('options/exclude/**/*.fixture.js', ['--exclude', '**/fail.fixture.js'], function (res) {
485+
assert.equal(res.stats.pending, 0);
486+
assert.equal(res.stats.passes, 2);
487+
assert.equal(res.stats.failures, 0);
488+
assert.equal(res.code, 0);
489+
}, done);
490+
});
491+
492+
it('should exclude multiple patterns', function (done) {
493+
runMochaTest('options/exclude/**/*.fixture.js', [
494+
'--exclude',
495+
'test/integration/fixtures/options/exclude/fail.fixture.js',
496+
'--exclude',
497+
'test/integration/fixtures/options/exclude/nested/fail.fixture.js'
498+
], function (res) {
499+
assert.equal(res.stats.pending, 0);
500+
assert.equal(res.stats.passes, 2);
501+
assert.equal(res.stats.failures, 0);
502+
assert.equal(res.code, 0);
503+
}, done);
504+
});
505+
});
453506
});

0 commit comments

Comments
 (0)