Skip to content

Commit a21c061

Browse files
committed
Update TypeDocs with some documentation. Also change export names.
1 parent 322e1f6 commit a21c061

11 files changed

+102
-28
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,14 @@ By default, it will track coverage for any file with a `*.schema.json`,
6262
`vitest-schema.config.js`
6363
```TypeScript
6464
import { defineConfig } from "vitest/config";
65-
import type { JsonSchemaCoverageProviderOptions } from "@hyperjump/json-schema-coverage/vitest-coverage-provider";
65+
import type { JsonSchemaCoverageProviderOptions } from "@hyperjump/json-schema-coverage/vitest/coverage-provider";
6666

6767
export default defineConfig({
6868
test: {
6969
include: ["schema-tests/"],
7070
coverage: {
7171
provider: "custom",
72-
customProviderModule: "@hyperjump/json-schema-coverage/vitest-coverage-provider",
72+
customProviderModule: "@hyperjump/json-schema-coverage/vitest/coverage-provider",
7373
include: ["schemas/**/*.json"] // Optional
7474
} as JsonSchemaCoverageProviderOptions
7575
}
@@ -107,7 +107,7 @@ for the validation to work.
107107

108108
```JavaScript
109109
import { describe, expect, test } from "vitest";
110-
import { registerSchema, unregisterSchema } from "@hyperjump/json-schema-coverage/vitest-matchers";
110+
import { registerSchema, unregisterSchema } from "@hyperjump/json-schema-coverage/vitest";
111111

