Categories
JavaScript Answers

How to check object equality in Jasmine and JavaScript?

Spread the love

Sometimes, we want to check object equality in Jasmine and JavaScript.

In this article, we’ll look at how to check object equality in Jasmine and JavaScript.

How to check object equality in Jasmine and JavaScript?

To check object equality in Jasmine and JavaScript, we can use the toEqual method.

For instance, we write

describe("jasmine.objectContaining", () => {
  let foo;

  beforeEach(() => {
    foo = {
      a: 1,
      b: 2,
      bar: "baz",
    };
  });

  it("matches objects with the expect key/value pairs", () => {
    expect(foo).toEqual(
      jasmine.objectContaining({
        bar: "baz",
      })
    );
  });
});

to call toEqual in the it callback.

We call it with the value returned by jasmine.objectContaining which checks if the foo object has the properties we’re looking for.

Conclusion

To check object equality in Jasmine and JavaScript, we can use the toEqual method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *