I hit upon a problem in testing an Angular project this week that had me stumped for a while. The problem was this:
Every time I ran the test suite as a whole, they failed.
But if I ran each test file on its own… individually, they would pass.
I find more and more of my time lately is spent dealing with these sorts of things – they aren’t ‘development’ tasks, just annoyances that keep me from doing the thing I enjoy most – writing code.
Anyway, here’s the error:
INFO [PhantomJS 1.9.8 (Linux 0.0.0)]: Connected on socket DGb_Xn6WLWQNnEEkUwtk with id 45807911
PhantomJS 1.9.8 (Linux 0.0.0) LOG: 'no config'
PhantomJS 1.9.8 (Linux 0.0.0) LOG: 'no config'
PhantomJS 1.9.8 (Linux 0.0.0) LOG: 'no config'
PhantomJS 1.9.8 (Linux 0.0.0) UrlManager should have get function FAILED
TypeError: 'null' is not an object (evaluating 'currentSpec.$injector')
at workFn (/tmp/7b8601aaed330a593345d271f7b468335eb33d59.browserify:23390:0 <- /my/project/node_modules/angular-mocks/angular-mocks.js:2330:0)
at /my/project/node_modules/karma-jasmine/lib/boot.js:126
at /my/project/node_modules/karma-jasmine/lib/adapter.js:171
at http://localhost:9876/karma.js:182
at http://localhost:9876/context.html:257
TypeError: 'null' is not an object (evaluating 'currentSpec.$injector')
at workFn (/tmp/7b8601aaed330a593345d271f7b468335eb33d59.browserify:23390:0 <- /my/project/node_modules/angular-mocks/angular-mocks.js:2330:0)
at /my/project/node_modules/karma-jasmine/lib/boot.js:126
at /my/project/node_modules/karma-jasmine/lib/adapter.js:171
at http://localhost:9876/karma.js:182
at http://localhost:9876/context.html:257
TypeError: 'null' is not an object (evaluating 'currentSpec.$injector')
at workFn (/tmp/7b8601aaed330a593345d271f7b468335eb33d59.browserify:23390:0 <- /my/project/node_modules/angular-mocks/angular-mocks.js:2330:0)
at /my/project/node_modules/karma-jasmine/lib/boot.js:126
at /my/project/node_modules/karma-jasmine/lib/adapter.js:171
at http://localhost:9876/karma.js:182
at http://localhost:9876/context.html:257
TypeError: 'null' is not an object (evaluating 'currentSpec.$modules')
at workFn (/tmp/7b8601aaed330a593345d271f7b468335eb33d59.browserify:23514:0 <- /my/project/node_modules/angular-mocks/angular-mocks.js:2454:0)
at /my/project/node_modules/karma-jasmine/lib/boot.js:126
at /my/project/node_modules/karma-jasmine/lib/adapter.js:171
at http://localhost:9876/karma.js:182
at http://localhost:9876/context.html:257
TypeError: 'undefined' is not an object (evaluating 'UrlManager.get')
at /tmp/7b8601aaed330a593345d271f7b468335eb33d59.browserify:42035:0 <- /my/project/src/components/UrlManager/spec/UrlManagerSpec.js:26:0
at /my/project/node_modules/karma-jasmine/lib/boot.js:126
at /my/project/node_modules/karma-jasmine/lib/adapter.js:171
at http://localhost:9876/karma.js:182
at http://localhost:9876/context.html:257
PhantomJS 1.9.8 (Linux 0.0.0) UrlManager should return object for the others FAILED
TypeError: 'null' is not an object (evaluating 'currentSpec.$injector')
at workFn (/tmp/7b8601aaed330a593345d271f7b468335eb33d59.browserify:23390:0 <- /my/project/node_modules/angular-mocks/angular-mocks.js:2330:0)
at /my/project/node_modules/karma-jasmine/lib/boot.js:126
at /my/project/node_modules/karma-jasmine/lib/adapter.js:171
at http://localhost:9876/karma.js:182
at http://localhost:9876/context.html:257
TypeError: 'null' is not an object (evaluating 'currentSpec.$injector')
at workFn (/tmp/7b8601aaed330a593345d271f7b468335eb33d59.browserify:23390:0 <- /my/project/node_modules/angular-mocks/angular-mocks.js:2330:0)
at /my/project/node_modules/karma-jasmine/lib/boot.js:126
at /my/project/node_modules/karma-jasmine/lib/adapter.js:171
at http://localhost:9876/karma.js:182
at http://localhost:9876/context.html:257
TypeError: 'null' is not an object (evaluating 'currentSpec.$injector')
at workFn (/tmp/7b8601aaed330a593345d271f7b468335eb33d59.browserify:23390:0 <- /my/project/node_modules/angular-mocks/angular-mocks.js:2330:0)
at /my/project/node_modules/karma-jasmine/lib/boot.js:126
at /my/project/node_modules/karma-jasmine/lib/adapter.js:171
at http://localhost:9876/karma.js:182
at http://localhost:9876/context.html:257
TypeError: 'null' is not an object (evaluating 'currentSpec.$modules')
at workFn (/tmp/7b8601aaed330a593345d271f7b468335eb33d59.browserify:23514:0 <- /my/project/node_modules/angular-mocks/angular-mocks.js:2454:0)
at /my/project/node_modules/karma-jasmine/lib/boot.js:126
at /my/project/node_modules/karma-jasmine/lib/adapter.js:171
at http://localhost:9876/karma.js:182
at http://localhost:9876/context.html:257
TypeError: 'undefined' is not an object (evaluating 'UrlManager.get')
at /tmp/7b8601aaed330a593345d271f7b468335eb33d59.browserify:42053:0 <- /my/project/src/components/UrlManager/spec/UrlManagerSpec.js:44:0
at /my/project/node_modules/karma-jasmine/lib/boot.js:126
at /my/project/node_modules/karma-jasmine/lib/adapter.js:171
at http://localhost:9876/karma.js:182
at http://localhost:9876/context.html:257
PhantomJS 1.9.8 (Linux 0.0.0): Executed 7 of 7 (2 FAILED) (0.415 secs / 0.026 secs)
=============================== Coverage summary ===============================
Statements : 17.48% ( 482/2758 )
Branches : 4.14% ( 40/967 )
Functions : 4.65% ( 30/645 )
Lines : 17.62% ( 477/2707 )
================================================================================
[13:41:23] 'test' errored after 8.21 s
[13:41:23] Error in plugin 'test'
Message:
Karma test returned 1
blimpyboy@project-dev1:~/Development/project$ ^C
blimpyboy@project-dev1:~/Development/project$ ^C
blimpyboy@project-dev1:~/Development/project$ gulp test
[13:42:58] Warning: gulp version mismatch:
[13:42:58] Global gulp is 3.9.0
[13:42:58] Local gulp is 3.8.11
[13:42:59] Using gulpfile ~/Development/project/gulpfile.js
[13:42:59] Starting 'test'...
INFO [framework.browserify]: Paths to browserify
/my/project/src/components/PubSub/spec/**/*.js
/my/project/src/components/UrlManager/spec/**/*.js
INFO [framework.browserify]: Browserified in 6543ms, 6524kB
INFO [karma]: Karma v0.12.37 server started at http://localhost:9876/
INFO [launcher]: Starting browser PhantomJS
INFO [PhantomJS 1.9.8 (Linux 0.0.0)]: Connected on socket VrUi5MBGOl0J1oHWVKNw with id 50398227
PhantomJS 1.9.8 (Linux 0.0.0) LOG: 'no config'
PhantomJS 1.9.8 (Linux 0.0.0) LOG: 'no config'
PhantomJS 1.9.8 (Linux 0.0.0) LOG: 'no config'
PhantomJS 1.9.8 (Linux 0.0.0): Executed 7 of 7 SUCCESS (0.056 secs / 0.035 secs)
=============================== Coverage summary ===============================
Statements : 20.01% ( 552/2758 )
Branches : 5.07% ( 49/967 )
Functions : 6.98% ( 45/645 )
Lines : 20.21% ( 547/2707 )
================================================================================
[13:43:07] Finished 'test' after 8.1 s
blimpyboy@project-dev1:~/Development/project$
I’m aware the coverage isn’t so good – but actually this is not the true coverage as I’d stripped out a whole bunch of tests by bastardising the karma.conf.js file to try and isolate the problem. No… seriously, I promise 🙂
Anyway, it turned out that the solution to this was actually pretty simple.
Of course, nearly all solutions to programming problem seem simple once you have figured out the problem. Hindsight is such a wonderful thing.
But in this case, the problem was that a bunch of variables had been declared inside on the of describe blocks:
describe("SomeModule module", function () {
beforeEach(angular.mock.module('Some.Module'));
var angular = require('angular');
var scope;
require('../../../scripts/app.js');
require('angular-mocks');
var SomeModule;
And the solution was to simply move all the setup stuff outside of the describe block:
var angular = require('angular');
var scope;
require('../../../scripts/app.js');
require('angular-mocks');
var SomeModule;
describe("SomeModule module", function () {
beforeEach(angular.mock.module('Some.Module'));
An easy fix.
The real annoyance here was that I went through this whole project alphabetically, and this particular module began with the letter ‘P’, so I’d been through over half the code before I spotted it. Hours I will never get back.
Still, it’s fixed now, and hopefully now you can save a few hours if you ever suffer from this problem yourself.