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

bug fix: concat(undefined) returns [undefined] #3668

Closed
wants to merge 1 commit into from

Conversation

givanse
Copy link
Contributor

@givanse givanse commented Jan 12, 2019

Requirements

Description of the Change

This line: this.options.globals = (this.options.globals || []).concat(globals); has the potential to produce arrays that contain the undefined element.

The result of concat(undefined) is [undefined], an array of length 1 that contains undefined.

Later in lib/runner there is a filter that iterates on the globals list and raises an error when trying to reference undefined.

Alternate Designs

There is different syntax that could be used to achieve the same validation and fallback. If there is a preferred style I'm happy to adjust to it.

Why should this be in core?

It is fixing an edge case within the core libs.

Benefits

Protects users from an edge case where globals is has falsy value.

Possible Drawbacks

The impact is trivial, it increases the set up overhead by a little bit (if at all).

Applicable issues

patch release

@coveralls
Copy link

Coverage Status

Coverage increased (+0.04%) to 90.814% when pulling 3efcc84 on givanse:concat-undefined into 0a86e6f on mochajs:master.

@boneskull
Copy link
Contributor

@givanse Seems plausible, but how did you encounter this?

@boneskull boneskull added semver-patch implementation requires increase of "patch" version number; "bug fixes" status: waiting for author waiting on response from OP - more information needed labels Jan 17, 2019
@givanse
Copy link
Contributor Author

givanse commented Jan 18, 2019

I encountered this by using Mocha programmatically with Testem. Right now I don't have access to the codebase. So it is going to take me a few days (maybe Wednesday) to get back to you with more details.

Maybe the fallback could be moved up, to:

.globals(options.globals);

and use .globals(options.globals || []);

this.options.globals = (this.options.globals || []).concat(globals);
this.options.globals = this.options.globals || [];
globals = globals || [];
this.options.globals = this.options.globals.concat(globals);
return this;
Copy link
Contributor

Choose a reason for hiding this comment

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

Since we shouldn't allow empty strings either...

Mocha.prototype.globals = function(globals) {
  this.options.globals = (this.options.globals || []).concat(globals).filter(Boolean);
  return this;
};

@boneskull boneskull added stale this has been inactive for a while... and removed status: waiting for author waiting on response from OP - more information needed labels Feb 10, 2019
@stale stale bot removed the stale this has been inactive for a while... label Feb 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
semver-patch implementation requires increase of "patch" version number; "bug fixes"
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants