'Jest is throwing Error: expect(received).toThrow()

I have class Person constructor(state, uid). State can only have two values "active" and "inactive". this is how setState function looks like

setState(value) {

    if (value === "active" || "inactive") {
      this._state = value
    } else {
      throw new Error("error")
    }
  }

and Jest is throwing error

Error: expect(received).toThrow()

Received function did not throw

this is the line where error occurs

expect(() =>  p1.setState("bla")).toThrow();

How can I rewrite setState function, so that Jest registers the error.



Solution 1:[1]

You made an error in the if statement expression. This is how it should be:

if (value === 'active' || value === 'inactive') {

In your code, the right side of the or operator is a string constant, which will always evaluate to true.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 miklosme