Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add C8 Coverage support for Vue template logic #751

Closed
4 tasks done
JessicaSachs opened this issue Feb 14, 2022 · 11 comments · Fixed by vitejs/vite#6972
Closed
4 tasks done

Add C8 Coverage support for Vue template logic #751

JessicaSachs opened this issue Feb 14, 2022 · 11 comments · Fixed by vitejs/vite#6972
Assignees
Labels

Comments

@JessicaSachs
Copy link
Contributor

JessicaSachs commented Feb 14, 2022

Clear and concise description of the problem

Currently, there's no support for reporting coverage of SFC files where the logic is performed in the template instead of the script block.

For example, in a test, if you mount this component with prop msg, the run will show 100% coverage, even though line No. 11 is not covered.

HelloWorld.spec.ts

Screen Shot 2022-02-14 at 5 37 20 AM

Logic in template

Screen Shot 2022-02-14 at 5 35 04 AM

Logic in script

When you refactor the file to pull the logic into the script tag instead of the template, the coverage report correctly reports the uncovered lines of code.

Screen Shot 2022-02-14 at 5 34 19 AM

Missing coverage

Screen Shot 2022-02-14 at 5 32 17 AM

Suggested solution

No idea. Jest + Istanbul doesn't solve this either.

Alternative

No response

Additional context

No response

Validations

@Demivan Demivan self-assigned this Feb 14, 2022
@Demivan
Copy link
Member

Demivan commented Feb 15, 2022

Template coverage is already working and is trying to mark v-if as not covered:

image

Problem is Vite sourcemaps. Not sure if this is the issue with Vite itself or Vue plugin.

image

@patak-dev Is there a way to investigate Vite sourcemaps after each plugin? Something like https://github.com/antfu/vite-plugin-inspect but for sourcemaps?

@Demivan
Copy link
Member

Demivan commented Feb 18, 2022

@JessicaSachs Vite PR created.

Coverage with fix:
vitest-fixed

@Demivan
Copy link
Member

Demivan commented Feb 18, 2022

Fixed in @vitejs/plugin-vue v2.2.1

update: Not fixed yet :)

@patak-dev
Copy link
Member

There was an issue while publishing, it will be fixed when we solve it

@patak-dev
Copy link
Member

Publishing issue is fixed, solved in https://www.npmjs.com/package/@vitejs/plugin-vue/v/2.2.2

@JessicaSachs
Copy link
Contributor Author

So cool! I can't wait to give it a try.

@ebisbe
Copy link

ebisbe commented Oct 23, 2022

@JessicaSachs did you try it?

@Demivan I'm seeing different results just by swapping C8 and Istanbul.
This first screenshot is with Istanbul
Screenshot 2022-10-23 at 19 41 49
The second one with C8
Screenshot 2022-10-23 at 19 42 08

"@vitejs/plugin-vue": "^3.1.2",
"@vitejs/plugin-vue": "^3.1.2",
"@vitest/coverage-c8": "^0.24.3",
"@vitest/coverage-istanbul": "^0.24.3",
"vite": "^3.1.8",
"vitest": "^0.24.3"

I'm seeing different results as well with different tests. For now I'll stick with Istanbul as it's the accurate one.

@Demivan
Copy link
Member

Demivan commented Oct 24, 2022

@ebisbe Tried your example with c8. Seems to be working correctly.
image
Maybe you have some other Vite plugins that are modifying code but not providing correct sourcemaps.

@ebisbe
Copy link

ebisbe commented Oct 24, 2022

@Demivan I was thinking something like that might happen. I'll review this afternoon removing plugins.

@ebisbe
Copy link

ebisbe commented Oct 24, 2022

The issue is with @antfu unplugin-vue-components. There are a few unresolved issues so I'll stick with istanbul as it's working fine.

@tw-eugeneoei
Copy link

tw-eugeneoei commented Feb 14, 2023

hello @Demivan @patak-dev . understand that this issue has been closed. however, i am facing a similar issue where the Branch coverage in c8 report remains at 100% when using the v-if and v-else directives to conditionally render a block within VueJS.

here are the dependencies versions:

  • vue: ^3.2.45
  • vitest: ^0.25.6
  • @testing-library/vue: ^6.6.1
  • @vitest/coverage-c8: ^0.28.5,

the vue file is as follows:

<script setup lang="ts">
import { ref } from "vue";

const showDefaultMessage = ref(true);

const toggleMessage = () => {
  showDefaultMessage.value = !showDefaultMessage.value;
};
</script>

<template>
  <main>
    <h1 v-if="showDefaultMessage">Hello world</h1>
    <h1 v-else>Hola Human</h1>
    <button @click="toggleMessage">Toggle Message</button>
  </main>
</template>

a test has been written to check that Hello world renders correctly on load. no test has been written for the v-else block.

describe("HomeView", async () => {
  it("should display 'Hello world' on load", () => {
    const { getByRole } = render(HomeView);
    getByRole("heading", { name: "Hello world" });
  });
});

however, the coverage on Branch remains at 100%

coverage-result

is this an expected or known behaviour? I have created a minimally reproducible repo and it can be found here.

also, using istanbul does show a different Branch coverage. below is the screenshot.

coverage-result-istanbul

@github-actions github-actions bot locked and limited conversation to collaborators Jun 6, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants