Terry
Terry

Reputation: 14219

Does this violate any testing principles?

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

Answers (1)

Blender
Blender

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, and toNotContain have been deprecated and will be removed in a future release. Please change your specs to use not.toEqual, not.toBe, not.toMatch, and not.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

Related Questions