-
-
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
Add ability to pass in test files to be ran before positional files via --file #3190
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; I wasn't going to have time to do it myself. I've requested some changes here.
bin/_mocha
Outdated
@@ -199,7 +205,8 @@ program | |||
.option('--delay', 'wait for async suite definition') | |||
.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('--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', 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.
alas, -f
is taken, so no alias is possible
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 was a repeatable flag instead of one that must be comma-delimited. I assume that's possible in commander, but am unfamiliar with its API
files = files.map(path => resolve(path)); | ||
|
||
if (program.sort) { | ||
files.sort(); | ||
} | ||
|
||
// add files given through --file to be ran first | ||
files = fileArgs.concat(files); |
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.
okay, so --sort
doesn't work with --file
, which makes sense. that should be noted somewhere if it isn't already.
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.
Added in docs/index.md
@@ -737,6 +737,7 @@ Mocha supports the `err.expected` and `err.actual` properties of any thrown `Ass | |||
--debug-brk enable node's debugger breaking on the first line | |||
--globals <names> allow the given comma-delimited global [names] | |||
--es_staging enable all staged features | |||
--file <file> include a file to be ran during the suite [file] |
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.
We should add a section in the documentation for this option.
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.
Added below.
|
||
describe('alpha', function () { | ||
it('should be executed first', function () { | ||
if (global.beta) { |
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.
probably better to assert global.beta
to be undefined?
test/integration/options.spec.js
Outdated
@@ -4,6 +4,7 @@ var path = require('path'); | |||
var assert = require('assert'); | |||
var run = require('./helpers').runMochaJSON; | |||
var directInvoke = require('./helpers').invokeMocha; | |||
var resolvePath = require('./helpers').resolveFixturePath; |
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.
we may as well:
var helpers = require('helpers');
var run = helpers.run;
// etc
test/integration/options.spec.js
Outdated
@@ -91,6 +92,29 @@ describe('options', function () { | |||
}); | |||
}); | |||
|
|||
describe.only('--file', function () { |
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.
whoops 😉
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.
D'oh! haha
test/integration/options.spec.js
Outdated
@@ -91,6 +92,29 @@ describe('options', function () { | |||
}); | |||
}); | |||
|
|||
describe.only('--file', function () { | |||
before(function () { |
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.
a good rule of thumb is that you should always use beforeEach
unless you can't.
test/integration/options.spec.js
Outdated
args = ['--file', resolvePath('options/file-alpha.fixture.js')]; | ||
}); | ||
|
||
it('should run tests passed via file first', function (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.
good
please reverse the whitespace changes; this might cause difficulty with incoming PR #3173 |
coveralls failure due to use of |
Thank you so much for the fast review! All PR feedback addressed. Thank for catching that |
bin/_mocha
Outdated
/** | ||
* Parse multiple flag. | ||
*/ | ||
const collect = (val, memo) => { |
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.
Could this just return memo.concat(val)
, or be Function.bind(Function.call, Array.prototype.concat)
?
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.
That it could. I took this straight from the commander.js page. I'm fine with it as is as it's pretty clear to any reader what the code is doing. Happy to change if people feel strongly otherwise.
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.
yeah, I agree this would be better:
const collect = (val, memo) => memo.concat(val);
docs/index.md
Outdated
@@ -25,17 +25,17 @@ Mocha is a feature-rich JavaScript test framework running on [Node.js](https://n | |||
- [maps uncaught exceptions to the correct test case](#browser-specific-methods) | |||
- [async test timeout support](#delayed-root-suite) | |||
- [test retry support](#retry-tests) | |||
- [test-specific timeouts](#test-level) | |||
- [test-specific timeouts](#test-level) |
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.
looks like the whitespace changes crept back in. that other PR has been merged, so if there are no conflicts then this is fine. please rebase onto master
if you haven't done so yet
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 the updates; was hoping you could address a few more issues I've found.
bin/_mocha
Outdated
@@ -199,7 +207,8 @@ program | |||
.option('--delay', 'wait for async suite definition') | |||
.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('--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, []); |
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.
the parameter to --file
is required, so I think this needs to be --file <file>
.
docs/index.md
Outdated
@@ -850,6 +851,10 @@ Specifies the test-case timeout, defaulting to 2 seconds. To override you may pa | |||
|
|||
Specify the "slow" test threshold, defaulting to 75ms. Mocha uses this to highlight test-cases that are taking too long. | |||
|
|||
### `--file <file>` | |||
|
|||
Add a file you want included first in a test suite. This is useful if you have some generic setup code that must be included within the test suite. The file passed is not effected by any other flags (`--recursive` or `--sort` have no effect). Accepts multiple `--file` flags to include multiple files. |
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.
-
--recursive
does come into play, afaict; so it's only--sort
that doesn't apply. - should be "is not affected" not "is not effected"
- let's mention that the order in which the flags are specified is relevant, and
- it can be used in
mocha.opts
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'm not seeing recursive effect. On line 476 it's taking the args passed in and then iterating over those values and applying recursive to them. Am I missing something?
@@ -91,6 +93,29 @@ describe('options', function () { | |||
}); | |||
}); | |||
|
|||
describe('--file', function () { |
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.
can we also test multiple --file
flag uses? this is basically a copy/paste of the test on L101 w/o the first parameter to runMochaJSON
; instead, appending to args
.
you may need to monkey with runMochaJSON
to optionally accept an array as the first argument...
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.
Added an individual additional test that should provide coverage for this.
RFAL. |
Awesome, thanks. |
…ia --file (mochajs#3190) * Add ability to pass in test files to be ran before positional files via --file Fixes mochajs#3181
This is to resolve #3181.
I was a little unsure how to describe this change. I think including something about how these args are ran first should be noted but I'm not exactly sure how best to do that.
Also I didn't do any file path sanitization to anything passed into
--file
nor did I apply--recursive
to the path as right now I'm of the mind that any file passed to this argument should be explicit and not be effected by any sibling flags.Happy to change anything that people disagree with.
Also apologies for the trailing whitespace cleanup, that was done by my editor. Happy to remove that change if you want.
Cheers! Excited to contribute! :D