Saroste
Saroste

Reputation: 163

What causes "Cannot configure the test module when the test module has already been instantiated"?

here is my test :

describe('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.

Upvotes: 16

Views: 19833

Answers (3)

Pavan
Pavan

Reputation: 451

This may also happen when we use a describe inside another describe as show below

describe('parent suite',()=>{
beforeEach(()=>{
// configure Testing module
// and some injections
})
// some tests....
describe('child suite',()=>{

beforeEach(()=>{
// here in parent suite if we perform any injections we have to use **TestBed.resetTestingModule()** before configuring another testing module`enter code here`
})


})
})
Hope this helps..

Upvotes: 2

Sameera
Sameera

Reputation: 1113

Please note that you may experience this error if the following conditions are true, even if you properly have your TestBed.configureTestingModule inside a describe:

  1. Your project is on Angular 13+
  2. You are using NgRx
  3. You are using provideMockStore in your test.

This issue is discussed here.

The fix is to add teardown: { destroyAfterEach: false } to your module configuration.

Upvotes: 15

JFPicard
JFPicard

Reputation: 5168

As discussed with the author, the problem arise when the TestBed is initialized outside a describe 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();
  });
});

Upvotes: 14

Related Questions