Search code examples
javascriptmocha.jschaidescribe

TypeError: Cannot read property 'should' of undefined


I have a function (detectNetwork) which checks a credit card number and if it matches a recognized vendor identifies the vendor for the card. I have another function (detectNetworkTest) which using Mocha can be invoked to test detectNetwork and validate it is working correctly.

I am working on the validation for China UnionPay and receiving the following error when invoking detectNetworkTest:

TypeError: Cannot read property 'should' of undefined at Context. (detectNetwork.test.js:241:39)

Which refers to this line in detectNetworkTest:

detectNetwork(cardNumberToCheck).should.equal('China UnionPay');

When running this function I can confirm by logging to the console that cardNumberToCheck is the correct value I need to utilize so I am bit stuck on trying to determine why it is being read as undefined.

detectNetwork

var detectNetwork = function(cardNumber) {
  // Note: `cardNumber` will always be a string

  var re = {
    "Diner's Club": /^3[8-9][0-9]{12}$/,
    "American Express": /^3[4,7][0-9]{13}$/,
    "Visa": /^4([0-9]{12}|[0-9]{15}|[0-9]{18})$/,
    "MasterCard": /^5[1-5]([0-9]{14})$/,
    "Discover": /^6(011|5[0-9]{2}|4[4-9]{1}[0-9]{1})[0-9]{12,15}$/,
    "Maestro": /^(5018|5020|5038|6304)[0-9]{8,15}$/
  }

  for(var card in re) {
    if(re[card].test(cardNumber)) {
      return card;
    }
  }
}

detectNetworkTest

describe('should support China UnionPay', function() {

  var should = chai.should();
  var prefixes = [];
  var randomNumber = Math.random().toString(7).substr(2, 17);
  var randomCardNumbers = [];

  for(i=624; i<627; i++) {
    prefixes.push(i);
  }

  for(j=622126; j<622926; j++) {
    prefixes.push(j);
  }

  for(k=6282; k<6288; k++) {
    prefixes.push(k);
  }

  prefixes.forEach(value => {
    for(i=14; i<18; i++) {
      var randomCardNumber = (value + "x" + randomNumber).substring(0,i);
      randomCardNumbers.push(randomCardNumber);
    };
  });

  randomCardNumbers.forEach(value => {
    var prefix = value.split("x")[0];
    var cardNumberToCheck = value.replace("x","");

    it('has a prefix of ' + prefix + ' and a length of ' + cardNumberToCheck.length, function() {
      detectNetwork(cardNumberToCheck).should.equal('China UnionPay');
    });
  });

});

Solution

  • The error means detectNetwork(cardNumberToCheck) evaluates to undefined.

    This is because there is no return in case there is absolutely no match for re[card].test(cardNumber)