6
6
RequestMismatch ,
7
7
MatchingResultRequestNotFound ,
8
8
MatchingResultMissingRequest ,
9
+ MatchingResultPlugin ,
10
+ PluginContentMismatch ,
9
11
} from '@pact-foundation/pact-core/src/consumer/index' ;
10
12
11
13
export function displayQuery ( query : Record < string , string [ ] > ) : string {
@@ -60,12 +62,29 @@ export function filterMissingFeatureFlag(
60
62
mismatches : MatchingResult [ ]
61
63
) : MatchingResult [ ] {
62
64
if ( process . env . PACT_EXPERIMENTAL_FEATURE_ALLOW_MISSING_REQUESTS ) {
63
- return mismatches . filter ( ( m ) => m . type !== 'request-mismatch' ) ;
65
+ return mismatches . filter (
66
+ ( m ) => ! isMismatchingResultPlugin ( m ) && m . type !== 'request-mismatch'
67
+ ) ;
64
68
}
65
69
return mismatches ;
66
70
}
67
71
68
72
export function printMismatch ( m : Mismatch ) : string {
73
+ if ( isPluginContentMismatch ( m ) ) {
74
+ const s = [
75
+ `\t${ m . path } : ${ m . mismatch } \n` ,
76
+ m . mismatch
77
+ ? ''
78
+ : `\t\tExpected '${ m . expected } ', got: '${ m . actual } ${ m . diff } '` ,
79
+ ] ;
80
+ if ( m . diff ) {
81
+ s . push ( `\t\tDiff:` ) ;
82
+ s . push ( `\t\t\t${ m . diff } ` ) ;
83
+ }
84
+
85
+ return s . join ( '\n\n' ) ;
86
+ }
87
+
69
88
switch ( m . type ) {
70
89
case 'MethodMismatch' :
71
90
return `Expected ${ m . expected } , got: ${ m . actual } ` ;
@@ -74,22 +93,30 @@ export function printMismatch(m: Mismatch): string {
74
93
}
75
94
}
76
95
96
+ export function printMismatches ( mismatches : Mismatch [ ] ) : string {
97
+ const errors = mismatches . map ( ( m ) => printMismatch ( m ) ) ;
98
+ return errors . join ( '\n' ) ;
99
+ }
100
+
77
101
export function generateMockServerError (
78
102
mismatches : MatchingResult [ ] ,
79
103
indent : string
80
104
) : string {
81
105
return [
82
106
'Mock server failed with the following mismatches:' ,
83
107
...mismatches . map ( ( mismatch , i ) => {
108
+ if ( isMismatchingResultPlugin ( mismatch ) ) {
109
+ return printMismatches ( mismatch . mismatches ) ;
110
+ }
84
111
if ( mismatch . type === 'request-mismatch' ) {
85
112
return `\n${ indent } ${ i } ) The following request was incorrect: \n
86
- ${ indent } ${ mismatch . method } ${ mismatch . path }
87
- ${ mismatch . mismatches
88
- ?. map (
89
- ( d , j ) =>
90
- `\n${ indent } ${ indent } ${ indent } 1.${ j } ${ printMismatch ( d ) } `
91
- )
92
- . join ( '' ) } `;
113
+ ${ indent } ${ mismatch . method } ${ mismatch . path }
114
+ ${ mismatch . mismatches
115
+ ?. map (
116
+ ( d , j ) =>
117
+ `\n${ indent } ${ indent } ${ indent } 1.${ j } ${ printMismatch ( d ) } `
118
+ )
119
+ . join ( '' ) } `;
93
120
}
94
121
if ( mismatch . type === 'request-not-found' ) {
95
122
return `\n${ indent } ${ i } ) The following request was not expected: ${ displayRequest (
@@ -107,3 +134,34 @@ export function generateMockServerError(
107
134
} ) ,
108
135
] . join ( '\n' ) ;
109
136
}
137
+
138
+ // TODO: update Matching in the rust core to have a `type` property
139
+ // to avoid having to do this check!
140
+
141
+ const isMismatchingResultPlugin = (
142
+ obj : MatchingResult
143
+ ) : obj is MatchingResultPlugin => {
144
+ if (
145
+ ( obj as MatchingResultPlugin ) . error !== undefined &&
146
+ ( obj as MatchingResultPlugin ) . mismatches
147
+ )
148
+ return true ;
149
+ return false ;
150
+ } ;
151
+
152
+ const isPluginContentMismatch = (
153
+ obj : Mismatch
154
+ ) : obj is PluginContentMismatch => {
155
+ const cast = obj as PluginContentMismatch ;
156
+
157
+ if (
158
+ cast . diff !== undefined ||
159
+ ( cast . expected !== undefined &&
160
+ cast . actual !== undefined &&
161
+ cast . mismatch !== undefined &&
162
+ cast . path !== undefined )
163
+ )
164
+ return true ;
165
+
166
+ return false ;
167
+ } ;
0 commit comments