88 */
99import { describe , it } from "@executablemd/test-support/bdd" ;
1010import { expect } from "@executablemd/test-support/expect" ;
11+ import type { Operation } from "effection" ;
1112import { validateProps } from "@executablemd/core" ;
1213import type { PropsSchema } from "@executablemd/core" ;
1314import { z } from "zod" ;
@@ -57,6 +58,16 @@ const SCALARS: PropsSchema = {
5758 additionalProperties : false ,
5859} ;
5960
61+ /** The failure an operation raised, so an assertion can read it. */
62+ function * raised ( operation : ( ) => Operation < unknown > ) : Operation < unknown > {
63+ try {
64+ yield * operation ( ) ;
65+ } catch ( error ) {
66+ return error ;
67+ }
68+ throw new Error ( "expected the operation to fail" ) ;
69+ }
70+
6071describe ( "Tier PS — JSON Schema to Standard Schema" , ( ) => {
6172 it ( "PS1: converts every scalar form the contract names" , function * ( ) {
6273 const shape = shapeOf ( SCALARS ) ;
@@ -108,8 +119,10 @@ describe("Tier PS — JSON Schema to Standard Schema", () => {
108119 required : [ "a" ] ,
109120 additionalProperties : false ,
110121 } ;
111- expect ( ( ) => validateProps ( "x" , { } , schema ) ) . toThrow ( / r e q u i r e d p r o p e r t y ' a ' / ) ;
112- expect ( validateProps ( "x" , { a : "1" } , schema ) ) . toEqual ( { a : "1" } ) ;
122+ expect ( String ( yield * raised ( ( ) => validateProps ( "x" , { } , schema ) ) ) ) . toMatch (
123+ / r e q u i r e d p r o p e r t y ' a ' / ,
124+ ) ;
125+ expect ( yield * validateProps ( "x" , { a : "1" } , schema ) ) . toEqual ( { a : "1" } ) ;
113126 } ) ;
114127
115128 it ( "PS4: local references resolve — draft-7 uses definitions" , function * ( ) {
@@ -122,7 +135,7 @@ describe("Tier PS — JSON Schema to Standard Schema", () => {
122135 } ;
123136 const shape = shapeOf ( schema ) ;
124137 expect ( shape . user . safeParse ( { name : "Ada" } ) . success ) . toBe ( true ) ;
125- expect ( validateProps ( "x" , { user : { name : "Ada" } } , schema ) ) . toEqual ( {
138+ expect ( yield * validateProps ( "x" , { user : { name : "Ada" } } , schema ) ) . toEqual ( {
126139 user : { name : "Ada" } ,
127140 } ) ;
128141 } ) ;
@@ -135,7 +148,7 @@ describe("Tier PS — JSON Schema to Standard Schema", () => {
135148 } ;
136149 expect ( ( ) => shapeOf ( schema , "draft-7" ) ) . toThrow ( / R e f e r e n c e n o t f o u n d / ) ;
137150 expect ( shapeOf ( schema , "draft-2020-12" ) . user . safeParse ( { name : "Ada" } ) . success ) . toBe ( true ) ;
138- expect ( validateProps ( "x" , { user : { name : "Ada" } } , schema ) ) . toEqual ( {
151+ expect ( yield * validateProps ( "x" , { user : { name : "Ada" } } , schema ) ) . toEqual ( {
139152 user : { name : "Ada" } ,
140153 } ) ;
141154 } ) ;
@@ -159,7 +172,7 @@ describe("Tier PS — JSON Schema to Standard Schema", () => {
159172 data : { name : "Ada" , role : "member" } ,
160173 } ) ;
161174 // ...whereas Ajv is the layer entitled to do so.
162- expect ( validateProps ( "x" , { user : { name : "Ada" } } , schema ) ) . toEqual ( {
175+ expect ( yield * validateProps ( "x" , { user : { name : "Ada" } } , schema ) ) . toEqual ( {
163176 user : { name : "Ada" , role : "member" } ,
164177 } ) ;
165178 } ) ;
@@ -171,8 +184,8 @@ describe("Tier PS — JSON Schema to Standard Schema", () => {
171184 success : true ,
172185 data : { a : "x" , extra : 1 } ,
173186 } ) ;
174- expect ( ( ) => validateProps ( "x" , { closed : { a : "y" , extra : 1 } } , SCALARS ) ) . toThrow (
175- / m u s t N O T h a v e a d d i t i o n a l p r o p e r t i e s / ,
176- ) ;
187+ expect (
188+ String ( yield * raised ( ( ) => validateProps ( "x" , { closed : { a : "y" , extra : 1 } } , SCALARS ) ) ) ,
189+ ) . toMatch ( / m u s t N O T h a v e a d d i t i o n a l p r o p e r t i e s / ) ;
177190 } ) ;
178191} ) ;
0 commit comments