-
-
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
Conversation
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.
👍 Thanks for this! Great work.
Can you please address these changes?
-
--exclude
should allow itself to be specified multiple times instead of expecting a comma-delimited value - ensure no unnecessary work is being done to check the type of
newFiles
on L500 - move the filter on L503 to chain on the call to
utils.lookupFiles()
on L489 - rename fixtures to have extension
.fixture.js
- see if you can use
runMocha()
instead ofdirectInvoke()
inoptions.spec.js
bin/_mocha
Outdated
@@ -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, []); |
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.
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 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.
@@ -0,0 +1,7 @@ | |||
'use strict'; |
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.
fixture files should all have the extension .fixture.js
test/integration/options.spec.js
Outdated
* 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 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
@@ -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 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 string
s, will it?
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.
newFiles
is the result of utils.lookupFiles
which can return an array of strings, a string, or undefined
.
Changes made:
Regarding these two:
Thanks for looking at this! |
This looks good. Not worried about conflicting with |
@boneskull any idea on when |
* 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.
Description of the Change
Adds option
--exclude
to exclude matched files.For example, this will match any files ending with "*.spec.js" unless they're inside the "node_modules" directory:
This required a small change to
bin/mocha.js
. As the file arguments are evaluated by mocha, they are checked against the pattern(s) provided by--exclude
. Any files matching any of the patterns in--exclude
are ignored.I used minimatch to check the filenames against the patterns. This is the same library used by glob.
As noted in #1577, this isn't strictly necessary.
mocha "./{,!(node_modules)/**/}*.spec.js"
would have the same effect as above. However, the--exclude
option is easier for users who don't speak glob.Why should this be in core?
Situations like the above example are common. Other tools (like Karma) include this behavior as core functionality.
Benefits
This is easier for users to understand how to exclude specific files or patterns, giving them more granular control over which files are used for testing without having to use complex glob patterns.
Possible Drawbacks
This is probably less performant than using glob patterns, and it's yet another option to support.
(If there is a more performant approach for implementing this behavior I'd be happy to change my code).
Applicable issues
Resolves #1577.
Enhancement.