Lea Hayes
Lea Hayes

Reputation: 64196

Does assert.equal offer any advantages over assert (assert.ok)?

In this question I refer to the assert module that is included within the node.js core.

As far as I can tell the following two assertions are pretty much identical:

assert.equal(typeof path, "string", "argument 'path' must be a string");
assert(typeof path === "string", "argument 'path' must be a string");

Upon failure both variations report the same message:

AssertionError: argument 'path' must be a string

Are there any notable advantages of the former over the latter in this situation?

Upvotes: 1

Views: 1546

Answers (3)

Ludovic C
Ludovic C

Reputation: 3065

Both will work.

First, assert uses the coercive == operator, not strict ===

Also, when you read a lot of your unit tests or other people's unit tests, you'll strain your eyes on repetitive syntax. You'll love it when people will write this

assert.equal(aValue, anotherValue) // sexy

/** But you will hate people writing this. **/
assert.ok(aValue == anotherValue) // ugly

In the first case, you can see the condition being checked within the first 9 letters. You don't even need to look further. In the other case, you have to read 20 letters to know what the test is checking. It's more cryptic.

Also, assert.equal is more declarative of your intention than assert.ok.

Imagine you are writing a test for testing a set intersection. You would read better

assert.setIntersect(set1, set2) // wow

assert.ok(setIntersect(set1, set2)); // hm.

To sum it up, the advantage is in readability (thus maintainability) of you unit tests. It's not much, but it helps writing better understandable code.

As alexander said, too, you will have more precise error messages when test fail if you don't specify a message.

Upvotes: 1

alexander.biskop
alexander.biskop

Reputation: 1862

Well, depending on the test runner framework, assert.equal will probably give you a more descriptive error message. For instance, in this case:

assert.equal(typeof path, "string");
assert(typeof path === "string");

the first statement would give you a message along the lines of:

actual: number
expected: string

which already tells you that the test case fails because typeof path is a number. The latter will only print something like this:

AssertionError: false == true

Also, note that if you want to check for strict equality (===), you should use assert.strictEqual instead of assert.equal.

Upvotes: 5

Blender
Blender

Reputation: 298076

assert.equal doesn't check for identity, only equality. It's equivalent to:

assert(typeof path == 'string', "argument 'path' must be a string");

The real equivalent would be assert.strictEqual, which uses the identity operator ===:

assert.strictEqual(typeof path, "string", "argument 'path' must be a string");

For typeof, no, there's no difference. You'll run into problems with other data types though:

> assert.equal('test', ['test']);
undefined
> 'test' == ['test']
true
> 'test' === ['test']
false

Upvotes: 1

Related Questions