Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assert: Allow fat arrow functions in throws expected handler #1492

Merged
merged 2 commits into from
Nov 5, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ module.exports = function( grunt ) {
"test/module-only",
"test/module-skip",
"test/module-todo",
"test/es2017/async-functions"
"test/es2017/async-functions",
"test/es2017/throws"
]
},
"watch-repeatable": {
Expand Down
3 changes: 2 additions & 1 deletion src/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,8 @@ class Assert {
expected = String( expected );

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

// Expected is an Error object
Expand Down
46 changes: 46 additions & 0 deletions test/es2017/throws.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@

QUnit.module( "assert" );

function CustomError( message ) {
this.message = message;
}

CustomError.prototype.toString = function() {
return this.message;
};

QUnit.test( "throws", function( assert ) {
assert.expect( 1 );

assert.throws(
function() {
throw new CustomError( "some error description" );
},
err => {
return err instanceof CustomError && /description/.test( err );
},
"custom validation function"
);
} );

QUnit.module( "failing assertions", {
beforeEach: function( assert ) {
var originalPushResult = assert.pushResult;
assert.pushResult = function( resultInfo ) {

// Inverts the result so we can test failing assertions
resultInfo.result = !resultInfo.result;
originalPushResult( resultInfo );
};
}
} );

QUnit.test( "throws", function( assert ) {
assert.throws(
function() {
throw "foo";
},
() => false,
"throws fail when expected function returns false"
);
} );
10 changes: 10 additions & 0 deletions test/main/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,16 @@ QUnit.test( "throws", function( assert ) {
/bar/,
"throws fail when regexp doesn't match the error message"
);

assert.throws(
function() {
throw "foo";
},
function() {
return false;
},
"throws fail when expected function returns false"
);
} );

QUnit.test( "rejects", function( assert ) {
Expand Down