Skip to content

test(replay): Update jest custom matchers for replay to use differ #8047

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 27 additions & 11 deletions packages/replay/jest.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,12 @@ const toHaveSameSession = function (received: jest.Mocked<ReplayContainer>, expe
return {
pass,
message: () =>
`${this.utils.matcherHint('toHaveSameSession', undefined, undefined, options)}\n\n` +
`Expected: ${pass ? 'not ' : ''}${this.utils.printExpected(expected)}\n` +
`Received: ${this.utils.printReceived(received.session)}`,
`${this.utils.matcherHint(
'toHaveSameSession',
undefined,
undefined,
options,
)}\n\n${this.utils.printDiffOrStringify(expected, received.session, 'Expected', 'Received')}`,
};
};

Expand Down Expand Up @@ -138,11 +141,18 @@ const toHaveSentReplay = function (

let result: CheckCallForSentReplayResult;

const expectedKeysLength = expected ? ('sample' in expected ? Object.keys(expected.sample) : Object.keys(expected)).length : 0;

for (const currentCall of calls) {
result = checkCallForSentReplay.call(this, currentCall[0], expected);
if (result.pass) {
break;
}

// stop on the first call where any of the expected obj passes
if (result.results.length < expectedKeysLength) {
break;
}
}

// @ts-ignore use before assigned
Expand All @@ -161,10 +171,13 @@ const toHaveSentReplay = function (
? 'Expected Replay to not have been sent, but a request was attempted'
: 'Expected Replay to have been sent, but a request was not attempted'
: `${this.utils.matcherHint('toHaveSentReplay', undefined, undefined, options)}\n\n${results
.map(
({ key, expectedVal, actualVal }: Result) =>
`Expected (key: ${key}): ${pass ? 'not ' : ''}${this.utils.printExpected(expectedVal)}\n` +
`Received (key: ${key}): ${this.utils.printReceived(actualVal)}`,
.map(({ key, expectedVal, actualVal }: Result) =>
this.utils.printDiffOrStringify(
expectedVal,
actualVal,
`Expected (key: ${key})`,
`Received (key: ${key})`,
),
)
.join('\n')}`,
};
Expand Down Expand Up @@ -197,10 +210,13 @@ const toHaveLastSentReplay = function (
? 'Expected Replay to not have been sent, but a request was attempted'
: 'Expected Replay to have last been sent, but a request was not attempted'
: `${this.utils.matcherHint('toHaveSentReplay', undefined, undefined, options)}\n\n${results
.map(
({ key, expectedVal, actualVal }: Result) =>
`Expected (key: ${key}): ${pass ? 'not ' : ''}${this.utils.printExpected(expectedVal)}\n` +
`Received (key: ${key}): ${this.utils.printReceived(actualVal)}`,
.map(({ key, expectedVal, actualVal }: Result) =>
this.utils.printDiffOrStringify(
expectedVal,
actualVal,
`Expected (key: ${key})`,
`Received (key: ${key})`,
),
)
.join('\n')}`,
};
Expand Down