Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setup karma unit tests with pouchdb

I want to setup unit tests for my angular app that uses pouchdb + angular-pouchdb. But when i run:

karma start karma.conf.js

I get the following error:

PhantomJS 1.9.7 (Mac OS X) ERROR

TypeError: 'undefined' is not a function (evaluating 'eventEmitter[method].bind(eventEmitter)')

at .../public/bower_components/pouchdb/dist/pouchdb-nightly.js:5758

The angular app runs well in the browser, and all my karma.conf.js file includes the same dependencies, so i don't understand why the pouchdb-nightly.js would have an undefined function. What am I missing?

like image 560
vilsbole Avatar asked Dec 25 '22 06:12

vilsbole


2 Answers

This is a common error: PhantomJS has not implemented Function.prototype.bind (here's the bug), so you need the es5-shim.

Or you can get by by just shimming Function.prototype.bind instead of including the whole es5-shim library. Here's an example (taken from here):

(function () {
  'use strict';
  // minimal polyfill for phantomjs; in the future, we should do ES5_SHIM=true like pouchdb
  if (!Function.prototype.bind) {
    Function.prototype.bind = function (oThis) {
      if (typeof this !== "function") {
        // closest thing possible to the ECMAScript 5
        // internal IsCallable function
        throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");
      }

      var aArgs = Array.prototype.slice.call(arguments, 1),
          fToBind = this,
          fNOP = function () {},
          fBound = function () {
            return fToBind.apply(this instanceof fNOP && oThis
                   ? this
                   : oThis,
                   aArgs.concat(Array.prototype.slice.call(arguments)));
          };

      fNOP.prototype = this.prototype;
      fBound.prototype = new fNOP();

      return fBound;
    };
  }
})();
like image 64
nlawson Avatar answered Dec 28 '22 07:12

nlawson


The issue is fixed in PhantomJS 2+. So an update of the Phantom node modules should do it.

Tested with the following versions of test-related modules in package.json

"karma": "0.13.21",
"karma-jasmine": "0.3.7",
"karma-phantomjs-launcher": "1.0.0",
"phantomjs-prebuilt": "2.1.3",
like image 33
Jörg Avatar answered Dec 28 '22 05:12

Jörg