5

I am using angular library with secondary entrypoint My library folder structure is

  • my-library
    • secondary-entrpoint 1
      • service 1
    • secondary-entrypoint 2
      • service 2
    • src

And I used this command ng test my-library --code-coverage

The secondary entry points' service test doesn't execute.

Then I tried, changing "test.ts" context path('./') with '../' which is under the src folder. Tests were executed but code coverage doesn't create.

How can I execute tests for a secondary entry point?

1 Answers1

9

That is because the source root of your project is pointing to projects/your-library/src and when running tests it does not take into account the other entry points.

To fix this just change the sourceRoot property in your "angular.json" file to projects/your-library, everything else should still work fine.

  • I have multiple secondary entrpoints. So I can't set sourceroot property on angular.json – Mert yılmaz Sep 23 '20 at 12:58
  • 2
    All your secondary entry points have the same root, so following your example in your angular.json you should set `"sourceRoot": "projects/my-library"` instead of `"sourceRoot": "projects/my-library/src"` that is what Angular sets as default – Andres Ballester Oct 22 '20 at 10:34
  • I got Mert's answer to work by also moving the test.ts file to the "project/my-library" level AND changing my tsconfig.spec.ts files from "src/test.ts" to "test.ts" – Ken Mar 12 '21 at 16:47
  • It worked great for me. tnx man :) – Shadowalker Aug 27 '22 at 14:13
  • After moving test.ts 1 level up, no need to change `sourceRoot` in angular.json, changing test/options/main path is enough. – Victor Zakharov Sep 04 '22 at 16:02
  • 1
    Actually, despite tests run, they don't reflect in the code coverage report. With or without changes to `sourceRoot`, only primary endpoint shows up in coverage. Any way to fix it? – Victor Zakharov Sep 14 '22 at 18:21