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

How are you suppose to use .toThrow() ? #781

Closed
AdamKyle opened this issue Mar 8, 2016 · 6 comments
Closed

How are you suppose to use .toThrow() ? #781

AdamKyle opened this issue Mar 8, 2016 · 6 comments

Comments

@AdamKyle
Copy link

AdamKyle commented Mar 8, 2016

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

> toy-box@0.0.1 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 ...

@cpojer
Copy link
Member

cpojer commented Mar 8, 2016

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.

@cpojer cpojer closed this as completed Mar 8, 2016
@chriswininger
Copy link

@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

@abraham
Copy link

abraham commented Jul 15, 2018

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

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

QuickStyles added a commit to QuickStyles/AbacApe that referenced this issue Aug 17, 2018
- wrapped function in call function so toThrow() works properly. see jestjs/jest#781 & http://jasmine.github.io/2.0/introduction.html
@aksel
Copy link

aksel commented Aug 22, 2018

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.

@kadimi
Copy link

kadimi commented Sep 27, 2018

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

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants