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?
One might assume the same for
toBeTruthyandtoBeFalsy, ortoBeLessThanandtoBeGreaterThan(although I guess the missing assert from the last two istoEqual). 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
toBeUndefinedis 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