mercredi 21 janvier 2015

Mocha JavaScript test warning: TypeError: 'undefined' is not a function

I have a Mocha test that assigns an object from a prototyped class to a variable. When this object is initiated a couple of processes get executed.



  • Possibly multiple DOM nodes are selected with a class selector.

  • A jQuery each loop is supposed to go through the elements within the fetched elements and assign click handlers.


This is where the test breaks.


The error from the terminal:



Running "mocha:all" (mocha) task
Testing: test/index.html
Warning: TypeError: 'undefined' is not a function (evaluating 'function(i, element) {
var $element = $(element);
this.setClickHandlers($element);
}.bind(this)')


The relevant part the JavaScript class:



Selectable = function(overrrideSettings, dependancies) {
// Instance variables.
this.__settings = $.extend(settings, overrrideSettings);
this.__classId = ((( 1 + Math.random()) * 0x10000) | 0) + new Date().getTime();
this.$__selectables = $(this.__settings.element);
// Call the init method.
this.init();
};

/*
* Initialize selectables.
*/
Selectable.prototype.init = function() {
// ** BUG **
// The callback function seems to be giving trouble.
this.$__selectables.each(function(i, element) {
var $element = $(element);
this.setClickHandlers($element);
}.bind(this));
};


Is it possible to fix this and what is the actual problem.


Aucun commentaire:

Enregistrer un commentaire