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

Already on GitHub? Sign in to your account

Allow use of a predicate function for validation of thrown exception #76

wants to merge 2 commits into


None yet
5 participants

gtanner commented Mar 16, 2011

We had a case where the validation of our exception was more than just a comparison on the error.message parameter.

We need to validate that our methods threw a specific exception type on the following spec:

Since error.type is not a standard field (like Microsoft's error.description or Mozilla's error.lineNumber) we didn't want to add a check natively into the test runner.

The most extensible way would be to allow us to use a predicate function when validating exceptions to allow for people to test for browser or runtime specific fields on the error.


infews commented Jun 19, 2011

Interesting. I think I get it from the spec - thanks. But do you have an example of this in real use so I can understand the use case better?

gtanner commented Jun 20, 2011

We write a mobile phone emulator that runs in the browser:

One of the platforms we emulate (WAC) throws a specific type of exception. We wanted to ensure that our emulation of this exception was correct and the only way to do that was to look at the type property of the exception instance.

it("does bad stuff", function () {
    expect(foo.bar).toThrow(function (e) {

it("disables the connection on error", function () {
     expect(crash.me).toThrow(function (e) {

This seemed like a good general purpose addition for anyone who is testing non standard exception types, for example the microsoft or mozilla properties that are added to the exception objects. Or if you need to do some more state checking on the system when an exception is thrown.

+1 for this, I would allow regexps as well... strict matching of the exception message is not flexible enough...


infews commented Nov 4, 2011

We've added a Tracker story for this: https://www.pivotaltracker.com/story/show/20622765

I would also like to see both a predicate and regexps support.


ghost commented Jan 15, 2013

Stephen - do you have an example of what you'd like to see?

On Tue, Jan 15, 2013 at 8:03 AM, Stephen Duncan Jr <notifications@github.com


I would also like to see both a predicate and regexps support.

Reply to this email directly or view it on GitHubhttps://github.com/pivotal/jasmine/pull/76#issuecomment-12274181.


My immediate problem would be solved by the ability to use a regex, so that expect(function() { throw "Bad stuff dude";}).toThrow(/Bad/) would pass. For a full solution, I think Jasmine's matcher should match the flexibility of other frameworks out there: http://chaijs.com/api/bdd/ allows "a specific error, or specific type of error (as determined using instanceof), optionally with a RegExp or string inclusion test for the error's message" & https://github.com/LearnBoost/expect.js throwException allows regex matching or passing a function that can be used to make expect calls on the error.


infews commented Mar 6, 2013

If this is still of interest, can you re-do this commit on top of master? We've not revisited Matchers yet so this work should be easy to re-apply. Thanks for helping make Jasmine awesome.


infews commented May 26, 2013

There is a matchers_redo branch. Matchers have been extensively re-written.

If you are still interested to rebase this on top of this branch, look at src/core/matchers.js where the try/catch block is.

If you aren't interested, then we can treat this as a feature request and I'll re-write it per the new matchers code.

Either way, this should make it in to 2.0.


slackersoft commented Oct 2, 2013

You can follow the story on tracker here : https://www.pivotaltracker.com/story/show/20622765

@slackersoft slackersoft closed this Oct 2, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment