Search code examples
javascripthtmlunit-testingassertionsjsunit

What does assertEquals() in JSUnit actually do?


I am doing some unit test exercises using a HTML5/JS game I wrote and JSUnit test runner. I am satisfied by the simplicity of it but that simplicity goes even in the docs as there is no explanation what trully assertEquals() does.

I made a stub aka fake object of my preloader and I want to check its state against this fake preloader.

I used assertEquals(gamePreloader, myPreloader) but the test fails with error:

Expected <[object Object]> (Object) but was <[object Object]> (Object)

Meaning they are not the same objects.

Does assertEquals() checks for the state of the objects or does it checks in memory that those two objects are actually the same object?


Solution

  • The assertEquals will perform an equal-equal-equal operation, so it will do:

     gamePreloader === myPreloader
    

    In that case, it will only return true if the objects are exactly the same, and not if they have the same values. If you want to test some object's property's value, you must specifically test it, e.g:

    assertEquals(gamePreloader.status, myPreloader.status)
    

    And if you want to test all the values, then you will need a loop, or something like that:

    Object.keys(gamePreloader).forEach(function(key) {
      assertEquals(gamePreloader[key], myPreloader[key]);
    });