toBe(true) vs toBeTruthy() vs toBeTrue()

后端 未结 4 1115
隐瞒了意图╮
隐瞒了意图╮ 2021-01-29 19:12

What is the difference between expect(something).toBe(true), expect(something).toBeTruthy() and expect(something).toBeTrue()?

Note

4条回答
  •  南方客
    南方客 (楼主)
    2021-01-29 19:32

    Disclamer: This is just a wild guess

    I know everybody loves an easy-to-read list:

    • toBe() - The returned value is the same as
    • toBeTrue() - Checks if the returned value is true
    • toBeTruthy() - Check if the value, when cast to a boolean, will be a truthy value

      Truthy values are all values that aren't 0, '' (empty string), false, null, NaN, undefined or [] (empty array)*.

      * Notice that when you run !![], it returns true, but when you run [] == false it also returns true. It depends on how it is implemented. In other words: (!![]) === ([] == false)


    On your example, toBe(true) and toBeTrue() will yield the same results.

提交回复
热议问题