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