1

I have seen some helpful posts on equal and deepEqual, but I still don't understand why this mocha test is failing.

describe('remove(thing)', function(){
    it('should remove the word from the wordlist if it is there', function(){
        var wordlist1 = new Wordlist('unit1');
        wordlist1.add("bird");
        wordlist1.add("elephant");
        wordlist1.add("cat");
        wordlist1.add("dog");
        assert.equal(wordlist1.find("bird"), 0);      //passes
        wordlist1.remove('bird');
        assert.equal(wordlist1.words[0], "elephant"); //passes
        assert.equal(wordlist1.words[1], "cat");      //passes
        assert.equal(wordlist1.words[2], "dog");      //passes
        var thingie = ["elephant", "cat", "dog"];
        assert.deepEqual(wordlist1.words, thingie);   //fails
    })
});

I have tried using single quotes rather than double quotes and changing the spacing around the brackets, but all of them tell me the same thing:

AssertionError: ["elephant","cat","dog"] deepEqual ["elephant","cat","dog"] <Click to see difference>

When I click to see the difference, WebStorm tells me that the two things are identical. Why does this test fail in spite of me using deepEqual if the two arrays have identical contents? If it's because they are strings, what approach can be used to check the equality of the arrays of strings?

Esteban
  • 2,540
  • 21
  • 27
PrairieProf
  • 174
  • 11
  • Please post your `WordList` implementation – maček Aug 13 '14 at 17:40
  • Possible duplicate of: http://stackoverflow.com/questions/13225274/the-difference-between-assert-equal-and-assert-deepequal-in-javascript-testing-w – SaintLike Aug 13 '14 at 17:52
  • The possible duplicate was what helped me understand to try deepEqual, but where their code works for that assertion, mine does not. I found that confusing and it is why I posted the question in the first place. Why does their example of deepEqual work while mine does not? I don't see why mine is failing. – PrairieProf Aug 13 '14 at 18:15
  • My entire project is located on github here: https://github.com/kklamberty/SightWords – PrairieProf Aug 13 '14 at 18:18
  • 1
    Would you try passing `wordlist1.words.toObject()` to the assertion? Mongoose might be fiddling with your array. – Esteban Aug 13 '14 at 21:33
  • @Esteban - That worked! Thanks! If this was an answer, I would be happy to check it as "accepted". I'm not sure I understand quite what was happening, but this did, indeed, make the test stop complaining. – PrairieProf Aug 13 '14 at 21:48
  • @PrairieProf sure thing, I created an answer and also added the `mongoose` tag to the question so other people can find in the future. Great to know it helped! – Esteban Aug 13 '14 at 23:35

1 Answers1

1

You need to call wordlist1.words.toObject() before passing it to the assertion.

Mongoose adds several metadata to its documents, even if they're just a subdocument or even a single property. To make sure you have a plain object or array, you can always call toObject on your document or any of its (or its properties') properties.

Another way of getting a bare object is to run your queries with the lean() modifier, such as Wordlist.findById(id).lean().exec().

Esteban
  • 2,540
  • 21
  • 27