A test reads a loan from the Shelf API and compares it with the loan it expects. Every field matches. The comparison says they are different.
const expected = { title: "Dune", daysLate: 0 };
const actual = { title: "Dune", daysLate: 0 };
console.log(actual === expected);
console.log(actual.title === expected.title);
console.log([1, 2] === [1, 2]);
false
true
false
For numbers, strings and booleans, === compares the values. For objects and arrays, it asks a different thing: are these the very same object?
A variable does not hold an object directly. It holds a reference, which is a pointer to where the object lives. expected and actual point at two separate objects that happen to have the same content. Two pointers to two objects are not equal.
The second line compares two strings, so it is true.
Two names for one object
Assigning an object to another variable copies the reference. It does not copy the object.
const loan = { title: "Dune", daysLate: 0 };
const sameLoan = loan;
sameLoan.daysLate = 3;
console.log(loan === sameLoan);
console.log(loan.daysLate);
true
3
loan and sameLoan point at one object. A change made through one name is visible through the other. This is how a helper can change test data that another test is about to use.
Comparing content
To compare what two objects contain, something has to go through the properties one by one. Assertion libraries do this for you. In Playwright:
import { test, expect } from '@playwright/test';
test('a loan from the API matches the expected loan', async () => {
const expected = { title: 'Dune', daysLate: 0 };
const actual = { title: 'Dune', daysLate: 0 };
expect(actual).toEqual(expected); // passes: same content
expect(actual).not.toBe(expected); // passes: two different objects
});
toBeasks whether two values are the same value. For objects that means the very same object.toEqualcompares content, going into nested objects and arrays.
Using toBe on two objects is a common first failure. The message shows two things that look identical, which is confusing until you know that toBe compared the references.
The shortcut that almost works
You will see people compare the JSON text of two objects.
const expected = { title: "Dune", daysLate: 0 };
const sameContent = { title: "Dune", daysLate: 0 };
const otherOrder = { daysLate: 0, title: "Dune" };
console.log(JSON.stringify(sameContent) === JSON.stringify(expected));
console.log(JSON.stringify(otherOrder) === JSON.stringify(expected));
true
false
JSON.stringify turns an object into text, and it writes the properties in the order they were added. otherOrder has the same content as expected and produces different text. A server may send its fields in any order, so this comparison can fail on a correct answer. Use toEqual, or compare the fields you care about one by one.
When you want the same object
Sometimes identity is the point. A test of a cache checks that the second call returns the very same object and not a fresh copy. A test of "the selected row" checks that the selection is the row from the list, not a row that looks like it. For those, === and toBe are the right tools.
So before you compare two objects, decide which of the two you mean: the same thing, or the same content.
Remember this
=== on two objects compares references, so two separate objects are never equal. Assigning an object gives a second name to the same object. Use toEqual for the same content and toBe for the very same object.
Reference: Equality comparisons and sameness on MDN.