Chapter 14: TDD in Action (only deepStrictEqual works for Feature 5)

I completed the unit testing project using TDD, the last feature was tricky.

  1. Allow for multiple rawData values
    • rawData would be returned as an array of numbers
    • Get the new test working and then fix any broken existing tests
    • Example Transmission: "9701::<21212.232323.242424>"
    • Result: { id: 9701, rawData: [21212,232323,242424] }

When I implemented my solution, the program returned this error message:

AssertionError [ERR_ASSERTION]: Input objects identical but not reference equal

assert.strictEqual and assert.equal return the same error. When I tried assert.deepStrictEqual, it seemed to work. I’ve looked at Stack Overflow and other websites about it, but I still don’t fully understand it.

It sounds like you are comparing two different variables/objects which may have the same value - but they are obviously not one and the same object/variable. It’s hard to be more specific without seeing your assert statements.

I’m comparing two different values which have the same value. I thought the solution required rawData to be returned as an array of numbers instead of an array of strings.

In my processor.js file, I convert rawData into an array of strings if the "." is present. Then, loop through the array to check for "<" and ">" symbols. After removing the symbol, I convert the string of numbers at the current index to numbers. Once each value is a number in the array, I return it in the object.

My assert statement right now is this:

  it ("takes array of rawData values returns object", function() {
    let result = processor("9701::<21212.232323.242424>");
    assert.deepStrictEqual(result.rawData, [21212, 232323, 242424]);
  });

OK, I see it now. There is no problem in your code. You just needed someone to help you understand the test results.

  1. Let’s knock out the easy one first: assert.equal is deprecated and is simply an alias for assert.strictEqual. So, they will be returning the same test result. No differences to worry about.

  2. So, the real question is: Why and how are assert.strictEqual and assert.deepStrictEqual different? In your particular case, result.rawData and [21212, 232323, 242424] are both arrays. They are two different array variables which are stored in two different memory locations. “Reference” is a pointer to a memory location. Thus, “not reference equal” just confirms that the two variables are stored in two different places.
    But they happen to have the same value (the two variables both contain [21212, 232323, 242424]). So, “Input objects identical”.

Same value, different references. “Deep” equality compares the values (contents) “deeply” inside the variables/objects. Shallow equality compares their references.

Because you want to be comparing the values (and not their places in memory), in your test you should indeed be checking for deepStrictEqual result.

I know this is a lot, but I am hoping it makes sense. :sweat_smile:

1 Like

Thank you for your feedback! It is overwhelming to think about. But, I’m beginning to understand the difference between deepStrictEqual and strictEqual now.

This statement was especially helpful:

Yes, this is the gist of it, for non-primitive (reference) types. The text discusses assert.strictEqual in 14.2.2.4. Specifications and Assertions (where they also point to 5.2. Equality), but they really don’t explain the difference for non-primitive types.

Think of two brand new plain white, non-descript Honda Civics which just rolled off the assembly line. They are the “same” / identical in “shallow” sense (and even more so from 50 feet away). But they are two distinct / not the same items in physical, “deep” sense. (I am not aiming for a perfect analogy here :grinning: ).

EDIT: @ArtisticAng Actually, check out 12.3.1. Booleans and Objects. They talk about values and references there.