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

Do not hard code err instanceof Error checks #1627

Closed
brian-mann opened this issue Mar 26, 2015 · 7 comments
Closed

Do not hard code err instanceof Error checks #1627

brian-mann opened this issue Mar 26, 2015 · 7 comments

Comments

@brian-mann
Copy link

Specifically this line: https://github.com/mochajs/mocha/blob/master/lib/runner.js#L205

Long story short, err may be a valid Error instance but in situations such as iframes each window will have its own Error object, and this will therefore always always fail.

I would suggest taking after what lodash does in its isError function.

It just ensures its "object like", has a string for message property, and is [object Error]

//lodash isError implementation
function isError(value) {
  return (isObjectLike(value) && typeof value.message == 'string' && objToString.call(value) == errorTag) || false;
}
@danielstjules
Copy link
Contributor

Good idea! We wouldn't be able to use lodash's implementation out of the box because of how pending tests are defined and propagated up to the runner/runnables. https://github.com/mochajs/mocha/blob/master/lib/pending.js If I recall correctly, there's cases where the instance of Pending will have an undefined message. That logic will need to be tweaked a bit.

@boneskull boneskull added the status: accepting prs Mocha can use your help with this one! label Mar 28, 2015
@brian-mann
Copy link
Author

Just as another reference, in other parts of Mocha, specifically here: https://github.com/mochajs/mocha/blob/master/lib/runnable.js#L234

Mocha already toStrings() the object as a fallback to the instanceof checks. That's all that's needed IMO.

@outdooricon
Copy link
Contributor

See #1758 for related PR.

@outdooricon
Copy link
Contributor

@brian-mann, does that pr solve what you're looking for? The object toString method that you mentioned doesn't work because it can still return something other than [object Object], as it does for AssertionError.

@danielstjules I don't think checking for error message is a problem here. Pending doesn't appear to be an instance of Error, so it should continue to work as implemented if a Pending object does indeed reach this.

@outdooricon
Copy link
Contributor

@boneskull I think we can remove the pr-please label at this point.

@danielstjules danielstjules removed the status: accepting prs Mocha can use your help with this one! label Jun 28, 2015
@danielstjules
Copy link
Contributor

so it should continue to work as implemented if a Pending object does indeed reach this.

Good point :) Not sure what I was thinking

@danielstjules
Copy link
Contributor

Closed by #1758

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants