I have written a service MyService
and a mocked version of it – MyServiceMock
, for unit tests providing and using the mocked service's result until my BE is ready.
I used MyService
as alias of MyServiceMock
, so I will not have to change my code in the future when the BE is ready:
In my services module I use:
import { MyServiceMock as MyService } from './my-service/my-service-mock.service';
const providers: Array<Provider> = [
MyService,
...
And the same in my components:
import { MyServiceMock as MyService } from './my-service/my-service-mock.service';
...
constructor(
myServce: myService,
...
And it worked fine with ng serve
and ng build
... until I served the build. There was a runtime error in the browser MyService is undefined
. But note that when served through Angular ng serve
all worked fine. And there were no warnings and errors in the ng build
| ng build --prod
build processes.
The solution below is simple but I would appreciate any explanations of that.
Thanks!