3
votes

here my test :

fdescribe('ValueService', () => {

  it('#getValue should return real value', () => {
    expect(true).toBeTruthy();
   });
});

And i have this error :

Failed: Cannot configure the test module when the test module has already been instantiated. Make sure you are not using inject before R3TestBed.configureTestingModule. Error: Cannot configure the test module when the test module has already been instantiated. Make sure you are not using inject before R3TestBed.configureTestingModule. at TestBedRender3.prototype.assertNotInstantiated (http:/localhost:9876/_karma_webpack_/vendor.js:76150:13) at TestBedRender3.prototype.configureTestingModule (http:/localhost:9876/_karma_webpack_/vendor.js:76045:9) at TestBedRender3.configureTestingModule (http:/localhost:9876/_karma_webpack_/vendor.js:75933:9) at Anonymous function (http:/localhost:9876/_karma_webpack_/main.js:27942:5) at ZoneDelegate.prototype.invoke (http:/localhost:9876/_karma_webpack_/polyfills.js:13991:17) at AsyncTestZoneSpec.prototype.onInvoke (http:/localhost:9876/_karma_webpack_/vendor.js:124156:21) at ProxyZoneSpec.prototype.onInvoke (http:/localhost:9876/_karma_webpack_/vendor.js:125618:17) at ZoneDelegate.prototype.invoke (http:/localhost:9876/_karma_webpack_/polyfills.js:13991:17) at Zone.prototype.runGuarded (http:/localhost:9876/_karma_webpack_/polyfills.js:13761:25) at runInTestZone (http:/localhost:9876/_karma_webpack_/vendor.js:124278:13) NullInjectorError: R3InjectorError(DynamicTestModule)[IRechercheService -> IRechercheService]: NullInjectorError: No provider for IRechercheService! error properties: Object({ ngTempTokenPath: null, ngTokenPath: [ 'IRechercheService', 'IRechercheService' ] }) at at NullInjector.prototype.get (http:/localhost:9876/_karma_webpack_/vendor.js:42155:13) at R3Injector.prototype.get (http:/localhost:9876/_karma_webpack_/vendor.js:52781:13) at R3Injector.prototype.get (http:/localhost:9876/_karma_webpack_/vendor.js:52781:13) at NgModuleRef$1.prototype.get (http:/localhost:9876/_karma_webpack_/vendor.js:66043:9) at get (http:/localhost:9876/_karma_webpack_/vendor.js:64279:13) at getOrCreateInjectable (http:/localhost:9876/_karma_webpack_/vendor.js:45064:17) at ɵɵdirectiveInject (http:/localhost:9876/_karma_webpack_/vendor.js:55585:5) at IdentificationRqComponent_Factory (Function code:3:3) at getNodeInjectable (http:/localhost:9876/_karma_webpack_/vendor.js:45172:13) at instantiateRootComponent (http:/localhost:9876/_karma_webpack_/vendor.js:49487:5) Failed: Cannot configure the test module when the test module has already been instantiated. Make sure you are not using inject before R3TestBed.configureTestingModule. Error: Cannot configure the test module when the test module has already been instantiated. Make sure you are not using inject before R3TestBed.configureTestingModule. at TestBedRender3.prototype.assertNotInstantiated ## Heading ##(http:/localhost:9876/_karma_webpack_/vendor.js:76150:13) at TestBedRender3.prototype.configureTestingModule (http:/localhost:9876/_karma_webpack_/vendor.js:76045:9) at TestBedRender3.configureTestingModule (http:/localhost:9876/_karma_webpack_/vendor.js:75933:9) at Anonymous function (http:/localhost:9876/_karma_webpack_/main.js:95084:5) at ZoneDelegate.prototype.invoke (http:/localhost:9876/_karma_webpack_/polyfills.js:13991:17) at AsyncTestZoneSpec.prototype.onInvoke (http:/localhost:9876/_karma_webpack_/vendor.js:124156:21) at ProxyZoneSpec.prototype.onInvoke (http:/localhost:9876/_karma_webpack_/vendor.js:125618:17) at ZoneDelegate.prototype.invoke (http:/localhost:9876/_karma_webpack_/polyfills.js:13991:17) at Zone.prototype.runGuarded (http:/localhost:9876/_karma_webpack_/polyfills.js:13761:25) at runInTestZone (http:/localhost:9876/_karma_webpack_/vendor.js:124278:13) NullInjectorError: R3InjectorError(DynamicTestModule)[IDroitsObligationsService -> IDroitsObligationsService]: NullInjectorError: No provider for IDroitsObligationsService! error properties: Object({ ngTempTokenPath: null, ngTokenPath: [ 'IDroitsObligationsService', 'IDroitsObligationsService' ] }) at at NullInjector.prototype.get (http:/localhost:9876/_karma_webpack_/vendor.js:42155:13) at R3Injector.prototype.get (http:/localhost:9876/_karma_webpack_/vendor.js:52781:13) at R3Injector.prototype.get (http:/localhost:9876/_karma_webpack_/vendor.js:52781:13) at NgModuleRef$1.prototype.get (http:/localhost:9876/_karma_webpack_/vendor.js:66043:9) at get (http:/localhost:9876/_karma_webpack_/vendor.js:64279:13) at getOrCreateInjectable (http:/localhost:9876/_karma_webpack_/vendor.js:45064:17) at ɵɵdirectiveInject (http:/localhost:9876/_karma_webpack_/vendor.js:55585:5) at DroitsObligationsComponent_Factory (Function code:3:3) at getNodeInjectable (http:/localhost:9876/_karma_webpack_/vendor.js:45172:13) at instantiateRootComponent (http:/localhost:9876/_karma_webpack_/vendor.js:49487:5)

