From fef8aa470d1fe4978a7fa7f3b15e1f87a48af7be Mon Sep 17 00:00:00 2001 From: AriPerkkio Date: Mon, 12 Dec 2022 20:30:06 +0200 Subject: [PATCH] fix(coverage): esbuild to preserve legal comments for ignore hints --- packages/vitest/src/node/plugins/index.ts | 3 + .../__snapshots__/c8.report.test.ts.snap | 105 ++++++++++++++++++ test/coverage-test/src/utils.ts | 5 + 3 files changed, 113 insertions(+) diff --git a/packages/vitest/src/node/plugins/index.ts b/packages/vitest/src/node/plugins/index.ts index 26c1e51c73f3..839f460935f5 100644 --- a/packages/vitest/src/node/plugins/index.ts +++ b/packages/vitest/src/node/plugins/index.ts @@ -92,6 +92,9 @@ export async function VitestPlugin(options: UserConfig = {}, ctx = new Vitest('t const config: ViteConfig = { esbuild: { sourcemap: 'external', + + // Enables using ignore hint for coverage providers with @preserve keyword + legalComments: 'inline', }, resolve: { // by default Vite resolves `module` field, which not always a native ESM module diff --git a/test/coverage-test/coverage-report-tests/__snapshots__/c8.report.test.ts.snap b/test/coverage-test/coverage-report-tests/__snapshots__/c8.report.test.ts.snap index f176dbf905c3..f4bc12646001 100644 --- a/test/coverage-test/coverage-report-tests/__snapshots__/c8.report.test.ts.snap +++ b/test/coverage-test/coverage-report-tests/__snapshots__/c8.report.test.ts.snap @@ -2272,6 +2272,8 @@ exports[`c8 json report 1`] = ` "f": { "0": 1, "1": 1, + "10": 0, + "11": 0, "2": 2, "3": 2, "4": 0, @@ -2330,6 +2332,54 @@ exports[`c8 json report 1`] = ` }, "name": "get", }, + "10": { + "decl": { + "end": { + "column": 1, + "line": 29, + }, + "start": { + "column": 7, + "line": 27, + }, + }, + "line": 27, + "loc": { + "end": { + "column": 1, + "line": 29, + }, + "start": { + "column": 7, + "line": 27, + }, + }, + "name": "unusedFunction", + }, + "11": { + "decl": { + "end": { + "column": 1, + "line": 29, + }, + "start": { + "column": 0, + "line": 29, + }, + }, + "line": 29, + "loc": { + "end": { + "column": 1, + "line": 29, + }, + "start": { + "column": 0, + "line": 29, + }, + }, + "name": "get", + }, "2": { "decl": { "end": { @@ -2542,6 +2592,11 @@ exports[`c8 json report 1`] = ` "21": 0, "22": 0, "23": 0, + "24": 1, + "25": 1, + "26": 1, + "27": 0, + "28": 0, "3": 1, "4": 1, "5": 2, @@ -2721,6 +2776,56 @@ exports[`c8 json report 1`] = ` "line": 24, }, }, + "24": { + "end": { + "column": 0, + "line": 25, + }, + "start": { + "column": 0, + "line": 25, + }, + }, + "25": { + "end": { + "column": 39, + "line": 26, + }, + "start": { + "column": 0, + "line": 26, + }, + }, + "26": { + "end": { + "column": 34, + "line": 27, + }, + "start": { + "column": 0, + "line": 27, + }, + }, + "27": { + "end": { + "column": 73, + "line": 28, + }, + "start": { + "column": 0, + "line": 28, + }, + }, + "28": { + "end": { + "column": 1, + "line": 29, + }, + "start": { + "column": 0, + "line": 29, + }, + }, "3": { "end": { "column": 0, diff --git a/test/coverage-test/src/utils.ts b/test/coverage-test/src/utils.ts index 3b2ffc2048a4..b61751a451d9 100644 --- a/test/coverage-test/src/utils.ts +++ b/test/coverage-test/src/utils.ts @@ -22,3 +22,8 @@ export function run() { // this should not be covered divide(1, 1) } + +/* istanbul ignore next -- @preserve */ +export function unusedFunction() { + return 'This function is not used, but excluded from istanbul coverage' +}