-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Changes from 9 commits
95aad4f
4dbc972
0790b1c
0316b2d
64684b8
9d0aa7f
87b8ef3
5c9547b
db0d1fd
88a98ad
aa307f8
d17863b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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('../'); | ||
|
@@ -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, []); | ||
|
||
program._name = 'mocha'; | ||
|
||
|
@@ -494,6 +496,13 @@ args.forEach(arg => { | |
throw err; | ||
} | ||
|
||
if (typeof newFiles !== 'undefined') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
if (typeof newFiles === 'string') { | ||
newFiles = [newFiles]; | ||
} | ||
newFiles = newFiles.filter(fileName => program.exclude.every(pattern => !minimatch(fileName, pattern))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you may be able to just chain this ( |
||
} | ||
|
||
files = files.concat(newFiles); | ||
}); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
'use strict'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixture files should all have the extension |
||
|
||
describe('exclude test fail', function () { | ||
it('should not run this test', function () { | ||
throw new Error('should not run'); | ||
}); | ||
}); |
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'); | ||
}); | ||
}); |
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 () {}); | ||
}); |
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 () {}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unless there's a Good Reason to use |
||
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); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
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.