Those of you who are getting into angular testing, normally face this error. Usually this happen when we set the testing environment for Angular. These are the lines
These are the culprits
TestBed.initTestEnvironment(
BrowserDynamicTestingModule,
platformBrowserDynamicTesting()
)
// or wrapped in beforeAll
beforeAll(() {
TestBed.initTestEnvironment(
BrowserDynamicTestingModule,
platformBrowserDynamicTesting()
)
})
Diagnosing & Solution
I might not have written this post if the error message was easy to understand
Your test might run just fine, even if this error occurs. All it says that above lines ran twice in your codebase.
Just look at the tests.ts file

Now let’s see the actual content of test.ts. I really don’t know whether this configuration came from the beginning. Or were they added in recent angular version i-e version 6

So next time don’t include them in your jasmine beforeEach block of your spec files
4 replies on “Fix Angular Error: Cannot set base providers because it has already been called”
[…] no doubt we do great sense of freedom once our store is ready. I only found the bug when I started testing my actions. Which also gave the positive reinforcement to continue testing. As testing makes you a better […]
great…works for me..
i followed the same steps as you said and it resolves one issue but there is another problem when implementing that code is, it reduces the percentage of code coverage. it totally ignores the test cases. then how should i solve these two problems.
Thanks for commenting. It’s been quite a while since I wrote this code, if time permits I will update it.