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

c8 coverage seems broken when using import.meta.env #2540

Closed
6 tasks done
Polluux opened this issue Dec 20, 2022 · 2 comments · Fixed by #2584
Closed
6 tasks done

c8 coverage seems broken when using import.meta.env #2540

Polluux opened this issue Dec 20, 2022 · 2 comments · Fixed by #2584
Labels
feat: coverage Issues and PRs related to the coverage feature

Comments

@Polluux
Copy link

Polluux commented Dec 20, 2022

Describe the bug

Hi,
I'm using c8 for test coverage in a Vite/Vue project and encountered an issue when using import.meta.env.
Vue project is written in OptionAPI, and .vue files import .ts files.
When using import.meta.env in an imported .ts file, coverage report indicate only 1 line, and 100% coverage.
Commenting the line causes the coverage report to display correct values.
(Running test with istanbul does not raise this issue.)
(Using script in .vue as SFC does not raise this issue.)

Result:
Statements : 100% ( 7/7 )
Branches : 100% ( 0/0 )
Functions : 100% ( 0/0 )
Lines : 100% ( 7/7 )

Expected Result:
Statements : 94.73% ( 18/19 )
Branches : 50% ( 1/2 )
Functions : 100% ( 1/1 )
Lines : 94.73% ( 18/19 )

Reproduction

Here is a repository to reproduce : https://github.com/Polluux/demo-c8-issues
(Note : This repository contains multiple issues for c8)

System Info

System: 
  Ubuntu 22.04.1 LTS x86_64 GNU/Linux
Node:
  v16.15.1
Dependencies:
  "vue": "^3.2.45"
  "@vitejs/plugin-vue": "^4.0.0",
  "@vitest/coverage-c8": "^0.26.0",
  "@vue/test-utils": "2.2.6",
  "c8": "^7.12.0",
  "jsdom": "^20.0.3",
  "typescript": "^4.9.4",
  "vite": "^4.0.2",
  "vitest": "^0.26.0",
  "vue-tsc": "^1.0.14"

Used Package Manager

npm

Validations

@sheremet-va sheremet-va added bug feat: coverage Issues and PRs related to the coverage feature labels Dec 20, 2022
@Polluux
Copy link
Author

Polluux commented Dec 20, 2022

Using process.env instead of import.meta.env does not raise this issue neither.

@AriPerkkio
Copy link
Member

I'm able to reproduce the issue and can fix it by modifying the C8 provider's map.sourcesContent. Below on the left side is the fixed case, right one is the original one. @Polluux I guess the left side looks as expected?

image

Manipulating the map.sourcesContent directly does not seem like right solution though. Need to study the root cause in more details.
This is likely causing some of the other reported C8 issues.

AriPerkkio added a commit to AriPerkkio/vitest that referenced this issue Dec 31, 2022
AriPerkkio added a commit to AriPerkkio/vitest that referenced this issue Dec 31, 2022
sheremet-va pushed a commit that referenced this issue Dec 31, 2022
…lenames (#2584)

* test: add failing test case for #2540

* fix(coverage): env-replacer to remove query params from sourcemaps
- Fixes bugs where vue components lost coverage when import.meta.env is used
@github-actions github-actions bot locked and limited conversation to collaborators Jun 8, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feat: coverage Issues and PRs related to the coverage feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants