|
1 |
| -import { randomUUID } from "node:crypto"; |
2 |
| -import { existsSync } from "node:fs"; |
3 |
| -import { writeFile } from "node:fs/promises"; |
4 | 1 | import { expect } from "vitest";
|
5 |
| -import { registerSchema, unregisterSchema, validate } from "@hyperjump/json-schema/draft-2020-12"; |
6 |
| -import "@hyperjump/json-schema/draft-2019-09"; |
7 |
| -import "@hyperjump/json-schema/draft-07"; |
8 |
| -import "@hyperjump/json-schema/draft-06"; |
9 |
| -import "@hyperjump/json-schema/draft-04"; |
10 |
| -import { BASIC } from "@hyperjump/json-schema/experimental"; |
11 |
| -import { TestCoverageEvaluationPlugin } from "../test-coverage-evaluation-plugin.js"; |
12 |
| - |
13 |
| -/** |
14 |
| - * @import { OutputUnit, SchemaObject } from "@hyperjump/json-schema" |
15 |
| - * @import { AsyncExpectationResult } from "@vitest/expect" |
16 |
| - */ |
17 |
| - |
18 |
| -/** @type (instance: any, uriOrSchema: string | SchemaObject | boolean) => AsyncExpectationResult */ |
19 |
| -const schemaMatcher = async (instance, uriOrSchema) => { |
20 |
| - /** @type OutputUnit */ |
21 |
| - let output; |
22 |
| - |
23 |
| - if (typeof uriOrSchema === "string") { |
24 |
| - const uri = uriOrSchema; |
25 |
| - |
26 |
| - if (existsSync(".json-schema-coverage")) { |
27 |
| - const testCoveragePlugin = new TestCoverageEvaluationPlugin(); |
28 |
| - |
29 |
| - // eslint-disable-next-line @typescript-eslint/no-unsafe-argument |
30 |
| - output = await validate(uri, instance, { |
31 |
| - outputFormat: BASIC, |
32 |
| - plugins: [testCoveragePlugin] |
33 |
| - }); |
34 |
| - |
35 |
| - const coverageMapPath = `.json-schema-coverage/${randomUUID()}.json`; |
36 |
| - const coverageMapJson = JSON.stringify(testCoveragePlugin.coverageMap); |
37 |
| - await writeFile(coverageMapPath, coverageMapJson); |
38 |
| - } else { |
39 |
| - // eslint-disable-next-line @typescript-eslint/no-unsafe-argument |
40 |
| - output = await validate(uri, instance, BASIC); |
41 |
| - } |
42 |
| - } else { |
43 |
| - const schema = uriOrSchema; |
44 |
| - const uri = `urn:uuid:${randomUUID()}`; |
45 |
| - registerSchema(schema, uri, "https://json-schema.org/draft/2020-12/schema"); |
46 |
| - try { |
47 |
| - // eslint-disable-next-line @typescript-eslint/no-unsafe-argument |
48 |
| - output = await validate(uri, instance, BASIC); |
49 |
| - } finally { |
50 |
| - unregisterSchema(uri); |
51 |
| - } |
52 |
| - } |
53 |
| - |
54 |
| - return { |
55 |
| - pass: output.valid, |
56 |
| - message: () => JSON.stringify(output, null, " ") |
57 |
| - }; |
58 |
| -}; |
| 2 | +import { matchJsonSchema } from "./json-schema-matcher.js"; |
59 | 3 |
|
60 | 4 | expect.extend({
|
61 |
| - matchJsonSchema: schemaMatcher, |
62 |
| - toMatchJsonSchema: schemaMatcher |
| 5 | + matchJsonSchema: matchJsonSchema, |
| 6 | + toMatchJsonSchema: matchJsonSchema |
63 | 7 | });
|
0 commit comments