Skip to content

Instantly share code, notes, and snippets.

@wolframkriesing
Created February 22, 2016 23:33
Show Gist options
  • Save wolframkriesing/fa5bfd16e45af3cf5757 to your computer and use it in GitHub Desktop.
Save wolframkriesing/fa5bfd16e45af3cf5757 to your computer and use it in GitHub Desktop.
import assert from 'assert';
import {assertThat, instanceOf, throws} from 'hamjest';
class MyError extends Error {
constructor(...args) {
super(...args);
}
}
function throwingFunction() {
throw new MyError();
}
describe('catch my error', () => {
it('finds error', () => {
assertThat(throwingFunction, throws(instanceOf(MyError)));
});
});
@wolframkriesing
Copy link
Author

the output when running the tests

  catch my error
    1) finds error


  0 passing (494ms)
  1 failing

  1) catch my error finds error:

Expected: a function throwing an instance of MyError
     but: thrown object: {"stack":"Error\n    at MyError.Error (native)\n    at new MyError (test.js:4:7)\n    at throwingFunction (test.js:11:9)\n    at Object.IsFunctionThrowing._.create.describeMismatch (/Users/cain/programming/tmp/hamjest-testing/node_modules/hamjest/lib/matchers/IsFunctionThrowing.js:28:5)\n    at assertThat (/Users/cain/programming/tmp/hamjest-testing/node_modules/hamjest/lib/assertThat.js:28:11)\n    at Context.<anonymous> (test.js:17:5)\n    at callFn (/Users/cain/programming/tmp/hamjest-testing/node_modules/mocha/lib/runnable.js:315:21)\n    at Test.Runnable.run (/Users/cain/programming/tmp/hamjest-testing/node_modules/mocha/lib/runnable.js:308:7)\n    at Runner.runTest (/Users/cain/programming/tmp/hamjest-testing/node_modules/mocha/lib/runner.js:422:10)\n    at /Users/cain/programming/tmp/hamjest-testing/node_modules/mocha/lib/runner.js:533:12\n    at next (/Users/cain/programming/tmp/hamjest-testing/node_modules/mocha/lib/runner.js:342:14)\n    at /Users/cain/programming/tmp/hamjest-testing/node_modules/mocha/lib/runner.js:352:7\n    at next (/Users/cain/programming/tmp/hamjest-testing/node_modules/mocha/lib/runner.js:284:14)\n    at Immediate._onImmediate (/Users/cain/programming/tmp/hamjest-testing/node_modules/mocha/lib/runner.js:320:5)\n    at processImmediate [as _immediateCallback] (timers.js:368:17)"} is a Error
  AssertionError: 
  Expected: a function throwing an instance of MyError
      at Context.<anonymous> (test.js:17:5)

I would have expected the test to pass, because throwingFunction does throw MyError, but the tests fails :(

@rluba
Copy link

rluba commented Feb 23, 2016

Interesting… your code seems correct.
Can you open a ticket with more details about your environment, please? We can investigate it there.

@rluba
Copy link

rluba commented Feb 23, 2016

Must be the ES6 class syntax. Seems like new MyError() instanceOf MyError is falsy — odd.

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