Question

We use the closure library across several different projects that share code. We're starting up with AngularJS in one of the projects now and we're trying to get the Karma runner working properly.

I've followed the advice from this post, but can't get it working all the way. How to configure karma-runner (also known as testacular) to work with closure-library

It is working to goog.require('my.namespaced.file') but when my/namespaced/file.js has a goog.require('goog.dom') or something from the closure library, the Karma runner pukes out this...

Starting Karma Server (http://karma-runner.github.io)
-------------------------------------------------------------------
INFO [karma]: Karma v0.9.2 server started at http://localhost:9876/
INFO [launcher]: Starting browser Chrome
NOT SERVED FILE undefined
INFO [Chrome 27.0 (Mac)]: Connected on socket id 3tRuxHEhooSb458fJnFt
ERROR [karma]: [TypeError: Cannot call method 'indexOf' of undefined]
TypeError: Cannot call method 'indexOf' of undefined
    at /usr/local/lib/node_modules/karma/lib/web-server.js:103:28
    at Array.map (native)
    at /usr/local/lib/node_modules/karma/lib/web-server.js:91:43
    at /usr/local/lib/node_modules/karma/lib/web-server.js:37:35
    at fs.js:266:14
    at Object.oncomplete (fs.js:107:15)

Here is my karma.conf.js file

basePath = '../../../../';

// frameworks to use
frameworks = ['jasmine', 'closure'];

// list of files / patterns to load in the browser
files = [
 // closure base
 {pattern: 'closure-library/closure/goog/base.js', watched: false, included: true, served: true},
 {pattern: 'ne-js/src/main/js/ne-js.deps.js', watched: false, included: true, served: true},
 {pattern: 'angular-ne/src/main/js/angular-ne.deps.js', watched: false, included: true, served: true},
 {pattern: 'ne-webServices/src/main/js/ne-webServices.deps.js', watched: false, included: true, served: true},
 {pattern: 'directives/src/main/js/directives.deps.js', watched: false, included: true, served: true},
 {pattern: 'webapp/src/main/js/webapp.deps.js', watched: false, included: true, served: true},
 {pattern: 'webapp/src/test/lib/angular.js', watched: false, included: false, served: true},
 {pattern: 'webapp/src/test/lib/angular-mocks.js', watched: false, included: false, served: true},

 {pattern: 'webapp/src/main/js/**/*.js', watched: true, included: false, served: true},
 {pattern: 'webapp/src/test/js/**/*.js', watched: true, included: true, served: true},
];

preprocessors = {
 'webapp/src/test/js/**/*.js': ['closure', 'closure-iit'],
 'webapp/src/main/js/**/*.js': ['closure'],
};

autoWatch = true;
browsers = ['Chrome'];

// plugins to load
plugins = [
 'testacular-closure',
 'karma-jasmine',
 'karma-chrome-launcher',
 'karma-phantomjs-launcher',
];
Was it helpful?

Solution

tl;dr; You need to add {pattern: 'lib/goog/deps.js', included: false, served: false} to your files list, where lib/goog/deps.js is the path to the deps.js file in your version of closure-library.

Alright, so I dug into the Karma web server and the Karma closure plugin and found the problem.

The problem is in web-server.js on line 103, caused by the fact that a file dependency is missing. Currently the karma-closure plugin doesn't report an error when there is a missing dependency and instead adds a file pointer with a path of undefined, which eventually causes the web-server.js file to blow up (which is what you're seeing).

In my case I had missed doing the preprocessor on all of my files, so one of my projects dependencies wasn't getting mapped causing it to blow up. In your case it looks like you're missing the closure deps.js file. If you notice in the demo conf file you need to manually include the closure libraries deps.js file. Without this file the closure provided namespaces (goog.dom and the like) aren't being mapped.

I've also added a pull request on GitHub that adds an error message for missing dependencies, instead of letting web-server.js blow up.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top