Search code examples
angularjskarma-runnerecmascript-6jspm

Unit testing with Karma on an Angular 1.3/JSPM/Babel project


So I'm setting up an angular 1.3 es6 project using jspm and babel. Im using es6's import/export feature. I have karma and karma-jspm installed and can run basic tests without an issue. When I try to import one of my modules into a test to test it, I get an error in karma.

Error loading "app/services/chart-data/chart-data.spec" at http://localhost:9876/base/app/services/chart-data/chart-data.spec.js Error loading "npm:[email protected]" at http://localhost:9876/base/jspm_packages/npm/[email protected] Error evaluating http://localhost:9876/base/jspm_packages/npm/[email protected] Error evaluating http://localhost:9876/base/jspm_packages/npm/[email protected]/browser.js TypeError: 'undefined' is not a function (evaluating 're.test.bind(re)') at undefined

And here is a piece of my karma config file:

frameworks: ['jspm', 'jasmine'],


// list of files / patterns to load in the browser
files: [
],

jspm: {
  // Edit this to your needs
  loadFiles: ['app/**/*.spec.js'],
  serveFiles: []
},

And this is an example test:

import {chartData} from 'chart-data.service';

describe('Example test', function() {
  it('should be foo', function() {
      expect('foo').toBe('foo');
  });
});

I haven't had any luck with searching for an answer and even asked for help in the jspm gitter, but nothings turned out so far.

Everything else in the project seems to work fine, and I can run the test if I remove the import line. Any help or suggestions would be greatly appreciated.


Solution

  • Looks like you are running tests in browser which doesn't support Function.prototype.bind. Babel needs this function.

    You can fix is by karma-phantomjs-shim, or try a new version of browser. Since version 2.0, PhantomJS has this function.