112112
describe("Worksheet", () => {
113113
beforeEach(async () => {
@@ -155,7 +155,7 @@ describe("Worksheet", () => {
155155
These are the functions available when working with the vitest integration.
156156

157157
```JavaScript
158-
import { ... } from "@hyperjump/json-schema-coverage/vitest-matchers"
158+
import { ... } from "@hyperjump/json-schema-coverage/vitest"
159159
```
160160

161161
- **matchJsonSchema**: (uriOrSchema: string | SchemaObject | boolean) => Promise\<void>

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@
1717
"type": "module",
1818
"exports": {
1919
".": "./src/index.js",
20-
"./vitest-matchers": "./src/vitest/matchers.js",
21-
"./vitest-coverage-provider": "./src/vitest/coverage-provider.js"
20+
"./vitest": "./src/vitest/index.js",
21+
"./vitest/coverage-provider": "./src/vitest/coverage-provider.js"
2222
},
2323
"scripts": {
2424
"lint": "eslint src",
2525
"test": "vitest run --config src/vitest/vitest-json-schema.config.js",
2626
"test:coverage": "vitest run --coverage",
2727
"type-check": "tsc --noEmit",
28-
"docs": "typedoc --excludeExternals"
28+
"docs": "typedoc"
2929
},
3030
"devDependencies": {
3131
"@stylistic/eslint-plugin": "*",

src/coverage-map-service.d.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,34 @@
11
import { CoverageMapData } from "istanbul-lib-coverage";
22

3+
/**
4+
* The `CoverageMapService` creates [istanbul](https://istanbul.js.org/)
5+
* coverage maps for your schemas and stores them for use by the
6+
* `TestCoverageEvaluationPlugin`. A coverage map stores the file positions of
7+
* all the keywords, schemas, and branches in a schema.
8+
*/
39
export class CoverageMapService {
10+
/**
11+
* This method takes a file path to a schema, generates a coverage map for it,
12+
* and stores it for later use. It returns the identifier for the schema
13+
* (usually the value of `$id`).
14+
*/
415
addFromFile(schemaPath: string): Promise<string>;
16+
17+
/**
18+
* If you have a coverage map you created yourself or got from some other
19+
* source, you can add it using this method. You probably don't need this. Use
20+
* `addFromFile` to create and store the coverage map for you.
21+
*/
522
addCoverageMap(coverageMap: CoverageMapData): void;
23+
24+
/**
25+
* Get the file path for the schema that is identified by the given URI.
26+
*/
627
getSchemaPath(schemaUri: string): string;
28+
29+
/**
30+
* Retrieve a coverage map that was previously added through `addFromFile` or
31+
* `addCoverageMap`.
32+
*/
733
getCoverageMap(schemaUri: string): CoverageMapData;
834
}

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
/** @module low-level-api */
2+
13
export * from "./test-coverage-evaluation-plugin.js";
24
export * from "./coverage-map-service.js";

src/test-coverage-evaluation-plugin.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ import type { JsonNode } from "@hyperjump/json-schema/instance/experimental";
88
import type { CoverageMapData } from "istanbul-lib-coverage";
99
import type { CoverageMapService } from "./coverage-map-service.d.ts";
1010

11+
/**
12+
* The `TestCoverageEvaluationPlugin` hooks into the evaluation process of the
13+
* [@hyperjump/json-schema](https://github.com/hyperjump-io/json-schema)
14+
* validator and uses the `CoverageMapService` to record when a keyword or
15+
* schema is visited. Once the evaluation process is completed, it contains an
16+
* [istanbul](https://istanbul.js.org/) coverage file. These files can then be
17+
* used to generate any report that supports [istanbul](https://istanbul.js.org/).
18+
*/
1119
export class TestCoverageEvaluationPlugin implements EvaluationPlugin {
1220
constructor(coverageService?: CoverageMapService);
1321
coverage: CoverageMapData;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`Worksheet > match failure returns BASIC output 1`] = `
4+
"{
5+
"valid": false,
6+
"errors": [
7+
{
8+
"keyword": "https://json-schema.org/keyword/type",
9+
"absoluteKeywordLocation": "https://example.com/main#/type",
10+
"instanceLocation": "#"
11+
}
12+
]
13+
}"
14+
`;

src/vitest/index.d.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import type { SchemaObject } from "@hyperjump/json-schema";
2+
import type { AsyncExpectationResult } from "@vitest/expect";
3+
import "vitest";
4+
5+
declare module "vitest" {
6+
interface Matchers<R = unknown> {
7+
matchJsonSchema: (uriOrSchema: string | SchemaObject | boolean) => Promise<R>;
8+
toMatchJsonSchema: (uriOrSchema: string | SchemaObject | boolean) => Promise<R>;
9+
}
10+
}
11+
12+
/**
13+
* Register a schema in your code base by it's path.
14+
*
15+
* _**NOTE**: This is **not** the same as the function from
16+
* [@hyperjump/json-schema](https://github.com/hyperjump-io/json-schema) that takes a schema._
17+
*/
18+
export const registerSchema: (filePath: string) => Promise<void>;
19+
20+
/**
21+
* Remove a registered schema in your code base by it's path.
22+
*
23+
* _**NOTE**: This is **not** the same as the function from
24+
* [@hyperjump/json-schema](https://github.com/hyperjump-io/json-schema) that takes the schema's `$id`._
25+
*/
26+
export const unregisterSchema: (filePath: string) => Promise<void>;
27+
28+
/**
29+
* A vitest matcher that can be used to validate a JSON-compatible value. It
30+
* can take a relative or full URI for a schema in your codebase. Use relative
31+
* URIs to reference a file and full URIs to reference the `$id` of a schema
32+
* you registered using the `registerSchema` function.
33+
*
34+
* You can use this matcher with an inline schema as well, but you will only
35+
* get coverage for schemas that are in files.
36+
*
37+
*/
38+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
39+
export const matchJsonSchema: (instance: any, uriOrSchema: string | SchemaObject | boolean) => AsyncExpectationResult;
40+
export const toMatchJsonSchema: typeof matchJsonSchema;
41+
42+
export { loadDialect, defineVocabulary, addKeyword } from "@hyperjump/json-schema/experimental";

src/vitest/matchers.js renamed to src/vitest/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/** @module vitest */
12
import { expect } from "vitest";
23
import { matchJsonSchema, toMatchJsonSchema } from "./json-schema-matchers.js";
34

src/vitest/matchers.test.js renamed to src/vitest/index.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { afterAll, afterEach, beforeAll, describe, expect, test } from "vitest";
22
import { mkdir, rm, writeFile } from "node:fs/promises";
33
import { resolve } from "node:path";
4-
import { registerSchema, unregisterSchema } from "./matchers.js";
4+
import { registerSchema, unregisterSchema } from "./index.js";
55

66
describe("Worksheet", () => {
77
test("matches with schema", async () => {

src/vitest/json-schema-matchers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { toAbsoluteIri } from "@hyperjump/uri";
1212

1313
/**
1414
* @import { OutputUnit, SchemaObject } from "@hyperjump/json-schema"
15-
* @import * as API from "./matchers.d.ts"
15+
* @import * as API from "./index.d.ts"
1616
*/
1717

1818
const coverageFilesDirectory = ".json-schema-coverage";

0 commit comments

Comments
 (0)