Get code coverage only for a folder in Cypress
Asked Answered
I

1

1

I am using Cypress and Nyc configurations. My folder structure looks like this:

|/project
| /coverage/
   /lcov-report
    /index.html
 
|/cypress
| /main
   /car
    /car.spec.tsx
    /color.spec.tsx
     ...
| /integration

I need a solution to get inside the index.html only the tests coverage from main folder. So as a result in index.html i need to see only the coverage for the tests that was written there.
I noticed that NYC docs. have some configurations https://github.com/istanbuljs/nyc#common-configuration-options . But there i can specifiy only the files that contains features in include, but in each folder from my project i could have a test file and i can not specify every time the each new file only to get the coverage.
Question: Is there a solution to get the coverage only for the main folder or to get the coverage for the files that have already written a spec test? Or to get a coverage for the file that have spec.tsx extension?

Ireland answered 25/3, 2022 at 7:12 Comment(14)
@user16695029, could you help?Ireland
What does "But there i can specifiy only the files that contains features in include" mean?Katti
@kegne, I mean that according documentation i can specify the files that contain the files with code implementation, not with tests, so i can not specify in include for example the files with spec.tsx because they contain only tests. This is how i understood. Do i understand correct?Ireland
Surely you select a subset of tests in the Cypress configuration?Katti
@kegne, what do you mean?Ireland
Your explanation is not at all clear, but this so i can not specify in include for example the files with spec.tsx implies you want to select tests not code files.Katti
Let us continue this discussion in chat.Ireland
@kegne, i need the coverage only for these file which i have written test. So i need the coverage only for these tests that are located in main folder. Please let me know if you understood.Ireland
@kegne, could you help? It will save my day.Ireland
So you written two test for only some of the src files, you want coverage only for those src files?Pyrogen
@Vispo Yes, all may tests are in main folder from /cypress folder. So, yes, i need the coverage only for these files that have tests. But i don't want to specific the list of the files in include: [] being hardcoded, i want somehow to get the coverage report only for the tests that are in main folder. Could you help please? It will help me a lot.Ireland
Could you help?Ireland
The obvious thing is just run the tests in main. But the premise of your question is flawed - Cypress tests are not unit tests, there is no 1:1 between test and src. You have to take whatever code-coverage sees when it runs your tests.Pyrogen
@Vispo, so i can not get only the test coverage for the tests that was written in main?Ireland
N
6

Please read the documentation, from the above page chose the link to Selecting files for coverage.

Globs are matched using minimatch.

Cypress explains minimatch here.

It's the same as many other places in javascript you will have seen files specified

include: [ '/main/**']
Nascent answered 25/3, 2022 at 8:8 Comment(1)
this include means that will set the coverage only for the tests that was written in main folder? I ask you because i understand that include means that in the coverage will be included the files that contain features but not tests. So can i add there the spec.tsx files?Ireland

© 2022 - 2024 — McMap. All rights reserved.