Skip to content

Commit

Permalink
fix(coverage): istanbul to support import attributes (#6006)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gravitonic committed Jul 1, 2024
1 parent d066ba9 commit 2898a52
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 13 deletions.
3 changes: 2 additions & 1 deletion packages/coverage-istanbul/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@
"vitest": "workspace:*"
},
"dependencies": {
"@istanbuljs/schema": "^0.1.3",
"debug": "^4.3.5",
"istanbul-lib-coverage": "^3.2.2",
"istanbul-lib-instrument": "^6.0.2",
"istanbul-lib-instrument": "^6.0.3",
"istanbul-lib-report": "^3.0.1",
"istanbul-lib-source-maps": "^5.0.4",
"istanbul-reports": "^3.1.7",
Expand Down
9 changes: 9 additions & 0 deletions packages/coverage-istanbul/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import type { CoverageMap } from 'istanbul-lib-coverage'
import libCoverage from 'istanbul-lib-coverage'
import libSourceMaps from 'istanbul-lib-source-maps'
import { type Instrumenter, createInstrumenter } from 'istanbul-lib-instrument'
// @ts-expect-error @istanbuljs/schema has no type definitions
import { defaults as istanbulDefaults } from '@istanbuljs/schema'

// @ts-expect-error missing types
import _TestExclude from 'test-exclude'
Expand Down Expand Up @@ -116,6 +118,13 @@ export class IstanbulCoverageProvider
coverageGlobalScope: 'globalThis',
coverageGlobalScopeFunc: false,
ignoreClassMethods: this.options.ignoreClassMethods,
parserPlugins: [
...istanbulDefaults.instrumenter.parserPlugins,
['importAttributes', { deprecatedAssertSyntax: true }],
],
generatorOpts: {
importAttributesKeyword: 'with',
},
})

this.testExclude = new _TestExclude({
Expand Down
30 changes: 18 additions & 12 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions test/coverage-test/fixtures/src/json-data-import.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import data from "./json-data.json" with { "type": "json" };

export function getJSON() {
return data
};
4 changes: 4 additions & 0 deletions test/coverage-test/fixtures/src/json-data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"foo": false,
"bar": "baz"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { expect, test } from "vitest";
import {getJSON} from "../src/json-data-import";
import json from "../src/json-data.json";

test("JSON data", () => {
expect(getJSON()).toEqual(json);
});
18 changes: 18 additions & 0 deletions test/coverage-test/test/import-attributes.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { expect } from 'vitest'
import { readCoverageMap, runVitest, test } from '../utils'

test('import attributes work', async () => {
await runVitest({
include: ['fixtures/test/import-attributes-fixture.test.ts'],
coverage: { reporter: 'json', all: false },
})

const coverageMap = await readCoverageMap()
const files = coverageMap.files()

expect(files).toMatchInlineSnapshot(`
[
"<process-cwd>/fixtures/src/json-data-import.ts",
]
`)
})
1 change: 1 addition & 0 deletions test/coverage-test/vitest.workspace.custom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export default defineWorkspace([

// Other non-provider-specific tests that should be run on browser mode as well
'**/ignore-hints.test.ts',
'**/import-attributes.test.ts',
'**/multi-suite.test.ts',
'**/setup-files.test.ts',
'**/results-snapshot.test.ts',
Expand Down

0 comments on commit 2898a52

Please sign in to comment.