Skip to content

Commit 134a08e

Browse files
committed
Add the "include" option
1 parent b5350a5 commit 134a08e

File tree

3 files changed

+35
-10
lines changed

3 files changed

+35
-10
lines changed

README.md

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@ All files | 81.81 | 66.66 | 80 | 88.88 |
2121

2222
**Legend**
2323
- **Statements** = Keywords and subschemas
24-
- **Branches** = true/false branches for each keyword
24+
- **Branches** = true/false branches for each keyword (except for keywords that
25+
don't branch such as annotation-only keywords)
2526
- **Functions** = Subschemas
2627

2728
## Limitations
2829

29-
The following are known limitations I'm hopeful can be addressed.
30+
The following are a list of known limitations. Some might be able to be
31+
addressed at some point, while others might not.
3032

31-
- Coverage can only be reported for `*.schema.(json|yaml|yml)` files.
3233
- Keywords can pass/fail for multiple reasons, but not all branches are captured
3334
- Example: `type: ["object", "boolean"]`. If you test with an object and a
3435
number, you've covered pass/fail, but haven't tested that a boolean should
@@ -38,18 +39,31 @@ The following are known limitations I'm hopeful can be addressed.
3839

3940
Integration with vitest is provided. You'll need a vitest config specifically
4041
for running schema coverage. You can't run with coverage for both your js/ts
41-
code and schema at the same time.
42+
code and schema code at the same time.
43+
44+
By default, it will track coverage for any file with a `*.schema.json`,
45+
`*.schema.yaml`, or `*.schema.yml` extension. You can change this with the
46+
`include` option.
47+
48+
**Options**
49+
50+
- **include** -- An array of glob paths of schemas you want to track coverage
51+
for. For example, if you keep your schemas in a folder called `schemas` and
52+
they just have plain extensions (`*.json`) instead of schema extensions
53+
`*.schema.json`, you could use `["./schemas/**/*.json"]`.
4254

4355
`vitest-schema.config.js`
44-
```JavaScript
56+
```TypeScript
4557
import { defineConfig } from "vitest/config";
58+
import type { JsonSchemaCoverageProviderOptions } from "@hyperjump/json-schema-coverage/vitest-coverage-provider";
4659

4760
export default defineConfig({
4861
test: {
4962
coverage: {
5063
provider: "custom",
51-
customProviderModule: "@hyperjump/json-schema-coverage/vitest-coverage-provider"
52-
}
64+
customProviderModule: "@hyperjump/json-schema-coverage/vitest-coverage-provider",
65+
include: ["./schemas/**/*.json"] // Optional
66+
} as JsonSchemaCoverageProviderOptions
5367
}
5468
});
5569
```

src/vitest/coverage-provider.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
import type { CoverageProviderModule } from "vitest";
2+
import type { CustomProviderOptions } from "vitest/node";
23

34
export default CoverageProviderModule;
5+
6+
export type JsonSchemaCoverageProviderOptions = CustomProviderOptions & {
7+
provider: "custom";
8+
customProviderModule: "@hyperjump/json-schema-coverage/vitest-coverage-provider";
9+
include?: string[];
10+
};

src/vitest/coverage-provider.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ import { getNodeFromPointer } from "../json-util.js";
2323
* CoverageProviderModule,
2424
* ResolvedCoverageOptions,
2525
* Vitest
26-
* } from "vitest"
26+
* } from "vitest/node"
2727
* @import { CoverageMap, CoverageMapData } from "istanbul-lib-coverage"
28-
* @import { SchemaObject } from "@hyperjump/json-schema"
2928
* @import { JRef } from "@hyperjump/browser/jref"
29+
* @import { JsonSchemaCoverageProviderOptions } from "./coverage-provider.js"
3030
* @import { JsonNode } from "../jsonast.js"
3131
*/
3232

@@ -60,7 +60,7 @@ class JsonSchemaCoverageProvider {
6060
initialize(ctx) {
6161
this.ctx = ctx;
6262

63-
const config = ctx.config.coverage;
63+
const config = /** @type ResolvedCoverageOptions & { include: string[]; } */ (ctx.config.coverage);
6464

6565
/** @type ResolvedCoverageOptions<"custom"> */
6666
this.options = {
@@ -85,6 +85,10 @@ class JsonSchemaCoverageProvider {
8585
this.roots = ctx.config.project?.length
8686
? [...new Set(ctx.projects.map((project) => project.config.root))]
8787
: [ctx.config.root];
88+
89+
if ("include" in config) {
90+
this.include = config.include;
91+
}
8892
}
8993

9094
/** @type CoverageProvider["resolveOptions"] */

0 commit comments

Comments
 (0)