Skip to content

Commit 2185693

Browse files
committed
feat: add support for rendering plugin content mismatches
1 parent 2dc966e commit 2185693

File tree

2 files changed

+69
-11
lines changed

2 files changed

+69
-11
lines changed

examples/v4/plugins/test/matt.consumer.spec.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe('Plugins - Matt Protocol', () => {
3030
.uponReceiving('an HTTP request to /matt')
3131
.usingPlugin({
3232
plugin: 'matt',
33-
version: '0.0.2',
33+
version: '0.0.5',
3434
})
3535
.withRequest('POST', '/matt', (builder) => {
3636
builder.pluginContents('application/matt', mattRequest);
@@ -73,12 +73,12 @@ describe('Plugins - Matt Protocol', () => {
7373
.addSynchronousInteraction('a MATT message')
7474
.usingPlugin({
7575
plugin: 'matt',
76-
version: '0.0.2',
76+
version: '0.0.5',
7777
})
7878
.withPluginContents(mattMessage, 'application/matt')
7979
.startTransport('matt', HOST)
8080
.executeTest(async (tc) => {
81-
const message = await sendMattMessageTCP('hello', HOST, tc.port);
81+
const message = await sendMattMessageTCP('hellotcp', HOST, tc.port);
8282
expect(message).to.eq('tcpworld');
8383
});
8484
});

src/v3/display.ts

+66-8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import {
66
RequestMismatch,
77
MatchingResultRequestNotFound,
88
MatchingResultMissingRequest,
9+
MatchingResultPlugin,
10+
PluginContentMismatch,
911
} from '@pact-foundation/pact-core/src/consumer/index';
1012

1113
export function displayQuery(query: Record<string, string[]>): string {
@@ -60,12 +62,29 @@ export function filterMissingFeatureFlag(
6062
mismatches: MatchingResult[]
6163
): MatchingResult[] {
6264
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+
);
6468
}
6569
return mismatches;
6670
}
6771

6872
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+
6988
switch (m.type) {
7089
case 'MethodMismatch':
7190
return `Expected ${m.expected}, got: ${m.actual}`;
@@ -74,22 +93,30 @@ export function printMismatch(m: Mismatch): string {
7493
}
7594
}
7695

96+
export function printMismatches(mismatches: Mismatch[]): string {
97+
const errors = mismatches.map((m) => printMismatch(m));
98+
return errors.join('\n');
99+
}
100+
77101
export function generateMockServerError(
78102
mismatches: MatchingResult[],
79103
indent: string
80104
): string {
81105
return [
82106
'Mock server failed with the following mismatches:',
83107
...mismatches.map((mismatch, i) => {
108+
if (isMismatchingResultPlugin(mismatch)) {
109+
return printMismatches(mismatch.mismatches);
110+
}
84111
if (mismatch.type === 'request-mismatch') {
85112
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('')}`;
93120
}
94121
if (mismatch.type === 'request-not-found') {
95122
return `\n${indent}${i}) The following request was not expected: ${displayRequest(
@@ -107,3 +134,34 @@ export function generateMockServerError(
107134
}),
108135
].join('\n');
109136
}
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

Comments
 (0)