admin管理员组文章数量:1134247
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 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();
Share
Improve this question
asked Sep 16, 2015 at 18:10
alecxealecxe
473k126 gold badges1.1k silver badges1.2k bronze badges
3
|
5 Answers
Reset to default 341 +100What I do when I wonder something like the question asked here is go to the source.
toBe()
expect().toBe()
is defined as:
function toBe() {
return {
compare: function(actual, expected) {
return {
pass: actual === expected
};
}
};
}
It performs its test with ===
which means that when used as expect(foo).toBe(true)
, it will pass only if foo
actually has the value true
. Truthy values won't make the test pass.
toBeTruthy()
expect().toBeTruthy()
is defined as:
function toBeTruthy() {
return {
compare: function(actual) {
return {
pass: !!actual
};
}
};
}
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 to expect
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 like
> "hello" == true
false
> "" == true
false
> [] == true
false
> [1, 2, 3] == true
false
Whereas using !!
yields:
> !!"hello"
true
> !!""
false
> !![1, 2, 3]
true
> !![]
true
(Yes, empty or not, an array is truthy.)
toBeTrue()
expect().toBeTrue()
is part of Jasmine-Matchers (which is registered on npm as jasmine-expect
after a later project registered jasmine-matchers
first).
expect().toBeTrue()
is defined as:
function toBeTrue(actual) {
return actual === true ||
is(actual, 'Boolean') &&
actual.valueOf();
}
The difference with expect().toBeTrue()
and expect().toBe(true)
is that expect().toBeTrue()
tests whether it is dealing with a Boolean
object. expect(new Boolean(true)).toBe(true)
would fail whereas expect(new Boolean(true)).toBeTrue()
would pass. This is because of this funny thing:
> new Boolean(true) === true
false
> new Boolean(true) === false
false
At least it is truthy:
> !!new Boolean(true)
true
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 a boolean
. 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.
Disclamer: This is just a wild guess
I know everybody loves an easy-to-read list:
toBe(<value>)
- The returned value is the same as<value>
toBeTrue()
- Checks if the returned value istrue
toBeTruthy()
- Check if the value, when cast to a boolean, will be a truthy valueTruthy values are all values that aren't
0
,''
(empty string),false
,null
,NaN
,undefined
or[]
(empty array)*.* Notice that when you run
!![]
, it returnstrue
, but when you run[] == false
it also returnstrue
. It depends on how it is implemented. In other words:(!![]) === ([] == false)
On your example, toBe(true)
and toBeTrue()
will yield the same results.
In javascript there are trues and truthys. When something is true it is obviously true or false. When something is truthy it may or may not be a boolean, but the "cast" value of is a boolean.
Examples.
true == true; // (true) true
1 == true; // (true) truthy
"hello" == true; // (true) truthy
[1, 2, 3] == true; // (true) truthy
[] == false; // (true) truthy
false == false; // (true) true
0 == false; // (true) truthy
"" == false; // (true) truthy
undefined == false; // (true) truthy
null == false; // (true) truthy
This can make things simpler if you want to check if a string is set or an array has any values.
var users = [];
if(users) {
// this array is populated. do something with the array
}
var name = "";
if(!name) {
// you forgot to enter your name!
}
And as stated. expect(something).toBe(true)
and expect(something).toBeTrue()
is the same. But expect(something).toBeTruthy()
is not the same as either of those.
As you read through the examples below, just keep in mind this difference
true === true // true
"string" === true // false
1 === true // false
{} === true // false
But
Boolean("string") === true // true
Boolean(1) === true // true
Boolean({}) === true // true
1. expect(statement).toBe(true)
Assertion passes when the statement passed to expect()
evaluates to true
expect(true).toBe(true) // pass
expect("123" === "123").toBe(true) // pass
In all other cases cases it would fail
expect("string").toBe(true) // fail
expect(1).toBe(true); // fail
expect({}).toBe(true) // fail
Even though all of these statements would evaluate to true
when doing Boolean()
:
So you can think of it as 'strict' comparison
2. expect(statement).toBeTrue()
This one does exactly the same type of comparison as .toBe(true)
, but was introduced in Jasmine recently in version 3.5.0
on Sep 20, 2019
3. expect(statement).toBeTruthy()
toBeTruthy
on the other hand, evaluates the output of the statement into boolean first and then does comparison
expect(false).toBeTruthy() // fail
expect(null).toBeTruthy() // fail
expect(undefined).toBeTruthy() // fail
expect(NaN).toBeTruthy() // fail
expect("").toBeTruthy() // fail
expect(0).toBeTruthy() // fail
And IN ALL OTHER CASES it would pass, for example
expect("string").toBeTruthy() // pass
expect(1).toBeTruthy() // pass
expect({}).toBeTruthy() // pass
There are a lot many good answers out there, i just wanted to add a scenario where the usage of these expectations might be helpful. Using element.all(xxx)
, if i need to check if all elements are displayed at a single run, i can perform -
expect(element.all(xxx).isDisplayed()).toBeTruthy(); //Expectation passes
expect(element.all(xxx).isDisplayed()).toBe(true); //Expectation fails
expect(element.all(xxx).isDisplayed()).toBeTrue(); //Expectation fails
Reason being .all()
returns an array of values and so all kinds of expectations(getText
, isPresent
, etc...) can be performed with toBeTruthy()
when .all()
comes into picture. Hope this helps.
本文标签: javascripttoBe(true) vs toBeTruthy() vs toBeTrue()Stack Overflow
版权声明:本文标题:javascript - toBe(true) vs toBeTruthy() vs toBeTrue() - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736758402a1951411.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
.toBe(true)
==.toBeTrue()
. toBeTruthy() can be true not only upon true, but upon 123, "dfgdfg", [1,2,3], etc... basicallyif(x==true)
are truthy, whileif(x===true)
are true true. – dandavis Commented Sep 16, 2015 at 18:14toBeTruthy
if you are unsure of the type it is the same as== true
while I suspect.toBe(true)
is the same as=== true
Mind you its a little overboard to call a function to test for true. Word of advice,. Forget==
and!=
exists in Javascript and never use it again. Truthy is not needed and a trap for beginers. Use===
and!==
instead. – Blindman67 Commented Sep 16, 2015 at 18:20eslint
reporting us if==
or!=
are used suggesting to change it to===
and!==
. – alecxe Commented Sep 23, 2015 at 12:33