Reputation: 14219
I'm using Jasmine.js to write JS unit tests, unsure if this type of code violates a testing principle of any kind:
expect(someObject).toNotBe(undefined || null);
as opposed to
expect(someObject).toNotBe(undefined);
expect(someObject).toNotBe(null);
Even though null
and undefined
are different, for the purposes of my test I don't (think I) care which one it is.
Upvotes: 0
Views: 351
Reputation: 298226
undefined || null
returns null
, as undefined
is falsy:
> undefined || null
null
Your first example is actually equivalent to the second line of your second example, which is:
expect(someObject).toNotBe(null);
Also, toNotBe
is deprecated:
The old matchers
toNotEqual
,toNotBe
,toNotMatch
, andtoNotContain
have been deprecated and will be removed in a future release. Please change your specs to usenot.toEqual
,not.toBe
,not.toMatch
, andnot.toContain
respectively.
You may want to check for equality (not identity!) with null
, as false != null
, but undefined == null
:
expect(someObject).not.toEqual(null);
If someObject
being false
, 0
, []
, etc. is also undesirable, you could also do:
expect(someObject).toBeTruthy();
Otherwise, you should write your own matcher.
Upvotes: 3