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.

Fixes #1333.
  • Loading branch information
Krinkle committed Jan 3, 2019
1 parent 2fb0e08 commit dc7fabe
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
18 changes: 14 additions & 4 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 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 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,7 @@ class Assert {

currentTest.assert.pushResult( {
result,
actual,
actual: errorString( actual ),
expected,
message
} );
Expand Down Expand Up @@ -378,12 +383,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 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 +401,9 @@ class Assert {
actual.name === expected.name &&
actual.message === expected.message;

// Log the string form 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 +422,7 @@ class Assert {

currentTest.assert.pushResult( {
result,
actual,
actual: errorString( actual ),
expected,
message
} );
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

0 comments on commit dc7fabe

Please sign in to comment.