jeudi 29 décembre 2016

Testing angular sub-module can't find dependency

I'm trying to test my angular 1 (using typescript) modules, and I'm having issues with the dependencies:

These are my modules:

app.module.ts

export const appModule: IModule = angular.module('app', [
    'ui.router',
    'ngMaterial',
    'ngMessages',
    'ngAnimate',
    'angularMoment',
    'ngMap',
    sharedModule.name,
    componentsModule.name
]);

shared.module.ts

export const sharedModule: IModule = angular.module('app.shared', [
    'ui.router',
    'ngMaterial',
    'ngMessages',
    'ngAnimate',
    'angularMoment',
    'ngMap'
]);

but when testing I get the error on all my tests, however my project runs just fine in the browser. So only the tests can't find the dependency throw erros like this:

Uncaught Error: [$injector:modulerr] Failed to instantiate module app.shared due to:
Error: [$injector:modulerr] Failed to instantiate module ui.router due to:
Error: [$injector:nomod] Module 'ui.router' 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.

but when i set my shared module to:

export const sharedModule: IModule = angular.module('app.shared', []);

then it errors only on the tests that use external services, e.g.:

Error: [$injector:unpr] Unknown provider: $stateProvider <- $state

the test is as follows:

describe('component: bookmark', () => {
    let parentScope: any;
    let element: any;

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

    beforeEach(inject(($compile: ng.ICompileService, $rootScope: ng.IRootScopeService) => {
        parentScope = $rootScope.$new();

        element = angular.element(`<bookmark></bookmark>`);
        $compile(element)(parentScope);

        parentScope.$digest();
    }));
    it('should display a star', () => {
        const someAttrValue: string = findIn(element, 'md-icon').text();
        console.debug('found ', someAttrValue);
        expect(someAttrValue).toEqual('star');

    });
});

and my bookmark requires $state

any clue what I might be doing wrong?

Aucun commentaire:

Enregistrer un commentaire