FAQ: Write Expressive Tests - assert.deepEqual I

This community-built FAQ covers the “assert.deepEqual I” exercise from the lesson “Write Expressive Tests”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Learn JavaScript Unit Testing

FAQs on the exercise assert.deepEqual I

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hi, could someone please clarify where does sum.results comes? It makes no sense to me.

let expected = {a: 3, b: 4, result: 7};
		let sum = {a: 3, b: 4};

    // Exercise
		**sum.result** = sum.a + sum.b;

    // Verify
    assert.deepEqual(sum, expected);
1 Like

sum.result = sum.a + sum.b; adds result property to sum object. So now you can assert your objects are the same.

5 Likes

Thanks, it would have thought it needed the .push() method to add it into the sum object, but I must be confusing it with an array. Seems to work just fine like this.

Just for fun, I tried another one:

describe('+', () => {
  it('returns the sum of two values', () => {
    // Setup
		let expected = {a: 3, b: 4, result: 7, substract: -1};
		let sum = {a: 3, b: 4};

    // Exercise
		sum.result = sum.a + sum.b;
		sum.substract = sum.a - sum.b;

    // Verify
    assert.deepEqual(sum, expected);
  });
});

Test passed: indeed, properties can be added to objects seamlessly using this method.

yea, if we look at some documentation:

Working with objects - JavaScript | MDN

we can see that objects are also associative arrays. so we could also add keys using:

sum['result'] = sum.a + sum.b;
1 Like

What is the difference between assert.equal() and assert.deepEqual()?

In the exercise before this one, it states this:

  • assert.equal() performs a == comparison.

Then for the assert.deepEqual() it states this:

  • This method compares the values of each object using loose ( == ) equality.

So what exactly is the difference between these two?

Its explained in the exercise:

using .equal to compare strings and so forth is fine. But won’t work for objects. Which is why we use deepEqual, which very likely uses a loop to compare all the items within the object

1 Like

Sorry I’m late to the party. This goes back to the reference over value section we had gone over. Objects are referenced by location. So with both objects having the same key value pairs they are considered different in nature. It still seems weird, but the deep equal does go deep into the object itself to extract the actual value to compare against. Whereas the .equal and .strictEqual are comparing the object, it will always return Falsey because they are different objects within memory.

2024 update, NodeJS docs describes assert.deepEqual() as legacy and recommends assert.deepStrictEqual() instead of that.