How to unit test an Angular service with AngularJS service dependencies









up vote
3
down vote

favorite












I am trying to migrate angular js services to a new angular 7 app. However, each service has many dependencies on other angularjs services and I am not able to convert them all at the same time whilst continuing to create new features. How can I inject angular js services inside unit tests so that I can test my Angular 7 service?



tests.ts // test entry file



import 'core-js/es6';
import 'reflect-metadata';
import 'zone.js/dist/zone';
import 'zone.js/dist/long-stack-trace-zone';
import 'zone.js/dist/proxy';
import 'zone.js/dist/sync-test';
import 'zone.js/dist/jasmine-patch';
import 'zone.js/dist/async-test';
import 'zone.js/dist/fake-async-test';

import getTestBed from '@angular/core/testing';
import
BrowserDynamicTestingModule,
platformBrowserDynamicTesting
from '@angular/platform-browser-dynamic/testing';

declare const require: any;

// First, initialize the Angular testing environment.
getTestBed().initTestEnvironment(
BrowserDynamicTestingModule,
platformBrowserDynamicTesting()
);

// Then we find all the tests.
const context = require.context('./', true, /.spec.ts$/);

// And load the modules.
context.keys().map(context);


TestService.ts



import UpgradeModule from '@angular/upgrade/static';
import CHECKOUT_SERVICE from '../../../../ajs-upgraded-providers';

@Injectable(
providedIn: 'root'
)
export class TestService
let checkout: any;
constructor(@Inject(CHECKOUT_SERVICE) private checkoutService: any)
this.checkout = checkoutService;




TestService.spec.ts



import TestBed from '@angular/core/testing';
import TestingService from './testing-service';
import CheckoutServiceProvider from 'ajs-upgraded-provider.ts'

describe('testing service with an injected AJS service', () =>
let service: TestingService;
TestBed.configureTestingModule(
providers:[
TestService,
CheckoutServiceProvider
],
imports:[ upgradeModule ]
);
service = TestBed.get(TestingService);
)
it('instance should be created', () =>
expect(expect(service).toBeTruthy();
)
})


When running this unit test I get the following error:



Error: Trying to get the AngularJS injector before it being set.









