Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jasmine: Why toBeUndefined and not.toBeDefined?

Tags:

jasmine

I'm just learning the Jasmine library, and I've noticed that Jasmine has a very limited number of built-in assertions. I've also noticed that, despite having such a limited number, two of its assertions appear to be redundant: toBeDefined/toBeUndefined.

In other words, both of these would seem to check for the same exact thing:

expect(1).toBeDefined();
expect(undefined).not.toBeUndefined();

Is there some reason for this, like a case where toBeDefined isn't the same as toBeUndefined? Or is this just the one assertion in Jasmine that has two perfectly equal ways of being invoked?

like image 588
machineghost Avatar asked Feb 05 '13 23:02

machineghost


1 Answers

One might assume the same for toBeTruthy and toBeFalsy, or toBeLessThan and toBeGreaterThan (although I guess the missing assert from the last two is toEqual). In the end it comes down to readability and user preference.

To give you a more complete answer, it might be useful to take a look at the code that is invoked for these functions. The code that is executed goes through separate paths (so toBeUndefined is not simply !toBeDefined). The only real answer that makes sense is readability (or giving in to annoying feature requests). https://github.com/jasmine/jasmine/tree/main/src/core/matchers

like image 166
DF_ Avatar answered Oct 12 '22 10:10

DF_