Skip to content

Commit

Permalink
fixup! Assert: Add assert.rejects.
Browse files Browse the repository at this point in the history
* Add explicit tests for various other invalid types (number, string,
  boolean, array, etc)
* Refactor logic around `expected` being a function in order to properly
  report the difference between invalid expected types and functions
  returning non-true values.
  • Loading branch information
rwjblue committed Dec 18, 2017
1 parent 8ab36f3 commit af87708
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 14 deletions.
40 changes: 26 additions & 14 deletions src/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,14 +313,19 @@ class Assert {

// 'expected' is optional unless doing string comparison
if ( objectType( expected ) === "string" ) {
if ( message == null ) {
if ( message === undefined ) {
message = expected;
expected = null;
expected = undefined;
} else {
throw new Error(
"rejects does not accept a string value for the expected argument.\n" +
"Use a non-string object value (e.g. regExp) instead if it's necessary."
);
message = "rejects does not accept a string value for the expected argument.\n" +
"Use a non-string object value (e.g. validator function) instead if necessary.";

currentTest.assert.pushResult( {
result: false,
message: message
} );

return;
}
}

Expand Down Expand Up @@ -359,8 +364,8 @@ class Assert {
if ( actual ) {
const expectedType = objectType( expected );

// We don't want to validate thrown error
if ( !expected ) {
// We don't want to validate
if ( expected === undefined ) {
result = true;
expected = null;

Expand All @@ -379,15 +384,22 @@ class Assert {
actual.message === expected.message;

// Expected is a validation function which returns true if validation passed
} else if (
expectedType === "function" &&
expected.call( {}, actual ) === true
) {
expected = null;
result = true;
} 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 + ".";
}
}
}


currentTest.assert.pushResult( {
result,
actual,
Expand Down
25 changes: 25 additions & 0 deletions test/main/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ QUnit.test( "rejects", function( assert ) {
const rejectsReturnValue = assert.rejects(
buildMockPromise( "my error" )
);

assert.equal(
typeof rejectsReturnValue.then,
"function",
Expand Down Expand Up @@ -555,6 +556,30 @@ QUnit.test( "rejects", function( assert ) {
);

assert.rejects( null );

assert.rejects(
buildMockPromise( "foo" ),
2,
"rejects fails when provided a number"
);

assert.rejects(
buildMockPromise( "foo" ),
"string matcher",
"rejects fails when provided a number"
);

assert.rejects(
buildMockPromise( "foo" ),
false,
"rejects fails when provided a boolean"
);

assert.rejects(
buildMockPromise( "foo" ),
[],
"rejects fails when provided an array"
);
} );

( function() {
Expand Down

0 comments on commit af87708

Please sign in to comment.