AngularJS: unable to inject module/service for unit test

Datetime:2016-08-23 02:35:36          Topic: AngularJS  Unit Testing           Share

when new to TDD and am trying to wire up a test, and have been stuck on it for hours. I keep getting the following error:

[$injector:modulerr] Failed to instantiate module AuthInterceptor due to:

Error: [$injector:nomod] Module 'AuthInterceptor' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.

http://errors.angularjs.org/1.5.8/$injector/nomod?p0=AuthInterceptor

at client/test/index.js:8237:13

at client/test/index.js:10251:18

at ensure (client/test/index.js:10175:39)

at module (client/test/index.js:10249:15)

at client/test/index.js:12786:23

at forEach (client/test/index.js:8490:21)

at loadModules (client/test/index.js:12770:6)

Here is my test:

import angularjs from 'angular';

import serviceModule from './auth.interceptor'

describe('wire.common.services', () => {

describe('AuthService', () => {

let AuthService;

beforeEach(angular.mock.module(serviceModule.name));

beforeEach(angular.mock.module(($provide) => {

$provide.factory('$q', () => ({}));

$provide.factory('$log', () => ({}));

}));

beforeEach(angular.mock.inject((_AuthService_) => {

AuthService = _AuthService_;

}));

it('should be a dummy test', () => {

expect(2).toEqual(2);

});

});

});

The actual code I'm testing:

export default function AuthInterceptor($q, $injector, $log) {

'ngInject';

return {

request(config) {

let AuthService = $injector.get('AuthService');

if (!config.bypassAuthorizationHeader) {

if (AuthService.jwtToken) {

config.headers.Authorization = `Bearer ${AuthService.jwtToken}`;

} else {

$log.warn('Missing JWT', config);

}

}

return config || $q.when(config);

},

responseError(rejection) {

let AuthService = $injector.get('AuthService');

if (rejection.status === 401) {

AuthService.backToDAS();

}

return $q.reject(rejection);

}

};

}

I don't understand why I'm getting this error – I provided all the dependencies for the service and am following what is outlined in the angularjs documentation. any help is appreciated!

Solution :

This means that ng module is failing to load. �� And this happens while bootstrapping the app and ng module is first in a three element array: ng, ['$provide', function($provide) { … }] and my own application module. It fails when loading the first one.

I've looked at console and I've copied this error message from it. There's no other error. None.

I hope you clicked that specific link and see that it doesn't give you any specific ideas about it. Unfortunately I've added this GitHub issue after exhausting other resources. I'm currently debugging angularjs code to get any further.

Solution 2:

This means that AuthInterceptor angularjs module wasn't defined (and by the way, relying on name is unsafe).

AuthInterceptor isn't a module but an injectable function. It can be tested in functional fashion as $http interceptor:

beforeEach(angular.mock.module(($httpProvider) => {

$httpProvider.interceptors.push(AuthInterceptor);

});

it('…', () => {

$httpBackend.when(…).respond(…);

$http.get(…).then((interceptedResult) => {

expect(interceptedResult)…

});

$rootScope.$digest();

});

or directly:

it('…', () => {

let interceptor = $injector.invoke(AuthInterceptor);

expect(interceptor).toEqual({

request: jasmine.any(Function),

requestError: jasmine.any(Function)

});

var config = { headers: {} };

interceptor.request(config);

expect(config)…

});

Services that produce side effects (AuthService, $log) should be stubbed.





About List