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.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.