Skip to content

Commit 49837ac

Browse files
authored
test(replay): Update jest custom matchers for replay to use differ (#8047)
Use `printDiffOrStringify()` util function from jest to generate a pretty diff of the received vs expected. Also when searching for *any* calls for `toHaveSentReplay`, stop at the first call where *any* of the keys have a successful match, instead of always falling through to the last call.
1 parent 194130e commit 49837ac

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

packages/replay/jest.setup.ts

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,12 @@ const toHaveSameSession = function (received: jest.Mocked<ReplayContainer>, expe
5050
return {
5151
pass,
5252
message: () =>
53-
`${this.utils.matcherHint('toHaveSameSession', undefined, undefined, options)}\n\n` +
54-
`Expected: ${pass ? 'not ' : ''}${this.utils.printExpected(expected)}\n` +
55-
`Received: ${this.utils.printReceived(received.session)}`,
53+
`${this.utils.matcherHint(
54+
'toHaveSameSession',
55+
undefined,
56+
undefined,
57+
options,
58+
)}\n\n${this.utils.printDiffOrStringify(expected, received.session, 'Expected', 'Received')}`,
5659
};
5760
};
5861

@@ -138,11 +141,18 @@ const toHaveSentReplay = function (
138141

139142
let result: CheckCallForSentReplayResult;
140143

144+
const expectedKeysLength = expected ? ('sample' in expected ? Object.keys(expected.sample) : Object.keys(expected)).length : 0;
145+
141146
for (const currentCall of calls) {
142147
result = checkCallForSentReplay.call(this, currentCall[0], expected);
143148
if (result.pass) {
144149
break;
145150
}
151+
152+
// stop on the first call where any of the expected obj passes
153+
if (result.results.length < expectedKeysLength) {
154+
break;
155+
}
146156
}
147157

148158
// @ts-ignore use before assigned
@@ -161,10 +171,13 @@ const toHaveSentReplay = function (
161171
? 'Expected Replay to not have been sent, but a request was attempted'
162172
: 'Expected Replay to have been sent, but a request was not attempted'
163173
: `${this.utils.matcherHint('toHaveSentReplay', undefined, undefined, options)}\n\n${results
164-
.map(
165-
({ key, expectedVal, actualVal }: Result) =>
166-
`Expected (key: ${key}): ${pass ? 'not ' : ''}${this.utils.printExpected(expectedVal)}\n` +
167-
`Received (key: ${key}): ${this.utils.printReceived(actualVal)}`,
174+
.map(({ key, expectedVal, actualVal }: Result) =>
175+
this.utils.printDiffOrStringify(
176+
expectedVal,
177+
actualVal,
178+
`Expected (key: ${key})`,
179+
`Received (key: ${key})`,
180+
),
168181
)
169182
.join('\n')}`,
170183
};
@@ -197,10 +210,13 @@ const toHaveLastSentReplay = function (
197210
? 'Expected Replay to not have been sent, but a request was attempted'
198211
: 'Expected Replay to have last been sent, but a request was not attempted'
199212
: `${this.utils.matcherHint('toHaveSentReplay', undefined, undefined, options)}\n\n${results
200-
.map(
201-
({ key, expectedVal, actualVal }: Result) =>
202-
`Expected (key: ${key}): ${pass ? 'not ' : ''}${this.utils.printExpected(expectedVal)}\n` +
203-
`Received (key: ${key}): ${this.utils.printReceived(actualVal)}`,
213+
.map(({ key, expectedVal, actualVal }: Result) =>
214+
this.utils.printDiffOrStringify(
215+
expectedVal,
216+
actualVal,
217+
`Expected (key: ${key})`,
218+
`Received (key: ${key})`,
219+
),
204220
)
205221
.join('\n')}`,
206222
};

0 commit comments

Comments
 (0)