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

safely stringify PhantomJS undefined value #1977

Merged
merged 1 commit into from
Dec 28, 2015
Merged

safely stringify PhantomJS undefined value #1977

merged 1 commit into from
Dec 28, 2015

Conversation

ahamid
Copy link
Contributor

@ahamid ahamid commented Nov 24, 2015

When running in PhantomJS, due to http://stackoverflow.com/questions/14218670/why-are-null-and-undefined-of-the-type-domwindow canonicalize can throw an exception, resulting from Runner.prototype.uncaught returning without emitting the end event (causing test mocha-phantomjs test runner to just hang and not exit). I'm not sure whether it's better to alter logic in Runner.prototype.uncaught to ensure end is always emitting, as I don't really understand the logic there (why in certain cases it returns without emitting).

This patch just catches an exception stringifying the canonicalized value, which seems like a reasonable thing to do anyway.

Review on Reviewable

@kcrwfrd
Copy link

kcrwfrd commented Dec 3, 2015

👍

@danielstjules
Copy link
Contributor

Thanks for the PR! Can you please revert changes to mocha.js? We only build that file before releases.

Also, could we not use canonicalizedObj = value +''; instead of canonicalizedObj = value.toString();?

var obj = {a: "b", toString: function() { return "foobar"; }};
obj.toString(); // "foobar"
obj+''; // "foobar"

@ahamid
Copy link
Contributor Author

ahamid commented Dec 28, 2015

@danielstjules I have reverted the change to mocha.js. I'm using value + '' explicitly because the failure may be due to the value being null or undefined. Arguably something more strict should happen here, but at least string concatenation coerces those values to "null" and "undefined". It's a workaround.

@danielstjules
Copy link
Contributor

Thanks for fixing that :) And what I meant was, why couldn't you do:

// Replace this
try {
  canonicalizedObj = value.toString();
} catch (e) {
  debug("Error invoking `toString()` on value: '%s'", value);
  canonicalizedObj = value + '';
}

// With this =>
canonicalizedObj = value + '';

Note the example I posted in my initial reply:

var obj = {a: "b", toString: function() { return "foobar"; }};
obj.toString(); // "foobar"
obj+''; // "foobar"

@danielstjules
Copy link
Contributor

Also, mind rebasing/squashing when done? :)

@ahamid
Copy link
Contributor Author

ahamid commented Dec 28, 2015

@danielstjules oh sorry, of course, I thought you were asking for the opposite. I didn't touch that line for the sake of preserving the initial intent, but I think you're right it's semantically the same.

So...here's the squashed one-liner :)

@danielstjules
Copy link
Contributor

Awesome, thanks again! :)

danielstjules added a commit that referenced this pull request Dec 28, 2015
safely stringify PhantomJS undefined value
@danielstjules danielstjules merged commit 0d3321d into mochajs:master Dec 28, 2015
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

Successfully merging this pull request may close these issues.

None yet

3 participants