0

I'm running into a wall with working out why my test isn't passing as I'm learning how do to testing using Mocha/Chai. Any help would be appreciated!

My console is throwing me the following error though I've read from multiple other posts that this may be masking the true error:

Error: done() called multiple times at Test.Runnable [as constructor]

And Mocha throws me this error for each of my test cases:

AssertionError: expected '' to equal 'JCB' at Context. (detectNetwork.test.js:253:65)

In association with this specific line of code:

detectNetwork(prefix4[i] + '1'.repeat(len[j]-4)).should.equal('JCB') 

When console logged independently however, the test goes through and produces 24 total tests.

This is the function:

var detectNetwork = function(cardNumber) {
  var cardBrand = '';
  var prefix1 = cardNumber.substring(0,1);
  var prefix2 = cardNumber.substring(0,2);
  var prefix3 = cardNumber.substring(0,3);
  var prefix4 = cardNumber.substring(0,4);
  var prefix6 = cardNumber.substring(0,6);

    if (prefix4 === '4903' || prefix4 === '4905' || prefix4 === '4911' || prefix4 === '4936' || prefix6 === '564182' || prefix6 === '633110' || prefix4 === '6333' || prefix4 === '6759') {
        if (cardNumber.length === 16 || cardNumber.length === 18 || cardNumber.length === 19) {
            cardBrand = 'JCB';
        }
    } 
return cardBrand;
};

This is the Chai test in Mocha:

describe('JCB', function() {
  var should = chai.should();
  var prefix4 = ['4903', '4905', '4911', '4936', '6333', '6759'];
  var prefix6 = ['564182', '633110'];
  var len = [16, 18, 19];

  for (var i = 0; i < prefix4.length; i++) {
    for (var j = 0; j < len.length; j++) {

      it('has a prefix ' + prefix4[i] + ' and a length of ' + len[j], function() {
        detectNetwork(prefix4[i] + '1'.repeat(len[j]-4)).should.equal('JCB');
  });

    }
  }

  for (var i = 0; i < prefix6.length; i++) {
    for (var j = 0; j < len.length; j++) {

  it('has a prefix ' + prefix6[i] + ' and a length of ' + len[j], function() {
    detectNetwork(prefix6[i] + '1'.repeat(len[j]-6)).should.equal('JCB');
      });

    }
  }

});
misteryeo
  • 27
  • 2
  • 7
  • Your code has the same mistake as what is described in [this question](http://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example). Read the question there and the answers Once you've edited your code to take care of the problem, if you still have issues, you can edit your question to focus on the remaining issue, and we can reopen. – Louis Jan 15 '17 at 23:10
  • Thanks @Louis, that's actually amazing. I had no clue that that was even the issue or the mistake. Once I read that link you included, everything makes sense. Thanks a lot! – misteryeo Jan 15 '17 at 23:41

0 Answers0