Skip to content

Commit 08f1c69

Browse files
committed
Assert: Report RegExp/Error as strings from rejects()/throws()
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.
1 parent 4255f95 commit 08f1c69

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

src/assert.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -286,12 +286,14 @@ class Assert {
286286
// We don't want to validate thrown error
287287
if ( !expected ) {
288288
result = true;
289-
expected = null;
290289

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

294+
// Log the string form of the regexp
295+
expected = String( expected );
296+
295297
// Expected is a constructor, maybe an Error constructor
296298
} else if ( expectedType === "function" && actual instanceof expected ) {
297299
result = true;
@@ -302,6 +304,9 @@ class Assert {
302304
actual.name === expected.name &&
303305
actual.message === expected.message;
304306

307+
// Log the string form of the Error object
308+
expected = errorString( expected );
309+
305310
// Expected is a validation function which returns true if validation passed
306311
} else if ( expectedType === "function" && expected.call( {}, actual ) === true ) {
307312
expected = null;
@@ -311,7 +316,9 @@ class Assert {
311316

312317
currentTest.assert.pushResult( {
313318
result,
314-
actual,
319+
320+
// undefined if it didn't throw
321+
actual: actual && errorString( actual ),
315322
expected,
316323
message
317324
} );
@@ -378,12 +385,14 @@ class Assert {
378385
// We don't want to validate
379386
if ( expected === undefined ) {
380387
result = true;
381-
expected = actual;
382388

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

393+
// Log the string form of the regexp
394+
expected = String( expected );
395+
387396
// Expected is a constructor, maybe an Error constructor
388397
} else if ( expectedType === "function" && actual instanceof expected ) {
389398
result = true;
@@ -394,6 +403,9 @@ class Assert {
394403
actual.name === expected.name &&
395404
actual.message === expected.message;
396405

406+
// Log the string form of the Error object
407+
expected = errorString( expected );
408+
397409
// Expected is a validation function which returns true if validation passed
398410
} else {
399411
if ( expectedType === "function" ) {
@@ -412,7 +424,9 @@ class Assert {
412424

413425
currentTest.assert.pushResult( {
414426
result,
415-
actual,
427+
428+
// leave rejection value of undefined as-is
429+
actual: actual && errorString( actual ),
416430
expected,
417431
message
418432
} );
@@ -431,12 +445,14 @@ Assert.prototype.raises = Assert.prototype[ "throws" ];
431445
/**
432446
* Converts an error into a simple string for comparisons.
433447
*
434-
* @param {Error} error
448+
* @param {Error|Object} error
435449
* @return {String}
436450
*/
437451
function errorString( error ) {
438452
const resultErrorString = error.toString();
439453

454+
// If the error wasn't a subclass of Error but something like
455+
// an object literal with name and message properties...
440456
if ( resultErrorString.substring( 0, 7 ) === "[object" ) {
441457
const name = error.name ? error.name.toString() : "Error";
442458
const message = error.message ? error.message.toString() : "";

test/cli/fixtures/expected/tap-outputs.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ ok 2 Second > 1
5454

5555
"qunit fail/throws-match.js":
5656
`TAP version 13
57-
not ok 1 global failure
57+
not ok 1 Throws match > bad
5858
---
5959
message: "match error"
6060
severity: failed
61-
actual: {}
62-
expected: {}
61+
actual: "Error: Match me with a pattern"
62+
expected: "/incorrect pattern/"
6363
stack: .*
6464
...
6565
1..1

test/main/assert.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ QUnit.test( "throws", function( assert ) {
252252
};
253253
},
254254
{ name: "SomeName", message: "some message" },
255-
"thrown error object is similar to the expected plain object"
255+
"thrown object is similar to the expected plain object"
256256
);
257257

258258
assert.throws(
@@ -378,7 +378,7 @@ QUnit.test( "rejects", function( assert ) {
378378
message: "some message"
379379
} ),
380380
{ name: "SomeName", message: "some message" },
381-
"thrown error object is similar to the expected plain object"
381+
"thrown object is similar to the expected plain object"
382382
);
383383

384384
assert.rejects(

0 commit comments

Comments
 (0)