Skip to content

Commit 2898a52

Browse files
authored
fix(coverage): istanbul to support import attributes (#6006)
1 parent d066ba9 commit 2898a52

File tree

8 files changed

+64
-13
lines changed

8 files changed

+64
-13
lines changed

Diff for: packages/coverage-istanbul/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@
4444
"vitest": "workspace:*"
4545
},
4646
"dependencies": {
47+
"@istanbuljs/schema": "^0.1.3",
4748
"debug": "^4.3.5",
4849
"istanbul-lib-coverage": "^3.2.2",
49-
"istanbul-lib-instrument": "^6.0.2",
50+
"istanbul-lib-instrument": "^6.0.3",
5051
"istanbul-lib-report": "^3.0.1",
5152
"istanbul-lib-source-maps": "^5.0.4",
5253
"istanbul-reports": "^3.1.7",

Diff for: packages/coverage-istanbul/src/provider.ts

+9
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import type { CoverageMap } from 'istanbul-lib-coverage'
2828
import libCoverage from 'istanbul-lib-coverage'
2929
import libSourceMaps from 'istanbul-lib-source-maps'
3030
import { type Instrumenter, createInstrumenter } from 'istanbul-lib-instrument'
31+
// @ts-expect-error @istanbuljs/schema has no type definitions
32+
import { defaults as istanbulDefaults } from '@istanbuljs/schema'
3133

3234
// @ts-expect-error missing types
3335
import _TestExclude from 'test-exclude'
@@ -116,6 +118,13 @@ export class IstanbulCoverageProvider
116118
coverageGlobalScope: 'globalThis',
117119
coverageGlobalScopeFunc: false,
118120
ignoreClassMethods: this.options.ignoreClassMethods,
121+
parserPlugins: [
122+
...istanbulDefaults.instrumenter.parserPlugins,
123+
['importAttributes', { deprecatedAssertSyntax: true }],
124+
],
125+
generatorOpts: {
126+
importAttributesKeyword: 'with',
127+
},
119128
})
120129

121130
this.testExclude = new _TestExclude({

Diff for: pnpm-lock.yaml

+18-12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: test/coverage-test/fixtures/src/json-data-import.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import data from "./json-data.json" with { "type": "json" };
2+
3+
export function getJSON() {
4+
return data
5+
};

Diff for: test/coverage-test/fixtures/src/json-data.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"foo": false,
3+
"bar": "baz"
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { expect, test } from "vitest";
2+
import {getJSON} from "../src/json-data-import";
3+
import json from "../src/json-data.json";
4+
5+
test("JSON data", () => {
6+
expect(getJSON()).toEqual(json);
7+
});

Diff for: test/coverage-test/test/import-attributes.test.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { expect } from 'vitest'
2+
import { readCoverageMap, runVitest, test } from '../utils'
3+
4+
test('import attributes work', async () => {
5+
await runVitest({
6+
include: ['fixtures/test/import-attributes-fixture.test.ts'],
7+
coverage: { reporter: 'json', all: false },
8+
})
9+
10+
const coverageMap = await readCoverageMap()
11+
const files = coverageMap.files()
12+
13+
expect(files).toMatchInlineSnapshot(`
14+
[
15+
"<process-cwd>/fixtures/src/json-data-import.ts",
16+
]
17+
`)
18+
})

Diff for: test/coverage-test/vitest.workspace.custom.ts

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export default defineWorkspace([
6767

6868
// Other non-provider-specific tests that should be run on browser mode as well
6969
'**/ignore-hints.test.ts',
70+
'**/import-attributes.test.ts',
7071
'**/multi-suite.test.ts',
7172
'**/setup-files.test.ts',
7273
'**/results-snapshot.test.ts',

0 commit comments

Comments
 (0)