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

fix(coverage): env-replacer to add filenames into sourcemaps #2338

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/vitest/src/node/plugins/envReplacer.ts
Expand Up @@ -8,7 +8,7 @@ export const EnvReplacerPlugin = (): Plugin => {
return {
name: 'vitest:env-replacer',
enforce: 'pre',
transform(code) {
transform(code, id) {
if (!/\bimport\.meta\.env\b/g.test(code))
return null

Expand All @@ -27,7 +27,7 @@ export const EnvReplacerPlugin = (): Plugin => {
if (s) {
return {
code: s.toString(),
map: s.generateMap({ hires: true }),
map: s.generateMap({ hires: true, source: id }),
}
}
},
Expand Down
7 changes: 7 additions & 0 deletions test/coverage-test/coverage-test/coverage.istanbul.test.ts
Expand Up @@ -50,3 +50,10 @@ test('implicit else is included in branch count', async () => {
expect(fileCoverage.b).toHaveProperty('0')
expect(fileCoverage.b['0']).toHaveLength(2)
})

test('file using import.meta.env is included in report', async () => {
const coveragePath = resolve('./coverage/src')
const files = fs.readdirSync(coveragePath)

expect(files).toContain('importEnv.ts.html')
})
3 changes: 3 additions & 0 deletions test/coverage-test/src/importEnv.ts
@@ -0,0 +1,3 @@
export function useImportEnv() {
return import.meta.env.SOME_VARIABLE == null
}
5 changes: 5 additions & 0 deletions test/coverage-test/test/coverage.test.ts
@@ -1,6 +1,7 @@
import { expect, test } from 'vitest'
import { pythagoras } from '../src'
import { implicitElse } from '../src/implicitElse'
import { useImportEnv } from '../src/importEnv'

test('Math.sqrt()', async () => {
expect(pythagoras(3, 4)).toBe(5)
Expand All @@ -9,3 +10,7 @@ test('Math.sqrt()', async () => {
test('implicit else', () => {
expect(implicitElse(true)).toBe(2)
})

test('import meta env', () => {
expect(useImportEnv()).toBe(true)
})