Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

protractor- difference between toBe(truth) and toBeTruthy()

as the title says- is there a difference between (for example)

 expect(element).isDisplayed().toBeTruthy();

and

 expect(element).isDisplayed().toBe(truth);

and if so what is the difference?

thanks

like image 240
user2880391 Avatar asked Jan 08 '23 20:01

user2880391


2 Answers

Many things are Truthy (i.e. anything that is not one of: false, 0, "", undefined, null, NaN). So

expect('apple').toBeTruthy();

passes. But:

expect('apple').toBe(true);

fails.

That being said, if you know you are testing a boolean, to me using toBeTruthy looks nicer.

like image 134
hankduan Avatar answered Jan 31 '23 19:01

hankduan


Even though this is not a new question I thought it deserves an exact answer.

expect('apple').toBe(true);

evaluates to 'apple' === true. (actual === expected in the jasmine code) I would prefer

expect('apple').toBe('apple');

That way you know you are not getting an 'orange'. On the other hand.

expect('apple').toBeTruthy();

evaluate 'apple' as !!'apple' (!!actual in the jasmine code). So not not 'apple'

like image 31
jamesRH Avatar answered Jan 31 '23 18:01

jamesRH