@@ -6,22 +6,23 @@ import {
66 ContentDescriptorObject ,
77 Servers ,
88 MethodObjectParams ,
9- MethodObject
9+ MethodObject ,
1010} from "@open-rpc/meta-schema" ;
1111const jsf = require ( "json-schema-faker" ) ; // tslint:disable-line
1212import Ajv from "ajv" ;
1313import { isEqual } from "lodash" ;
14+ import Reporter from "./reporters/emptyReporter" ;
1415
1516const getFakeParams = ( params : any [ ] ) : any [ ] => {
1617 return params . map ( ( p ) => jsf . generate ( p . schema ) ) ;
1718} ;
1819
19- interface IOptions {
20+ export interface IOptions {
2021 openrpcDocument : OpenrpcDocument ;
2122 skip : string [ ] ;
2223 only : string [ ] ;
2324 transport ( url : string , method : string , params : any [ ] ) : PromiseLike < any > ;
24- reporter ( value : any [ ] , schema : OpenrpcDocument ) : any ;
25+ reporter : Reporter ;
2526}
2627
2728export interface ExampleCall {
@@ -34,49 +35,66 @@ export interface ExampleCall {
3435 resultSchema : JSONSchema ;
3536 expectedResult ?: any ;
3637 requestError ?: any ;
38+ title : string ;
3739}
3840
39- const paramsToObj = ( params : any [ ] , methodParams : ContentDescriptorObject [ ] ) : any => {
41+ const paramsToObj = (
42+ params : any [ ] ,
43+ methodParams : ContentDescriptorObject [ ]
44+ ) : any => {
4045 return params . reduce ( ( acc , val , i ) => {
4146 acc [ methodParams [ i ] . name ] = val ;
4247 return acc ;
4348 } , { } ) ;
44- }
49+ } ;
4550
4651export default async ( options : IOptions ) => {
47- const filteredMethods = ( options . openrpcDocument . methods as MethodObject [ ] )
52+ const filteredMethods = options . openrpcDocument . methods
4853 . filter ( ( { name } ) => ! options . skip . includes ( name ) )
49- . filter ( ( { name } ) => options . only . length === 0 || options . only . includes ( name ) ) ;
54+ . filter (
55+ ( { name } ) => options . only . length === 0 || options . only . includes ( name )
56+ ) ;
5057
5158 if ( filteredMethods . length === 0 ) {
5259 throw new Error ( "No methods to test" ) ;
5360 }
5461
62+
5563 const exampleCalls : ExampleCall [ ] = [ ] ;
5664
57- const servers : Servers = ( options . openrpcDocument . servers || [ { url : "http://localhost:3333" } ] ) ;
65+ const servers : Servers = options . openrpcDocument . servers || [
66+ { url : "http://localhost:3333" } ,
67+ ] ;
5868
5969 servers . forEach ( ( { url } ) => {
6070 filteredMethods . forEach ( ( method ) => {
6171 if ( method . examples === undefined || method . examples . length === 0 ) {
6272 for ( let i = 0 ; i < 10 ; i ++ ) {
6373 const p = getFakeParams ( method . params ) ;
6474 // handle object or array case
65- const params = method . paramStructure === "by-name" ? paramsToObj ( p , method . params as ContentDescriptorObject [ ] ) : p ;
75+ const params =
76+ method . paramStructure === "by-name"
77+ ? paramsToObj ( p , method . params as ContentDescriptorObject [ ] )
78+ : p ;
6679 exampleCalls . push ( {
80+ title : method . name + " > json-schema-faker params and expect result schema to match [" + i + "]" ,
6781 methodName : method . name ,
6882 params,
6983 url,
70- resultSchema : ( method . result as ContentDescriptorObject ) . schema
84+ resultSchema : ( method . result as ContentDescriptorObject ) . schema ,
7185 } ) ;
7286 }
7387 return ;
7488 }
7589
7690 ( method . examples as ExamplePairingObject [ ] ) . forEach ( ( ex ) => {
7791 const p = ( ex . params as ExampleObject [ ] ) . map ( ( e ) => e . value ) ;
78- const params = method . paramStructure === "by-name" ? paramsToObj ( p , method . params as ContentDescriptorObject [ ] ) : p ;
92+ const params =
93+ method . paramStructure === "by-name"
94+ ? paramsToObj ( p , method . params as ContentDescriptorObject [ ] )
95+ : p ;
7996 exampleCalls . push ( {
97+ title : method . name + " > example params and expect result to match: " + ex . name ,
8098 methodName : method . name ,
8199 params,
82100 url,
@@ -87,13 +105,22 @@ export default async (options: IOptions) => {
87105 } ) ;
88106 } ) ;
89107
108+ options . reporter . onBegin ( options , exampleCalls ) ;
90109 for ( const exampleCall of exampleCalls ) {
110+ options . reporter . onTestBegin ( options , exampleCall ) ;
91111 try {
92- const callResult = await options . transport ( exampleCall . url , exampleCall . methodName , exampleCall . params ) ;
112+ const callResult = await options . transport (
113+ exampleCall . url ,
114+ exampleCall . methodName ,
115+ exampleCall . params
116+ ) ;
93117 exampleCall . result = callResult . result ;
94118
95119 if ( exampleCall . expectedResult ) {
96- exampleCall . valid = isEqual ( exampleCall . expectedResult , exampleCall . result ) ;
120+ exampleCall . valid = isEqual (
121+ exampleCall . expectedResult ,
122+ exampleCall . result
123+ ) ;
97124 } else {
98125 const ajv = new Ajv ( ) ;
99126 ajv . validate ( exampleCall . resultSchema , exampleCall . result ) ;
@@ -108,7 +135,8 @@ export default async (options: IOptions) => {
108135 exampleCall . valid = false ;
109136 exampleCall . requestError = e ;
110137 }
138+ options . reporter . onTestEnd ( options , exampleCall ) ;
111139 }
112140
113- return options . reporter ( exampleCalls , options . openrpcDocument ) ;
141+ return options . reporter . onEnd ( options , exampleCalls ) ;
114142} ;
0 commit comments