What is the difference between expect(something).toBe(true)
, expect(something).toBeTruthy()
and expect(something).toBeTrue()
?
Note that toBeTrue()
is a custom matcher introduced in jasmine-matchers
among other useful and handy matchers like toHaveMethod()
or toBeArrayOfStrings()
.
The question is meant to be generic, but, as a real-world example, I'm testing that an element is displayed in protractor
. Which matcher should I use in this case?
expect(elm.isDisplayed()).toBe(true);
expect(elm.isDisplayed()).toBeTruthy();
expect(elm.isDisplayed()).toBeTrue();
What I do when I wonder something like the question asked here is go to the source.
toBe()
expect().toBe()
is defined as:It performs its test with
===
which means that when used asexpect(foo).toBe(true)
, it will pass only iffoo
actually has the valuetrue
. Truthy values won't make the test pass.toBeTruthy()
expect().toBeTruthy()
is defined as:Type coercion
A value is truthy if the coercion of this value to a boolean yields the value
true
. The operation!!
tests for truthiness by coercing the value passed toexpect
to a boolean. Note that contrarily to what the currently accepted answer implies,== true
is not a correct test for truthiness. You'll get funny things likeWhereas using
!!
yields:(Yes, empty or not, an array is truthy.)
toBeTrue()
expect().toBeTrue()
is part of Jasmine-Matchers (which is registered on npm asjasmine-expect
after a later project registeredjasmine-matchers
first).expect().toBeTrue()
is defined as:The difference with
expect().toBeTrue()
andexpect().toBe(true)
is thatexpect().toBeTrue()
tests whether it is dealing with aBoolean
object.expect(new Boolean(true)).toBe(true)
would fail whereasexpect(new Boolean(true)).toBeTrue()
would pass. This is because of this funny thing:At least it is truthy:
Which is best suited for use with
elem.isDisplayed()
?Ultimately Protractor hands off this request to Selenium. The documentation states that the value produced by
.isDisplayed()
is a promise that resolves to aboolean
. I would take it at face value and use.toBeTrue()
or.toBe(true)
. If I found a case where the implementation returns truthy/falsy values, I would file a bug report.