From a2e9daf6a33153060435e50ba50fa862cf9e7b55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ari=20Perkki=C3=B6?= Date: Mon, 21 Nov 2022 16:05:59 +0200 Subject: [PATCH] fix(coverage): env-replacer to add filenames into sourcemaps (#2338) --- packages/vitest/src/node/plugins/envReplacer.ts | 4 ++-- test/coverage-test/coverage-test/coverage.istanbul.test.ts | 7 +++++++ test/coverage-test/src/importEnv.ts | 3 +++ test/coverage-test/test/coverage.test.ts | 5 +++++ 4 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 test/coverage-test/src/importEnv.ts diff --git a/packages/vitest/src/node/plugins/envReplacer.ts b/packages/vitest/src/node/plugins/envReplacer.ts index ddf4b223bf60..29a4b1b22a3e 100644 --- a/packages/vitest/src/node/plugins/envReplacer.ts +++ b/packages/vitest/src/node/plugins/envReplacer.ts @@ -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 @@ -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 }), } } }, diff --git a/test/coverage-test/coverage-test/coverage.istanbul.test.ts b/test/coverage-test/coverage-test/coverage.istanbul.test.ts index 688b0ec37f05..eecd9b7239a6 100644 --- a/test/coverage-test/coverage-test/coverage.istanbul.test.ts +++ b/test/coverage-test/coverage-test/coverage.istanbul.test.ts @@ -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') +}) diff --git a/test/coverage-test/src/importEnv.ts b/test/coverage-test/src/importEnv.ts new file mode 100644 index 000000000000..8c66fa37ba60 --- /dev/null +++ b/test/coverage-test/src/importEnv.ts @@ -0,0 +1,3 @@ +export function useImportEnv() { + return import.meta.env.SOME_VARIABLE == null +} diff --git a/test/coverage-test/test/coverage.test.ts b/test/coverage-test/test/coverage.test.ts index ecf60dd6ca84..945b0b99167a 100644 --- a/test/coverage-test/test/coverage.test.ts +++ b/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) @@ -9,3 +10,7 @@ test('Math.sqrt()', async () => { test('implicit else', () => { expect(implicitElse(true)).toBe(2) }) + +test('import meta env', () => { + expect(useImportEnv()).toBe(true) +})