Cookies concent notice

This site uses cookies from Google to deliver its services and to analyze traffic.
Learn more
Skip to main content
This site is no longer updated.Head to Angular.devHome
/

This is the archived documentation for Angular v17. Please visit angular.dev to see this page for the current version of Angular.

createAngularJSTestingModule

A helper function to use when unit testing AngularJS services that depend upon downgraded Angular services.

See more...

      
      createAngularJSTestingModule(angularModules: any[]): string
    
Parameters
angularModules any[]

a collection of Angular modules to include in the configuration.

Returns

string

Description

This function returns an AngularJS module that is configured to wire up the AngularJS and Angular injectors without the need to actually bootstrap a hybrid application. This makes it simpler and faster to unit test services.

Use the returned AngularJS module in a call to angular.mocks.module to include this module in the unit test injector.

In the following code snippet, we are configuring the $injector with two modules: The AngularJS ng1AppModule, which is the AngularJS part of our hybrid application and the Ng2AppModule, which is the Angular part.

      
      beforeEach(module(createAngularJSTestingModule([Ng2AppModule])));
beforeEach(module(ng1AppModule.name));
    

Once this is done we can get hold of services via the AngularJS $injector as normal. Services that are (or have dependencies on) a downgraded Angular service, will be instantiated as needed by the Angular root Injector.

In the following code snippet, heroesService is a downgraded Angular service that we are accessing from AngularJS.

      
      it('should have access to the HeroesService', inject((heroesService: HeroesService) => {
  expect(heroesService).toBeDefined();
}));
    

This helper is for testing services not components. For Component testing you must still bootstrap a hybrid app. See UpgradeModule or downgradeModule for more information.

The resulting configuration does not wire up AngularJS digests to Zone hooks. It is the responsibility of the test writer to call $rootScope.$apply, as necessary, to trigger AngularJS handlers of async events from Angular.

The helper sets up global variables to hold the shared Angular and AngularJS injectors.

Here is the example application and its unit tests that use createAngularTestingModule and createAngularJSTestingModule.

      
      /**
 * @license
 * Copyright Google LLC All Rights Reserved.
 *
 * Use of this source code is governed by an MIT-style license that can be
 * found in the LICENSE file at https://angular.io/license
 */

import {TestBed} from '@angular/core/testing';
import {
  createAngularJSTestingModule,
  createAngularTestingModule,
} from '@angular/upgrade/static/testing';

import {HeroesService, ng1AppModule, Ng2AppModule} from './module';

const {module, inject} = (window as any).angular.mock;

describe('HeroesService (from Angular)', () => {
  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [createAngularTestingModule([ng1AppModule.name]), Ng2AppModule],
    });
  });

  it('should have access to the HeroesService', () => {
    const heroesService = TestBed.inject(HeroesService);
    expect(heroesService).toBeDefined();
  });
});

describe('HeroesService (from AngularJS)', () => {
  beforeEach(module(createAngularJSTestingModule([Ng2AppModule])));
  beforeEach(module(ng1AppModule.name));

  it('should have access to the HeroesService', inject((heroesService: HeroesService) => {
    expect(heroesService).toBeDefined();
  }));
});