Issue
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
beforeR3TestBed.configureTestingModule
. Error: Cannot configure the test module when the test module has already been instantiated. Make sure you are not usinginject
beforeR3TestBed.configureTestingModule
.
Solution
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();
});
});
Answered By - JFPicard
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.