0

I have an AngularJS service which has a dependency that has been converted to a .ts Angular service. In my main entry point file, I did this:

angular
  .module('MyNg1Module')
  .factory('MyDowngradedService', downgradeInjectable(MyFancyModernAngularService))

While this works great in the actual app, when running the tests (karma specs) for that service, I got unknown provider errors when it was trying to inject that MyDowngradedService singleton in AngularJS... After some searching I found: https://angular.io/api/upgrade/static/testing/createAngularJSTestingModule, so in my test's entry point file, I did:

import AppModule from 'entrypoints/main';
import { createAngularJSTestingModule } from '@angular/upgrade/static/testing';

beforeEach(angular.mock.module(createAngularJSTestingModule([AppModule])));

and then in the spec, I added:

 beforeEach(angular.mock.module('MyNg1Module'))

Believing that would give the AngularJS injector access to the downgraded module... However, when I run the spec I get:

Error while instantiating injectable 'MyDowngradedService': Need to call TestBed.initTestEnvironment() first

There is nothing in those angular docs about TestBed.initTestEnvironment()

patrick
  • 9,290
  • 13
  • 61
  • 112

1 Answers1

0

Turns out I needed to do:

import { destroyPlatform } from '@angular/core';
import 'zone.js/dist/zone';
import 'zone.js/dist/zone-testing';
import { getTestBed } from '@angular/core/testing';
import { createAngularJSTestingModule } from '@angular/upgrade/static/testing';
import { BrowserDynamicTestingModule, platformBrowserDynamicTesting, } from '@angular/platform-browser-dynamic/testing';

beforeEach(() => {
  destroyPlatform();
  getTestBed().initTestEnvironment(
    BrowserDynamicTestingModule,
    platformBrowserDynamicTesting()
  );
});
patrick
  • 9,290
  • 13
  • 61
  • 112