0

I am using a library (FontAwesome for Angular) which provides two services under the same name:

Lets say the first one is the real one:

@Injectable({
  providedIn: 'root',
})
export class Service { }

@NgModule({
  exports: [...],
})
export class Module {}

and the second one provides a mock version

@Injectable({
  providedIn: 'root',
})
export class MockService implements Service { }

@NgModule({
  exports: [...],
  provide: [{ provide: Service, useExisting: MockService }],
})
export class MockModule {}

Normally you would either use Module or MockModule. I now ran into a case where I would like to use both of them (arguably not very smart but it is in some util testing modules to reduce boilerplate code).

Is it possible to import Module and MockModule but having access to the actual Service and not the mock implementation MockService? So far I played around with the order of the modules but any order results in the MockService being injected.

Would a change in the services from providedIn: 'root' to instead provide them in the module directly make this possible or is there any other way to get access to the real service again?

Simon
  • 194
  • 13
  • 1
    As you suggest `@Injectable({ providedIn: 'root', })` is incompatible with your scenario. Remove the configuration from the decorator and explicitly register the service in question – Aluan Haddad Apr 15 '22 at 20:24
  • Thanks for the input, I guess I will have to open a PR for the library then. – Simon Apr 15 '22 at 20:27
  • That's probably a good idea, but why not override the registration in your own app module's config? – Aluan Haddad Apr 15 '22 at 20:28
  • The modules are also provided by the Library. So I cannot change how the services are provided. – Simon Apr 15 '22 at 20:31

0 Answers0