42

Here is my error codes:

 FAIL  build/__test__/FuncOps.CheckFunctionExistenceByString.test.js
  ● 
    expect(CheckFunctionExistenceByStr(
      'any string', 'FunctionThatDoesNotExistsInString'
    )).toThrow();


    Function FunctionThatDoesNotExistsInString does not exists in string.

      at CheckFunctionExistenceByStr (build/FuncOps.js:35:15)
      at Object.<anonymous> (build/__test__/FuncOps.CheckFunctionExistenceByString.test.js:12:51)
          at new Promise (<anonymous>)
          at <anonymous>

As you can see the error did indeed occurred: Function FunctionThatDoesNotExistsInString does not exists in string.. However it is not captured as a pass in Jest.

Here is my codes:

test(`
    expect(CheckFunctionExistenceByStr(
      'any string', 'FunctionThatDoesNotExistsInString'
    )).toThrow();
  `, () => {
    expect(CheckFunctionExistenceByStr(
      'any string', 'FunctionThatDoesNotExistsInString'
    )).toThrow();
  }
);
Michael Jungo
  • 31,583
  • 3
  • 91
  • 84
notalentgeek
  • 4,939
  • 11
  • 34
  • 53

4 Answers4

48

expect(fn).toThrow() expects a function fn that, when called, throws an exception.

However you are calling CheckFunctionExistenceByStr immediatelly, which causes the function to throw before running the assert.

Replace

test(`
    expect(CheckFunctionExistenceByStr(
      'any string', 'FunctionThatDoesNotExistsInString'
    )).toThrow();
  `, () => {
    expect(CheckFunctionExistenceByStr(
      'any string', 'FunctionThatDoesNotExistsInString'
    )).toThrow();
  }
);

with

test(`
    expect(() => {
      CheckFunctionExistenceByStr(
        'any string', 'FunctionThatDoesNotExistsInString'
      )
    }).toThrow();
  `, () => {
    expect(() => {
      CheckFunctionExistenceByStr(
        'any string', 'FunctionThatDoesNotExistsInString'
      )
    }).toThrow();
  }
);
Maluen
  • 1,753
  • 11
  • 16
  • 1
    This helps: https://facebook.github.io/jest/docs/en/expect.html#tothrowerror -- Encapsulate the method you are actually want to test inside another function. Place that outer function inside expect() without calling it (no trailing parenthesis). – fuma Mar 23 '18 at 15:08
40

Jest needs a callback function here. Instead of passing the function definition like this:

expect(yourFunction(args)).toThrow(ErrorTypeOrErrorMessage)

Do this:

expect(() => yourFunction(args)).toThrow(ErrorTypeOrErrorMessage)

If you don't need to pass arguments you can also do:

expect(yourFunction).toThrow(ErrorTypeOrErrorMessage)

And for asynchronous functions:

expect(async () => await yourFunction(args)).rejects.toThrow(ErrorTypeOrErrorMessage)
leonheess
  • 16,068
  • 14
  • 77
  • 112
8

had a similar issue but my function to be tested is async and hence your expect have to look like this:

await expect(async () => {await myFunctionToBeTested();}).rejects.toThrow('My Error message')

the solution I got from here

Note:

the await before expect I added 'cause Eslint was complaining but would work without it.

kap
  • 81
  • 1
  • 2
0

For me, the problem was that the function I was testing was a getter that was immediately executed even when omitting the (). Wrapping the getter in a closure (() => {myClass.get}) fixed the problem.

Niek
  • 1,464
  • 1
  • 17
  • 27