browserify/karma/angular.js "TypeError: Cannot read property '$injector' of null" when second test uses "angular.mock.inject", "currentSpec" is null -
i have angular.js app , experimenting using browserify. app works, want run tests too, have 2 jasmine tests run karma. user browserify give me access angular.js , angular-mocks.js , other test fixtures within tests.
versions are:-
"angular": "^1.4.0", "angular-mocks": "^1.4.0", "browserify": "^10.2.3", "karma": "^0.12.32", "karma-browserify": "^4.2.1", "karma-chrome-launcher": "^0.1.12", "karma-coffee-preprocessor": "^0.2.1", "karma-jasmine": "^0.3.5", "karma-phantomjs-launcher": "^0.1.4",
if run tests individually (by commenting 1 or other karma.conf file) both work ok. (yey!)
but if run them both error
typeerror: cannot read property '$injector' of null @ object.workfn (/tmp/3efdb16f2047e981872d82fd8db9c0a8.browserify:2272:22 <- node_modules/angular-mocks/angular-mocks.js:2271:0)
looking @ line 2271 of angular.mocks.js reads
if (currentspec.$injector) {
so currentspec somehow null.
i have isolated problem when call "angular.mock.inject" in second test.
beforeeach(angular.mock.inject(function (_gridutilservice_) { gridutilservice = _gridutilservice_; }));
if comment out works, can't run test n gridutilservice.
does know how run 2 (or more :-) angular-mock jasmine tests karma , browserify?
below tests, karma.conf file. angular services work when deployed purpose can dumb services nothing.
karma.conf:-
// karma configuration module.exports = function(config) { config.set({ // base path used resolve patterns (eg. files, exclude) basepath: '', // frameworks use // available frameworks: https://npmjs.org/browse/keyword/karma-adapter frameworks: ['browserify', 'jasmine'], // list of files / patterns load in browser files: [ 'src/main/assets/js/**/*.js', // 'src/test/**/*.js' 'src/test/services/settingutil*.js', 'src/test/services/gridutil*.js' ], // list of files exclude exclude: [ 'src/main/assets/js/**/app-config.js' ], // preprocess matching files before serving them browser // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor preprocessors: { 'src/main/assets/js/**/*.js': ['browserify'], 'src/test/**/*.js': ['browserify'] }, browserify: { debug: true, extensions: ['.js', '.coffee', '.hbs'] }, // test results reporter use // possible values: 'dots', 'progress' // available reporters: https://npmjs.org/browse/keyword/karma-reporter reporters: ['progress'], // web server port port: 9876, // enable / disable colors in output (reporters , logs) colors: true, // level of logging // possible values: config.log_disable || config.log_error || config.log_warn || config.log_info || config.log_debug loglevel: config.log_debug, // enable / disable watching file , executing tests whenever file changes autowatch: true, // start these browsers // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher // browsers: ['phantomjs', 'chrome'], // browsers: ['phantomjs'], browsers: ['chrome'], // continuous integration mode // if true, karma captures browsers, runs tests , exits singlerun: false }); };
src/test/services/settingutilservicetest.js:
'use strict'; describe("settingutilservicetest.", function() { it("contains spec expectation", function() { expect(true).tobe(true); }); require('angular'); require('angular-mocks'); // can't below see error @ https://github.com/xdissent/karma-browserify/issues/10 //beforeeach(module('dpservices')); //so need todo beforeeach(angular.mock.module('dpservices')); var fixtures = require('./servicefixtures.js'); var sus = fixtures.settingutilservicetestfixtures; var ts1 = sus.tablesetting1; var ts2 = sus.tablesetting2; var settingutilservice; beforeeach(angular.mock.inject(function (_settingutilservice_) { settingutilservice = _settingutilservice_; })); it('should return object containing mins , maxs function minmaxes()', function() { expect(ts1).tobedefined(); expect(ts2).tobedefined(); var minmaxs = settingutilservice.minmaxs(ts1); var mins = minmaxs.mins; expect(mins).tobedefined(); var maxs = minmaxs.maxs; expect(maxs).tobedefined(); }); });
src/test/services/gridutilservicetest.js:
'use strict'; describe("gridutilservicetest.", function() { it("is set of tests test gridutilservice.", function() { expect(true).tobe(true); }); require('angular'); require('angular-mocks'); // can't below see error @ https://github.com/xdissent/karma-browserify/issues/10 // beforeeach(module('dpservices')); //so need todo beforeeach(angular.mock.module('dpservices')); var fixtures = require('./gridfixtures.js'); var gridfix = fixtures.gridutilservicetestfixtures; var ts1 = gridfix.tablesetting1; var ts2 = gridfix.tablesetting2; var ts3 = gridfix.tablesetting3; var gridutilservice; beforeeach(angular.mock.inject(function (_gridutilservice_) { gridutilservice = _gridutilservice_; })); it('should return object containing mins , maxs function minmaxes()', function() { expect(ts1).tobedefined(); expect(ts2).tobedefined(); expect(ts3).tobedefined(); }); });
if need access angular setup can provide (split multiple files using browserify's require() function , built gulp... app runs ok , tests fail when there 2 tests, think issue karma-jasmine , angular-mocks or overwriting currentspec
variable.
if knows how split angular tests multiple tests (i don't want monolithic angular test) without error message appreciated. thanks.
i had same issue, issue me because requiring angular , angular-mocks inside each describe block. moved 2 lines
require('angular'); require('angular-mocks');
above describe blocks , made sure call them once.
Comments
Post a Comment