share|improve this question



























    up vote
    3
    down vote

    favorite












    I am trying to migrate angular js services to a new angular 7 app. However, each service has many dependencies on other angularjs services and I am not able to convert them all at the same time whilst continuing to create new features. How can I inject angular js services inside unit tests so that I can test my Angular 7 service?



    tests.ts // test entry file



    import 'core-js/es6';
    import 'reflect-metadata';
    import 'zone.js/dist/zone';
    import 'zone.js/dist/long-stack-trace-zone';
    import 'zone.js/dist/proxy';
    import 'zone.js/dist/sync-test';
    import 'zone.js/dist/jasmine-patch';
    import 'zone.js/dist/async-test';
    import 'zone.js/dist/fake-async-test';

    import getTestBed from '@angular/core/testing';
    import
    BrowserDynamicTestingModule,
    platformBrowserDynamicTesting
    from '@angular/platform-browser-dynamic/testing';

    declare const require: any;

    // First, initialize the Angular testing environment.
    getTestBed().initTestEnvironment(
    BrowserDynamicTestingModule,
    platformBrowserDynamicTesting()
    );

    // Then we find all the tests.
    const context = require.context('./', true, /.spec.ts$/);

    // And load the modules.
    context.keys().map(context);


    TestService.ts



    import UpgradeModule from '@angular/upgrade/static';
    import CHECKOUT_SERVICE from '../../../../ajs-upgraded-providers';

    @Injectable(
    providedIn: 'root'
    )
    export class TestService
    let checkout: any;
    constructor(@Inject(CHECKOUT_SERVICE) private checkoutService: any)
    this.checkout = checkoutService;




    TestService.spec.ts



    import TestBed from '@angular/core/testing';
    import TestingService from './testing-service';
    import CheckoutServiceProvider from 'ajs-upgraded-provider.ts'

    describe('testing service with an injected AJS service', () =>
    let service: TestingService;
    TestBed.configureTestingModule(
    providers:[
    TestService,
    CheckoutServiceProvider
    ],
    imports:[ upgradeModule ]
    );
    service = TestBed.get(TestingService);
    )
    it('instance should be created', () =>
    expect(expect(service).toBeTruthy();
    )
    })


    When running this unit test I get the following error:



    Error: Trying to get the AngularJS injector before it being set.









    share|improve this question

























      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      I am trying to migrate angular js services to a new angular 7 app. However, each service has many dependencies on other angularjs services and I am not able to convert them all at the same time whilst continuing to create new features. How can I inject angular js services inside unit tests so that I can test my Angular 7 service?



      tests.ts // test entry file



      import 'core-js/es6';
      import 'reflect-metadata';
      import 'zone.js/dist/zone';
      import 'zone.js/dist/long-stack-trace-zone';
      import 'zone.js/dist/proxy';
      import 'zone.js/dist/sync-test';
      import 'zone.js/dist/jasmine-patch';
      import 'zone.js/dist/async-test';
      import 'zone.js/dist/fake-async-test';

      import getTestBed from '@angular/core/testing';
      import
      BrowserDynamicTestingModule,
      platformBrowserDynamicTesting
      from '@angular/platform-browser-dynamic/testing';

      declare const require: any;

      // First, initialize the Angular testing environment.
      getTestBed().initTestEnvironment(
      BrowserDynamicTestingModule,
      platformBrowserDynamicTesting()
      );

      // Then we find all the tests.
      const context = require.context('./', true, /.spec.ts$/);

      // And load the modules.
      context.keys().map(context);


      TestService.ts



      import UpgradeModule from '@angular/upgrade/static';
      import CHECKOUT_SERVICE from '../../../../ajs-upgraded-providers';

      @Injectable(
      providedIn: 'root'
      )
      export class TestService
      let checkout: any;
      constructor(@Inject(CHECKOUT_SERVICE) private checkoutService: any)
      this.checkout = checkoutService;




      TestService.spec.ts



      import TestBed from '@angular/core/testing';
      import TestingService from './testing-service';
      import CheckoutServiceProvider from 'ajs-upgraded-provider.ts'

      describe('testing service with an injected AJS service', () =>
      let service: TestingService;
      TestBed.configureTestingModule(
      providers:[
      TestService,
      CheckoutServiceProvider
      ],
      imports:[ upgradeModule ]
      );
      service = TestBed.get(TestingService);
      )
      it('instance should be created', () =>
      expect(expect(service).toBeTruthy();
      )
      })


      When running this unit test I get the following error:



      Error: Trying to get the AngularJS injector before it being set.









      share|improve this question















      I am trying to migrate angular js services to a new angular 7 app. However, each service has many dependencies on other angularjs services and I am not able to convert them all at the same time whilst continuing to create new features. How can I inject angular js services inside unit tests so that I can test my Angular 7 service?



      tests.ts // test entry file



      import 'core-js/es6';
      import 'reflect-metadata';
      import 'zone.js/dist/zone';
      import 'zone.js/dist/long-stack-trace-zone';
      import 'zone.js/dist/proxy';
      import 'zone.js/dist/sync-test';
      import 'zone.js/dist/jasmine-patch';
      import 'zone.js/dist/async-test';
      import 'zone.js/dist/fake-async-test';

      import getTestBed from '@angular/core/testing';
      import
      BrowserDynamicTestingModule,
      platformBrowserDynamicTesting
      from '@angular/platform-browser-dynamic/testing';

      declare const require: any;

      // First, initialize the Angular testing environment.
      getTestBed().initTestEnvironment(
      BrowserDynamicTestingModule,
      platformBrowserDynamicTesting()
      );

      // Then we find all the tests.
      const context = require.context('./', true, /.spec.ts$/);

      // And load the modules.
      context.keys().map(context);


      TestService.ts



      import UpgradeModule from '@angular/upgrade/static';
      import CHECKOUT_SERVICE from '../../../../ajs-upgraded-providers';

      @Injectable(
      providedIn: 'root'
      )
      export class TestService
      let checkout: any;
      constructor(@Inject(CHECKOUT_SERVICE) private checkoutService: any)
      this.checkout = checkoutService;




      TestService.spec.ts



      import TestBed from '@angular/core/testing';
      import TestingService from './testing-service';
      import CheckoutServiceProvider from 'ajs-upgraded-provider.ts'

      describe('testing service with an injected AJS service', () =>
      let service: TestingService;
      TestBed.configureTestingModule(
      providers:[
      TestService,
      CheckoutServiceProvider
      ],
      imports:[ upgradeModule ]
      );
      service = TestBed.get(TestingService);
      )
      it('instance should be created', () =>
      expect(expect(service).toBeTruthy();
      )
      })


      When running this unit test I get the following error:



      Error: Trying to get the AngularJS injector before it being set.






      angularjs angular unit-testing migration angular7






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 9 at 15:35









      Goncalo Peres

      8171311




      8171311










      asked Nov 8 at 14:25









      Bunne GilShaw

      162




      162



























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53209735%2fhow-to-unit-test-an-angular-service-with-angularjs-service-dependencies%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53209735%2fhow-to-unit-test-an-angular-service-with-angularjs-service-dependencies%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Kleinkühnau

          Makov (Slowakei)

          Deutsches Schauspielhaus