Jest: How are you suppose to use .toThrow() ?

Created on 8 Mar 2016  ·  5Comments  ·  Source: facebook/jest

I didn't know if there was a community forum or an active stack tag. So I thought I would post this here.

If we look at this function also shown below:

    resolve(name, fn) {
      const obj = this.fetch(name);

      if (typeof obj === 'string') {
        throw Error('object created must be a function. We could not resolve: ' + name);
      }

      return fn(obj);
    },

We can see that if obj is of type string that we can throw an error. So I though I could write a jest test as such:

  it ('should not resolve a container object', () => {
    const Foo = () => {
      let _message = '';

      return {
        set message(msg) {
          _message = msg;
        },

        get message() {
          return _message;
        }
      }
    }

    const toyBox = ToyBox();

    expect(toyBox.resolve('foo', function(obj){})).toThrow('object created must be a function. We could not resolve: foo');
  });

How ever when run:

npm test

> [email protected] test /Users/AdamBalan/Documents/toy-box
> jest

Using Jest CLI v0.9.0, jasmine2, babel-jest
 FAIL  __tests__/toy_box-test.js (0.073s)
● register a function › it should not resolve a container object
  - Error: object created must be a function. We could not resolve: foo
        at Error (native)
        at Object.resolve (src/toy_box/toy_box.js:207:15)
        at Object.eval (__tests__/toy_box-test.js:249:19)
1 test failed, 14 tests passed (15 total in 1 test suite, run time 2.607s)
npm ERR! Test failed.  See above for more details.

So I am confused. How do I test that an error was thrown with out actually throwing the error? If that makes sense? Like is there an issue with how I throw the error or with jest or my test ...

Most helpful comment

Thank you for creating an issue but please ask questions on stackoverflow or discord: facebook.github.io/jest/support.html

An example of toThrow can be found in the Jasmine documentation: http://jasmine.github.io/2.0/introduction.html

expect(() => {
  throw new Error();
}).toThrow();

you should wrap your resolve function call in another function that is passed to expect.

All 5 comments

Thank you for creating an issue but please ask questions on stackoverflow or discord: facebook.github.io/jest/support.html

An example of toThrow can be found in the Jasmine documentation: http://jasmine.github.io/2.0/introduction.html

expect(() => {
  throw new Error();
}).toThrow();

you should wrap your resolve function call in another function that is passed to expect.

@kopax apparently you need to do

it('should fail the insertTheme', () => { expect(() => themeReducer(state, insertThemeAction(bootstrap))).toThrow(); });

Not entirely obvious from the example given, but that resolved my issue

If you what you are testing is short, you can omit the {} and and have more concise form.

expect(() => throw new Error()).toThrow();

toThrow executes a function passed in expect, and verifies that it throws an error.

In this case, you're not passing expect a function. You're executing a function that throws an error. toThrow is never executed, and even if it were, it would throw an error, telling you that it should receive a function, but received something else instead.

expect(someFunctionThatThrows()) is essentially the same as expect(throw new Error()).

Sorry for the necrobumps 😅. Just wanted to let future people know why this happens.

Note: You must wrap the code in a function, otherwise the error will not be caught and the assertion will fail. -- https://jestjs.io/docs/en/expect#tothrowerror

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nsand picture nsand  ·  3Comments

gustavjf picture gustavjf  ·  3Comments

jardakotesovec picture jardakotesovec  ·  3Comments

ianp picture ianp  ·  3Comments

kentor picture kentor  ·  3Comments