I'm updating my unit tests for Angular2 RC5. The changelog notes the following breaking change:
addProviders
[is deprecated], useTestBed.configureTestingModule
instead
But this seems to take error only when attempting to include a service in a test. Where my unit test used to do the following:
beforeEach(() => addProviders([
MyService,
MockBackend,
...
]));
it should now configure the test module:
TestBed.configureTestingModule({
providers: [
StoryService,
MockBackend,
...
]
});
But that now throws an error
Service: MyService encountered a declaration exception FAILED
Error: Cannot configure the test module when the test module has already been instantiated. Make sure you are not using
inject
beforeTestBed.configureTestingModule
.
I have checked that inject
isn't been called before the configureTestingModule
. This doesn't affect any other component/directive tests, they seem to pass just fine. How can I resolve this error for unit testing a service with RC5? I realize that I may have to wait until the the testing documentation are updated for RC5 but any insight into possible solutions would be much appreciated.
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();
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With