Skip to content

Commit

Permalink
Assert: Report RegExp/Error as strings from rejects()/throws()
Browse files Browse the repository at this point in the history
Report the `actual` (Error object) as a string.
And report an `expected` RegExp or Error object also in its
string form.

Currently, they were reported as their objects, which in the
generic js-reporters module was just printed as an empty object
without any properties because instances of RegExp and instances
of Error both have no own properties that are enumerable.

Also fix errorString() which oddly had logic for passing a plain
object, but didn't

Fixes #1333.
  • Loading branch information
Krinkle committed Jan 3, 2019
1 parent 4255f95 commit 08f1c69
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
26 changes: 21 additions & 5 deletions src/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,12 +286,14 @@ class Assert {
// We don't want to validate thrown error
if ( !expected ) {
result = true;
expected = null;

// Expected is a regexp
} else if ( expectedType === "regexp" ) {
result = expected.test( errorString( actual ) );

// Log the string form of the regexp
expected = String( expected );

// Expected is a constructor, maybe an Error constructor
} else if ( expectedType === "function" && actual instanceof expected ) {
result = true;
Expand All @@ -302,6 +304,9 @@ class Assert {
actual.name === expected.name &&
actual.message === expected.message;

// Log the string form of the Error object
expected = errorString( expected );

// Expected is a validation function which returns true if validation passed
} else if ( expectedType === "function" && expected.call( {}, actual ) === true ) {
expected = null;
Expand All @@ -311,7 +316,9 @@ class Assert {

currentTest.assert.pushResult( {
result,
actual,

// undefined if it didn't throw
actual: actual && errorString( actual ),
expected,
message
} );
Expand Down Expand Up @@ -378,12 +385,14 @@ class Assert {
// We don't want to validate
if ( expected === undefined ) {
result = true;
expected = actual;

// Expected is a regexp
} else if ( expectedType === "regexp" ) {
result = expected.test( errorString( actual ) );

// Log the string form of the regexp
expected = String( expected );

// Expected is a constructor, maybe an Error constructor
} else if ( expectedType === "function" && actual instanceof expected ) {
result = true;
Expand All @@ -394,6 +403,9 @@ class Assert {
actual.name === expected.name &&
actual.message === expected.message;

// Log the string form of the Error object
expected = errorString( expected );

// Expected is a validation function which returns true if validation passed
} else {
if ( expectedType === "function" ) {
Expand All @@ -412,7 +424,9 @@ class Assert {

currentTest.assert.pushResult( {
result,
actual,

// leave rejection value of undefined as-is
actual: actual && errorString( actual ),
expected,
message
} );
Expand All @@ -431,12 +445,14 @@ Assert.prototype.raises = Assert.prototype[ "throws" ];
/**
* Converts an error into a simple string for comparisons.
*
* @param {Error} error
* @param {Error|Object} error
* @return {String}
*/
function errorString( error ) {
const resultErrorString = error.toString();

// If the error wasn't a subclass of Error but something like
// an object literal with name and message properties...
if ( resultErrorString.substring( 0, 7 ) === "[object" ) {
const name = error.name ? error.name.toString() : "Error";
const message = error.message ? error.message.toString() : "";
Expand Down
6 changes: 3 additions & 3 deletions test/cli/fixtures/expected/tap-outputs.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ ok 2 Second > 1

"qunit fail/throws-match.js":
`TAP version 13
not ok 1 global failure
not ok 1 Throws match > bad
---
message: "match error"
severity: failed
actual: {}
expected: {}
actual: "Error: Match me with a pattern"
expected: "/incorrect pattern/"
stack: .*
...
1..1
Expand Down
4 changes: 2 additions & 2 deletions test/main/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ QUnit.test( "throws", function( assert ) {
};
},
{ name: "SomeName", message: "some message" },
"thrown error object is similar to the expected plain object"
"thrown object is similar to the expected plain object"
);

assert.throws(
Expand Down Expand Up @@ -378,7 +378,7 @@ QUnit.test( "rejects", function( assert ) {
message: "some message"
} ),
{ name: "SomeName", message: "some message" },
"thrown error object is similar to the expected plain object"
"thrown object is similar to the expected plain object"
);

assert.rejects(
Expand Down

0 comments on commit 08f1c69

Please sign in to comment.