Navigation Menu

Skip to content

Commit

Permalink
Assert: Support rejecting falsy values against no matcher in rejects
Browse files Browse the repository at this point in the history
  • Loading branch information
trentmwillis committed Mar 1, 2018
1 parent b16b16e commit a577183
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 33 deletions.
62 changes: 30 additions & 32 deletions src/assert.js
Expand Up @@ -373,41 +373,39 @@ class Assert {
},

function handleRejection( actual ) {
if ( actual ) {
const expectedType = objectType( expected );

// We don't want to validate
if ( expected === undefined ) {
result = true;
const expectedType = objectType( expected );

// 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 ) );

// Expected is a constructor, maybe an Error constructor
} else if ( expectedType === "function" && actual instanceof expected ) {
result = true;

// Expected is an Error object
} else if ( expectedType === "object" ) {
result = actual instanceof expected.constructor &&
actual.name === expected.name &&
actual.message === expected.message;

// Expected is a validation function which returns true if validation passed
} else {
if ( expectedType === "function" ) {
result = expected.call( {}, actual ) === true;
expected = null;

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

// Expected is a constructor, maybe an Error constructor
} else if ( expectedType === "function" && actual instanceof expected ) {
result = true;

// Expected is an Error object
} else if ( expectedType === "object" ) {
result = actual instanceof expected.constructor &&
actual.name === expected.name &&
actual.message === expected.message;

// Expected is a validation function which returns true if validation passed
// Expected is some other invalid type
} else {
if ( expectedType === "function" ) {
result = expected.call( {}, actual ) === true;
expected = null;

// Expected is some other invalid type
} else {
result = false;
message = "invalid expected value provided to `assert.rejects` " +
"callback in \"" + currentTest.testName + "\": " +
expectedType + ".";
}
result = false;
message = "invalid expected value provided to `assert.rejects` " +
"callback in \"" + currentTest.testName + "\": " +
expectedType + ".";
}
}

Expand Down
7 changes: 6 additions & 1 deletion test/main/assert.js
Expand Up @@ -287,7 +287,7 @@ QUnit.test( "throws", function( assert ) {
} );

QUnit.test( "rejects", function( assert ) {
assert.expect( 15 );
assert.expect( 16 );

function CustomError( message ) {
this.message = message;
Expand Down Expand Up @@ -396,6 +396,11 @@ QUnit.test( "rejects", function( assert ) {
/description/,
"throw error from property of 'this' context"
);

assert.rejects(
buildMockPromise( undefined ),
"reject with undefined against no matcher"
);
} );

QUnit.test( "raises, alias for throws", function( assert ) {
Expand Down

0 comments on commit a577183

Please sign in to comment.