Context
I created an ApiService
class to be able to handle our custom API queries, while using our own serializer + other features.
ApiService
's constructor signature is:
constructor(metaManager: MetaManager, connector: ApiConnectorService, eventDispatcher: EventDispatcher);
MetaManager
is an injectable service that handles api's metadatas.ApiConnectorService
is a service which is wrappingHttp
to add our custom headers and signature system.EventDispatcher
is basically Symfony's event dispatcher system, in typescript.
Problem
When I test the ApiService
, I do an initialization in beforeEach
:
beforeEach(async(() => {
TestBed.configureTestingModule({
imports : [
HttpModule
],
providers: [
ApiConnectorService,
ApiService,
MetaManager,
EventDispatcher,
OFF_LOGGER_PROVIDERS
]
});
}));
and it works fine.
Then I add my second spec file, which is for ApiConnectorService
, with this beforeEach
:
beforeEach(async(() => {
TestBed.configureTestingModule({
imports : [HttpModule],
providers: [
ApiConnectorService,
OFF_LOGGER_PROVIDERS,
AuthenticationManager,
EventDispatcher
]
});
}));
And all the tests fail with this error:
Error: Can't resolve all parameters for ApiService: (MetaManager, ?, EventDispatcher).
- If I remove
api-connector-service.spec.ts
(ApiConnectorService
's spec file) from my loaded tests,ApiService
's tests will succeed. - If I remove
api-service.spec.ts
(ApiService
's spec file) from my loaded tests,ApiConnectorService
's tests will succeed.
Why do I have this error? It seems like the context between my two files are in conflict and I don't know why and how to fix this.
The issue wasn't really solved in the chosen answer, which is really just a recommendation for writing tests, but rather in the comments, and you have to follow a link and search for it there. Since I had another issue with the same error, I'll add both solutions here.
If you have a barrel (index.ts or multi export file) like this:
Then you could get an error like
EXCEPTION: Can't resolve all parameters for MyComponent: (?)
.To fix it, you have to export the service before the component:
The same error can happen due to a
circular dependency
which causes anundefined
service import. To check,console.log(YourService)
after importing it (in your test file - where the issue is happening). If it's undefined, you may have made an index.ts file (barrel) exporting both the service and the file using it (component/effect/whatever you're testing) - by importing the service from the index file where both are exported (making it full circle).Find that file and import the service you need directly from
your.service.ts
file instead of the index.It's because the
Http
service can't be resolved from theHttpModule
, in a test environment. It is dependent on the platform browser. You shouldn't even be trying to to make XHR calls anyway during the tests.For this reason, Angular provides a
MockBackend
for theHttp
service to use. We use this mock backend to subscribe to connections in our tests, and we can mock the response when each connection is made.Here is a short complete example you can work off of