Jasmine JavaScript Testing - toBe vs toEqualUpdate

2022-08-29 23:24:26

Let's say I have the following:

var myNumber = 5;
expect(myNumber).toBe(5);
expect(myNumber).toEqual(5);

Both of the above tests will pass. Is there a difference between and when it comes to evaluating numbers? If so, when I should use one and not the other?toBe()toEqual()


答案 1

For primitive types (e.g. numbers, booleans, strings, etc.), there is no difference between and ; either one will work for , , or .toBetoEqual5true"the cake is a lie"

To understand the difference between and , let's imagine three objects.toBetoEqual

var a = { bar: 'baz' },
    b = { foo: a },
    c = { foo: a };

Using a strict comparison (), some things are "the same":===

> b.foo.bar === c.foo.bar
true

> b.foo.bar === a.bar
true

> c.foo === b.foo
true

But some things, even though they are "equal", are not "the same", since they represent objects that live in different locations in memory.

> b === c
false

Jasmine's matcher is nothing more than a wrapper for a strict equality comparisontoBe

expect(c.foo).toBe(b.foo)

is the same thing as

expect(c.foo === b.foo).toBe(true)

Don't just take my word for it; see the source code for toBe.

But and represent functionally equivalent objects; they both look likebc

{ foo: { bar: 'baz' } }

Wouldn't it be great if we could say that and are "equal" even if they don't represent the same object?bc

Enter , which checks "deep equality" (i.e. does a recursive search through the objects to determine whether the values for their keys are equivalent). Both of the following tests will pass:toEqual

expect(b).not.toBe(c);
expect(b).toEqual(c);

Hope that helps clarify some things.


答案 2

toBe() versus : checks equivalence. , on the other hand, makes sure that they're the exact same object.toEqual()toEqual()toBe()

I would say use when comparing values, and when comparing objects.toBe()toEqual()

When comparing primitive types, and will yield the same result. When comparing objects, is a stricter comparison, and if it is not the exact same object in memory this will return false. So unless you want to make sure it's the exact same object in memory, use for comparing objects.toEqual()toBe()toBe()toEqual()

Check this link out for more info : http://evanhahn.com/how-do-i-jasmine/

Now when looking at the difference between and when it comes to numbers, there shouldn't be any difference so long as your comparison is correct. will always be equivalent to .toBe()toEqual()55

A nice place to play around with this to see different outcomes is here

Update

An easy way to look at and is to understand what exactly they do in JavaScript. According to Jasmine API, found here:toBe()toEqual()

toEqual() works for simple literals and variables, and should work for objects

toBe() compares with ===

Essentially what that is saying is and are similar Javascripts operator except is also checking to make sure it is the exact same object, in that for the example below as well. However, will return true in that situation.toEqual()toBe()===toBe()objectOne === objectTwo //returns falsetoEqual()

Now, you can at least understand why when given:

var objectOne = {
    propertyOne: str,
    propertyTwo: num    
}

var objectTwo = {
    propertyOne: str,
    propertyTwo: num    
}

expect(objectOne).toBe(objectTwo); //returns false

That is because, as stated in this answer to a different, but similar question, the operator actually means that both operands reference the same object, or in case of value types, have the same value.===