Actually you need not just create a spy but also execute it to increase coverage. A spy replaces the spied function with a stub. If you want this spy function to be called normally, you need to add .and.callThrough() to your spy.
spyOn(EnvironmentService, 'isProduction').and.callThrough()
You can also read more here: about spy and callThrough
Answer from Tatsiana on Stack OverflowNot able to increase code coverage in Angular Unit testing - Stack Overflow
angular - How do I get hundred percent code coverage with my unit testing on this component and access the else part? - Stack Overflow
angular - How to increase code coverage to 80 percentage in Jasmine test case - Stack Overflow
angular5 - How to implement code coverage for angular 5? - Stack Overflow
Videos
Actually you need not just create a spy but also execute it to increase coverage. A spy replaces the spied function with a stub. If you want this spy function to be called normally, you need to add .and.callThrough() to your spy.
spyOn(EnvironmentService, 'isProduction').and.callThrough()
You can also read more here: about spy and callThrough
spyOn(EnvironmentService, 'isProduction'); overwrites the service method and then EnvironmentService.isProduction(); is calling a spy instead of your method. Thus your method isn't called. valid test would be removing spy and will looke in some way like expect(EnvironmentService.isProduction()).toBe(false)
I have researched through various articles and the easiest way i found was:-
$ npm install karma karma-jasmine karma-chrome-launcher karma-jasmine-html-reporter karma-coverage-istanbul-reporter$ ng test --code-coverageMake sure you have
http-serverinstalled (npm install http-server -g) and then$ http-server -c-1 -o -p 9875 ./coverage
Open it in a chrome browser only.
I don't know how to set it up from scratch, but I can tell you that projects generated with the angular-cli have already configured the code coverage plugin. Check https://github.com/angular/angular-cli/wiki/test, you can run ng test -cc.
If you want to inspect how it was configured, generate a project with the angular-cli and execute on the root folder ng eject, that will allow you to see the webpack configuration file.