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

Add ability to pass in test files to be ran before positional files via --file #3190

Merged
merged 4 commits into from Jan 16, 2018
Merged

Add ability to pass in test files to be ran before positional files via --file #3190

merged 4 commits into from Jan 16, 2018

Conversation

hswolff
Copy link
Contributor

@hswolff hswolff commented Jan 3, 2018

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

@jsf-clabot
Copy link

jsf-clabot commented Jan 3, 2018

CLA assistant check
All committers have signed the CLA.

@coveralls
Copy link

Coverage Status

Coverage decreased (-4.9%) to 85.073% when pulling 9ea849c on hswolff:add-file-arg into a554adb on mochajs:master.

Copy link
Member

@boneskull boneskull left a 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, []);
Copy link
Member

@boneskull boneskull Jan 3, 2018

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

Copy link
Member

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);
Copy link
Member

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.

Copy link
Contributor Author

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]
Copy link
Member

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.

Copy link
Contributor Author

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) {
Copy link
Member

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?

@@ -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;
Copy link
Member

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

@@ -91,6 +92,29 @@ describe('options', function () {
});
});

describe.only('--file', function () {
Copy link
Member

Choose a reason for hiding this comment

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

whoops 😉

Copy link
Contributor Author

Choose a reason for hiding this comment

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

D'oh! haha

@@ -91,6 +92,29 @@ describe('options', function () {
});
});

describe.only('--file', function () {
before(function () {
Copy link
Member

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.

args = ['--file', resolvePath('options/file-alpha.fixture.js')];
});

it('should run tests passed via file first', function (done) {
Copy link
Member

Choose a reason for hiding this comment

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

good

@boneskull boneskull added the semver-minor implementation requires increase of "minor" version number; "features" label Jan 3, 2018
@boneskull
Copy link
Member

please reverse the whitespace changes; this might cause difficulty with incoming PR #3173

@boneskull
Copy link
Member

coveralls failure due to use of .only

@hswolff
Copy link
Contributor Author

hswolff commented Jan 4, 2018

Thank you so much for the fast review!

All PR feedback addressed. Thank for catching that .only goof :D

bin/_mocha Outdated
/**
* Parse multiple flag.
*/
const collect = (val, memo) => {
Copy link

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)?

Copy link
Contributor Author

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.

Copy link
Member

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);

@coveralls
Copy link

Coverage Status

Coverage increased (+0.05%) to 90.018% when pulling a12c310 on hswolff:add-file-arg into a554adb on mochajs:master.

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)
Copy link
Member

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

Copy link
Member

@boneskull boneskull left a 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, []);
Copy link
Member

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.
Copy link
Member

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

Copy link
Contributor Author

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 () {
Copy link
Member

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...

Copy link
Contributor Author

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.

@hswolff
Copy link
Contributor Author

hswolff commented Jan 5, 2018

RFAL.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.05%) to 90.018% when pulling 0008924 on hswolff:add-file-arg into a723b8f on mochajs:master.

@boneskull
Copy link
Member

Awesome, thanks.

@boneskull boneskull merged commit 50aec7a into mochajs:master Jan 16, 2018
@hswolff hswolff deleted the add-file-arg branch January 16, 2018 20:50
@boneskull boneskull added this to the v5.0.0 milestone Jan 17, 2018
@boneskull boneskull added the type: feature enhancement proposal label Jan 17, 2018
sgilroy pushed a commit to TwineHealth/mocha that referenced this pull request Feb 27, 2019
…ia --file (mochajs#3190)

* Add ability to pass in test files to be ran before positional files via --file

Fixes mochajs#3181
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
semver-minor implementation requires increase of "minor" version number; "features" type: feature enhancement proposal
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add new --add-file CLI flag
5 participants