Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1577 Add "--exclude" Option #3210

Merged
merged 12 commits into from
Apr 1, 2018
11 changes: 10 additions & 1 deletion bin/_mocha
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
const program = require('commander');
const path = require('path');
const fs = require('fs');
const minimatch = require('minimatch');
const resolve = path.resolve;
const exists = fs.existsSync;
const Mocha = require('../');
Expand Down Expand Up @@ -205,7 +206,8 @@ program
.option('--allow-uncaught', 'enable uncaught errors to propagate')
.option('--forbid-only', 'causes test marked with only to fail the suite')
.option('--forbid-pending', 'causes pending tests and test marked with skip to fail the suite')
.option('--file <file>', 'include a file to be ran during the suite', collect, []);
.option('--file <file>', 'include a file to be ran during the suite', collect, [])
.option('--exclude <files>', 'comma-delimited list of files or glob patterns to ignore', list, []);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer this works like --file above, where it can be provided multiple times.


program._name = 'mocha';

Expand Down Expand Up @@ -494,6 +496,13 @@ args.forEach(arg => {
throw err;
}

if (typeof newFiles !== 'undefined') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think newFiles will be anything other than an Array of strings, will it?

Copy link
Author

@alexbainter alexbainter Jan 20, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

newFiles is the result of utils.lookupFiles which can return an array of strings, a string, or undefined.

if (typeof newFiles === 'string') {
newFiles = [newFiles];
}
newFiles = newFiles.filter(fileName => program.exclude.every(pattern => !minimatch(fileName, pattern)));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you may be able to just chain this (.filter and everything after) to L489.

}

files = files.concat(newFiles);
});

Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"Adrian Ludwig (https://github.com/adrian-ludwig)",
"Ainthe Kitchen <a.in.the.k@gmail.com> (https://github.com/ainthek)",
"ajaykodali (https://github.com/ajaykodali)",
"Alex Bainter <alexbainter+github@gmail.com> (https://github.com/metalex9)",
"Alex Early (https://github.com/aearly)",
"Alex Pham <thedark1337@thedark1337.com> (https://github.com/thedark1337)",
"amsul (https://github.com/amsul)",
Expand Down Expand Up @@ -314,6 +315,7 @@
"glob": "7.1.2",
"growl": "1.10.3",
"he": "1.1.1",
"minimatch": "^3.0.4",
"mkdirp": "0.5.1",
"supports-color": "4.4.0"
},
Expand Down
7 changes: 7 additions & 0 deletions test/integration/fixtures/options/exclude/fail.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixture files should all have the extension .fixture.js


describe('exclude test fail', function () {
it('should not run this test', function () {
throw new Error('should not run');
});
});
7 changes: 7 additions & 0 deletions test/integration/fixtures/options/exclude/nested/fail.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

describe('exclude test nested fail', function () {
it('should not run this test', function () {
throw new Error('should not run');
});
});
5 changes: 5 additions & 0 deletions test/integration/fixtures/options/exclude/nested/pass.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

describe('exclude test nested pass', function () {
it('should find this test', function () {});
});
5 changes: 5 additions & 0 deletions test/integration/fixtures/options/exclude/pass.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

describe('exclude test pass', function () {
it('should find this test', function () {});
});
37 changes: 37 additions & 0 deletions test/integration/options.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -415,4 +415,41 @@ describe('options', function () {
}, path.join(__dirname, 'fixtures', 'options', 'help'));
});
});

describe('--exclude', function () {
/*
* Runs mocha in ./fixtures/options/exclude with the given args.
* Calls handleResult with the result
*/
function runExcludeTest (args, handleResult, done) {
directInvoke(args, function (error, result) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unless there's a Good Reason to use directInvoke(), please use runMocha() instead

if (error) {
return done(error);
}
handleResult(result);
done();
}, path.join(__dirname, 'fixtures', 'options', 'exclude'));
}

it('should exclude specific files', function (done) {
runExcludeTest(['*.spec.js', '--exclude', 'fail.spec.js'], function (result) {
expect(result.output).to.contain('1 passing');
expect(result.code).to.equal(0);
}, done);
});

it('should exclude globbed files', function (done) {
runExcludeTest(['**/*.spec.js', '--exclude', '**/fail.spec.js'], function (result) {
expect(result.output).to.contain('2 passing');
expect(result.code).to.equal(0);
}, done);
});

it('should exclude multiple patterns', function (done) {
runExcludeTest(['**/*.spec.js', '--exclude', 'fail.spec.js,nested/fail.spec.js'], function (result) {
expect(result.output).to.contain('2 passing');
expect(result.code).to.equal(0);
}, done);
});
});
});