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

Fix .only() behaviour when running with watch option, fixes #2429 #2544

Merged
merged 3 commits into from
Sep 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions lib/interfaces/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ module.exports = function (suites, context, mocha) {
* @returns {Suite}
*/
only: function only (opts) {
mocha.options.hasOnly = true;
opts.isOnly = true;
return this.create(opts);
},
Expand Down Expand Up @@ -108,7 +107,6 @@ module.exports = function (suites, context, mocha) {
suites.unshift(suite);
if (opts.isOnly) {
suite.parent._onlySuites = suite.parent._onlySuites.concat(suite);
mocha.options.hasOnly = true;
}
if (typeof opts.fn === 'function') {
opts.fn.call(suite);
Expand All @@ -132,7 +130,6 @@ module.exports = function (suites, context, mocha) {
*/
only: function (mocha, test) {
test.parent._onlyTests = test.parent._onlyTests.concat(test);
mocha.options.hasOnly = true;
return test;
},

Expand Down
1 change: 0 additions & 1 deletion lib/mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,6 @@ Mocha.prototype.run = function (fn) {
var reporter = new this._reporter(runner, options);
runner.ignoreLeaks = options.ignoreLeaks !== false;
runner.fullStackTrace = options.fullStackTrace;
runner.hasOnly = options.hasOnly;
runner.asyncOnly = options.asyncOnly;
runner.allowUncaught = options.allowUncaught;
runner.forbidOnly = options.forbidOnly;
Expand Down
4 changes: 2 additions & 2 deletions lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ Runner.prototype.runTest = function (fn) {
if (!test) {
return;
}
if (this.forbidOnly && this.hasOnly) {
if (this.forbidOnly && hasOnly(this.parents().reverse()[0] || this.suite)) {
fn(new Error('`.only` forbidden'));
return;
}
Expand Down Expand Up @@ -816,7 +816,7 @@ Runner.prototype.run = function (fn) {
var rootSuite = this.suite;

// If there is an `only` filter
if (this.hasOnly) {
if (hasOnly(rootSuite)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Okay, so it looks like with the addition of --forbid-only, this may need some changes.

Runner#hasOnly is set if e.g. a describe.only() is encountered (see lib/interfaces/common.js).

The --forbid-only functionality will check this value. At the point that it's checked, the Runner may or may not have a suite property. This means there's likely some weird interactions between --forbid-only and --watch.

A solution may be to set Runner#hasOnly to true if the result of hasOnly() is true. hasOnly() may also want to check the value of Runner#hasOnly. When the Runner's end event is handled, it may also need to reset Runner#hasOnly to false.

I haven't fiddled with this too much, but this is the matrix we're trying to assert is OK:

  1. describe.only()
  2. it.only()
  3. --watch
  4. --forbid-only

For example, run a --watch with --forbid-only, then add a describe.only(), save, then remove it again & save. What happens?

Copy link
Contributor

Choose a reason for hiding this comment

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

(careful with nested suites here; hasOnly() looks like it only inspects the innards of any given suite, and not if that suite was itself marked as an "only" suite; that information is stored in the aforemention suite's parent! 😄)

Copy link
Contributor

Choose a reason for hiding this comment

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

Worth noting that #2874 also touches hasOnly-using code in addition to the --forbid-only code already merged doing so.

(I plan on a more detailed review later this week.)

Copy link
Contributor

@ScottFreeCode ScottFreeCode Sep 29, 2017

Choose a reason for hiding this comment

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

This should be merged correctly now, I think.

filterOnly(rootSuite);
}

Expand Down