here my dependencies :

"devDependencies": { "@angular-devkit/build-angular": "~0.803.26", "@angular/cli": "~9.1.6", "@angular/compiler-cli": "~9.1.9", "@angular/language-service": "~9.1.9", "@types/datatables.net-buttons": "~1.4.0", "@types/jasmine": "2.8.3", "@types/jasminewd2": "~2.0.5", "@types/node": "~7.10.11", "@typescript-eslint/eslint-plugin": "~2.34.0", "@typescript-eslint/parser": "~2.34.0", "azure-devops-node-api": "^9.0.1", "codelyzer": "~4.3.0", "config": "~2.0.1", "copyfiles": "^2.1.1", "current-git-branch": "^1.1.0", "eslint": "~7.0.0", "eslint-plugin-react": "~7.20.0", "jasmine-core": "~3.5.0", "jasmine-spec-reporter": "~5.0.2", "karma": "~5.0.9", "karma-chrome-launcher": "~3.1.0", "karma-coverage-istanbul-reporter": "~3.0.2", "karma-ie-launcher": "~1.0.0", "karma-jasmine": "~3.1.1", "karma-jasmine-html-reporter": "~1.5.4", "karma-junit-reporter": "~2.0.1", "ng-mocks": "~9.3.0", "prompt": "^1.0.0", "protractor": "~7.0.0", "ts-node": "~4.1.0", "typescript": "~3.7.5" }

1
please add the complete spec file contentGérôme Grignon
Hi Saroste, just a friendly tip, use the CODE button to add code, it will help a lot to people reading your posts and help will be more probable.mrbarletta
Thanks mrbarletta @GérômeGrignon i only have this in my spec file.Saroste
Is it the only test with a focus with 'fdescribe' ? It sounds like you have a another test with missing dependencies begin triggered too by karma.Gérôme Grignon
Yes @GérômeGrignon it's the only one. How can i identify the test with missing dependencies?Saroste

1 Answers

2
votes

As discussed with the author, the problem arise when the TestBed is initialized outside a before each when having two spec files or more.

For example:

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [
        RouterTestingModule
      ],
      declarations: [
        AppComponent
      ],
    }).compileComponents();
  }));
describe('AppComponent', () => {
  it('should create the app', () => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.componentInstance;
    expect(app).toBeTruthy();
  });
 });

will instanciate a TestBed beforeEach tests, not only the spec file. Hence, if you have another .spec with a TestBed and a beforeEach it will be interpreted as 2 TestBed instanciated like this:

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [
        RouterTestingModule
      ],
      declarations: [
        AppComponent
      ],
    }).compileComponents();
  }));
  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [
        RouterTestingModule
      ],
      declarations: [
        AppComponent
      ],
    }).compileComponents();
  }));
describe('AppComponent', () => {
  it('should create the app', () => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.componentInstance;
    expect(app).toBeTruthy();
  });
 });

The error

Failed: Cannot configure the test module when the test module has already been instantiated.

will be right, since you instanciate two TestBed (but in two spec files).

To solve this problem, you must always put the TestBed definition (so the beforeEach) in a describe like this:

describe('AppComponent', () => {
  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [
        RouterTestingModule
      ],
      declarations: [
        AppComponent
      ],
    }).compileComponents();
  }));

  it('should create the app', () => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.componentInstance;
    expect(app).toBeTruthy();
  });
});