Lightbulb: Equality

Oliver Stigley

16 April 2016

While learning some javascript testing frameworks this week (Tape), I noted that one of my tests was failing persistently, no matter what I did:

test('make Friendly Dates', function (t) {
	t.equal( makeFriendlyDates(["2017-03-01", "2017-05-05”]), ["March 1st, 2017","May 5th”] );
	t.end();
});

The first function makeFriendlyDates was producing the right output, but my test was failing. Why? Because javascript cannot test the equity of two arrays, that’s why. For example [1,2,3] == [1,2,3] will return false. To test array equality, you need to evaluate each index against each other. As a quick solution, I evaluated array.join(“ “) == array.join(“ “), turning my arrays in to strings, which then evaluated true.

Boom! Each time I learn a new secret that seems so obvious, I get really excited. The alternative negative approach of “how the hell did I not know about that? I’m so stupid” is al too easy to fall in to. Lightbulb moments are amazing!

[]==[]