30/05/2016 - 1 minute read

Comparing Objects in Chai Doesn't Work as Expected?

Written by

Gary Lewis Cheetham

Automotive marketing specialist from Manchester, UK

Here's the gotcha of all gotchas.

If you compare two objects in Chai with .equal, you're not going to get what you expected.

These two objects are clearly equal, however the test fails:

× should equal
  PhantomJS 2.1.1 (Windows 8 0.0.0)
expected { Object (name, species, ...) } to equal { Object (name, species, ...) }

What's going wrong here? Use to.deep.equal. An equivalent method is to.eql, but I personally find the latter more readable.

When comparing objects, Chai needs to know that it must traverse the objects and compare nested properties. That's why the deep flag is needed for object comparison.

I hope you enjoyed this article, if you have anything you'd like to add or would otherwise like to get in touch, you can do so on twitter @glcheetham.

About the author

Gary Lewis Cheetham is an automotive marketing specialist, born and raised in Oldham, Greater Manchester. He spent his teenage years working at the family dealership after school, learning the ropes of the car trade from the inside out.

After moving on to working in marketing, Gary Lewis founded GL Digital in 2020 when he noticed a need for direct-to-consumer marketing platforms within the auto industry. He now strives every day to help independent dealers in the UK and US realise their potential. Gary also loves Formula 1 and motorsport.

More Articles

SEO For Car Dealerships: Everything You Need to Know

SEO For Car Dealerships: Everything You Need to Know

We will be the first to say it: SEO for car dealerships is officially over in 2022. Car dealers fail at SEO time and time again, because…

5 minute read - 07/02/2022

Everything you need to know about Conversion Rate Optimisation for Car dealerships in 2022

Everything you need to know about Conversion Rate Optimisation for Car dealerships in 2022

This article will cover how dealerships can do conversion rate optimisation on their website and sales funnels, including for lead…

6 minute read - 27/01/2022

How to run successful Email Marketing for Car Dealerships in 2022

How to run successful Email Marketing for Car Dealerships in 2022

Email marketing is one of the most effective digital marketing strategies for car dealerships to build relationships with customers. It…

5 minute read - 27/01/2022

See More