vuejs / vue-jest

Jest Vue transformer
MIT License
748 stars 156 forks source link

Using v8 as the coverageProvider, used files without tests are being reported with 100% coverage #543

Open pasanenf opened 1 year ago

pasanenf commented 1 year ago

I am experiencing the same issue as #422 with some Vue files not included in the coverage report, so I decided to try changing to v8 as recommended. However, using v8, used files without tests are being reported as having 100% coverage.

In the repo below, there are tests for two components, RedirectLink and Anchor. There is another component (ArrowLink) that is not used, and it is being correctly reported with 0% coverage.

Repo: https://github.com/pasanenf/jest-coverage-bug


RedirectLink is using Anchor to display links, and Anchor is being stubbed in the tests. The stub is not the Anchor component mentioned above, but another one under __mocks__. (Even if Anchor is not stubbed, coverage is the same.)

shallowMount(RedirectLink, {
  global: { stubs: { Anchor } },
  ...
});

Anchor has also got tests, but it is only using standard HTML. There are a few branches and functions in the code, so I'd expect these to be counted in the report, even if I'm not actually testing Anchor.

When testing only the RedirectLink component, the Anchor component is reported with 100% coverage.

$ yarn test __tests__/components/RedirectLink.test.js

------------------|---------|----------|---------|---------|-------------------
File              | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
------------------|---------|----------|---------|---------|-------------------
All files         |   69.86 |        0 |       0 |   69.86 |
 Anchor.vue       |     100 |      100 |     100 |     100 |
 ArrowLink.vue    |       0 |        0 |       0 |       0 | 1-21
 RedirectLink.vue |      96 |      100 |       0 |      96 | 23
------------------|---------|----------|---------|---------|-------------------

However, test everything, and the coverage changes.

$ yarn test

------------------|---------|----------|---------|---------|-------------------
File              | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
------------------|---------|----------|---------|---------|-------------------
All files         |   68.49 |    33.33 |       0 |   68.49 |
 Anchor.vue       |   96.29 |       50 |       0 |   96.29 | 25
 ArrowLink.vue    |       0 |        0 |       0 |       0 | 1-21
 RedirectLink.vue |      96 |      100 |       0 |      96 | 23
------------------|---------|----------|---------|---------|-------------------

Is this the expected behavior? I'd expect the coverage for Anchor to be the same, even if I'm not specifically testing Anchor. Btw, removing the tests for Anchor and running yarn test also results in 100% coverage for Anchor.

steele-cjf commented 1 year ago

yeah,same question,has any solution?

michaelsimmonds commented 10 months ago

Running into this issue too. Coverage seems to default to 100% when components which use <script setup> that don't have their own tests run are imported into a file which is being tested. If their own tests are run then coverage is calculated properly.

If you change the component to not use <script setup> then it doesn't default to 100% but this is not really a viable solution.