Karma instances do not work after migrating to angular 1.4.3

I have a stable product that uses angular 1.2.23. Recently, I decided to upgrade to angular 1.4.3. After several compatibility issues with all the dependencies, my application works fine, but all the test modules of the device started to fail .. After investing, I realized that if I update the versions of all the dependencies, but keep angular in the previous version, that is 1.2.23, test files work fine. Since angular 1.4.3, for some reason, failures in dependency injection in unit tests fail.

Below is a list of updated dependencies in bower.json.

"dependencies": { "angular-cookies": "1.4.3", "bootstrap": "3.0.3", "angular-ui-router": "0.2.15", "angular-gettext": "2.1.0", "angular": "1.4.3", "angular-ui-utils": "3.0.0", "restangular": "1.4.0", "angular-route": "1.4.3", "momentjs": "2.10.6", "angular-i18n": "1.4.3" } 

Below is a test file -

 describe("Module: x.xyz", function () { describe("Factory: xyz", function () { var service; beforeEach(function () { module('x.xyz'); inject(function ($injector) { service = $injector.get("xyz"); }); }); describe("Testing service(): ", function () { describe('Testing getXYZDescription(): ', function () { it('should return the description for the xyz event name passed if it is available', function () { expect(service.getXYZDescription('abc')).toBe('abc'); }); }); }); }); }); 

When I run the above test case, I get the service undefined. Can anyone help?

+5
source share
1 answer

I had a similar problem when upgrading from angular from 1.3 to 1.4. In my case, I forgot to update angular-mocks from 1.3 to 1.4. I suspect that the mocking functionality was split into a separate module in the transition from 1.2 to 1.3, although I can not find the documentation confirming this. In this case, you would not need the angular-mocks for your original application, but you would need to add the dependency when updating.

You can fix this by adding "angular-mocks": "1.4.x" to the list of dependencies and a link to the installed file in the karma configuration. For completeness, here is a minimal example:

karma.conf.js :

 /*global module*/ module.exports = function (config) { 'use strict'; config.set({ basePath: '.', frameworks: ['jasmine'], files: [ 'node_modules/angular/angular.js', 'node_modules/angular-mocks/angular-mocks.js', '*.js' ], autoWatch: true, singleRun: false, browsers: ['Chrome'] }); }; 

package.json :

 { "name": "angular-inject-test", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "karma start karma.conf.js" }, "author": "", "dependencies": { "angular": "1.4.x", "angular-mocks": "1.4.x", "karma": "^0.13.x", "karma-cli": "^0.1.x", "karma-jasmine": "^0.3.x", "karma-chrome-launcher": "^0.2.x" } } 

test.js :

 /*global angular, beforeEach, describe, expect, inject, it, module*/ angular.module('x', []) .factory('xyz', function () { "use strict"; return { getXYZDescription: function (value) { return value; } }; }); describe("Module: x.xyz", function () { "use strict"; describe("Factory: xyz", function () { var service; beforeEach(function () { module('x'); inject(function ($injector) { service = $injector.get("xyz"); }); }); it('Should echo input', function () { expect(service.getXYZDescription('abc')).toBe('abc'); }); }); }); 
+2
source

All Articles