Skip to content

Commit

Permalink
fix(coverage): env-replacer to add filenames into sourcemaps (#2338)
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio authored Nov 21, 2022
1 parent 2499828 commit a2e9daf
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/vitest/src/node/plugins/envReplacer.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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)
})

0 comments on commit a2e9daf

Please sign in to comment.