Skip to content

feat(replay): Add toHaveLastSentReplay jest matcher #6467

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 3 commits into from
Dec 12, 2022
Merged
Show file tree
Hide file tree
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
154 changes: 117 additions & 37 deletions packages/replay/jest.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,25 @@ afterEach(() => {
(client.getTransport()?.send as MockTransport).mockClear();
});

type SentReplayExpected = {
envelopeHeader?: {
event_id: string;
sent_at: string;
sdk: {
name: string;
version?: string;
};
type EnvelopeHeader = {
event_id: string;
sent_at: string;
sdk: {
name: string;
version?: string;
};
replayEventHeader?: { type: 'replay_event' };
replayEventPayload?: Record<string, unknown>;
recordingHeader?: { type: 'replay_recording'; length: number };
recordingPayloadHeader?: Record<string, unknown>;
};

type ReplayEventHeader = { type: 'replay_event' };
type ReplayEventPayload = Record<string, unknown>;
type RecordingHeader = { type: 'replay_recording'; length: number };
type RecordingPayloadHeader = Record<string, unknown>;
type SentReplayExpected = {
envelopeHeader?: EnvelopeHeader;
replayEventHeader?: ReplayEventHeader;
replayEventPayload?: ReplayEventPayload;
recordingHeader?: RecordingHeader;
recordingPayloadHeader?: RecordingPayloadHeader;
events?: string | Uint8Array;
};

Expand All @@ -70,20 +76,27 @@ const toHaveSameSession = function (received: jest.Mocked<ReplayContainer>, expe
};
};

/**
* Checks the last call to `fetch` and ensures a replay was uploaded by
* checking the `fetch()` request's body.
*/
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
const toHaveSentReplay = function (
_received: jest.Mocked<ReplayContainer>,
type Result = {
passed: boolean;
key: string;
expectedVal: SentReplayExpected[keyof SentReplayExpected];
actualVal: SentReplayExpected[keyof SentReplayExpected];
};
type Call = [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I'm wondering if "Call" is a good name here. Isn't this just an Envelope?

EnvelopeHeader,
[
[ReplayEventHeader | undefined, ReplayEventPayload | undefined],
[RecordingHeader | undefined, RecordingPayloadHeader | undefined],
],
];
type CheckCallForSentReplayResult = { pass: boolean; call: Call | undefined; results: Result[] };

function checkCallForSentReplay(
call: Call | undefined,
expected?: SentReplayExpected | { sample: SentReplayExpected; inverse: boolean },
) {
const { calls } = (getCurrentHub().getClient()?.getTransport()?.send as MockTransport).mock;
const lastCall = calls[calls.length - 1]?.[0];

const envelopeHeader = lastCall?.[0];
const envelopeItems = lastCall?.[1] || [[], []];
): CheckCallForSentReplayResult {
const envelopeHeader = call?.[0];
const envelopeItems = call?.[1] || [[], []];
const [[replayEventHeader, replayEventPayload], [recordingHeader, recordingPayload] = []] = envelopeItems;

// @ts-ignore recordingPayload is always a string in our tests
Expand Down Expand Up @@ -116,34 +129,98 @@ const toHaveSentReplay = function (
.map(key => {
const actualVal = actualObj[key as keyof SentReplayExpected];
const expectedVal = expectedObj[key as keyof SentReplayExpected];
const matches = !expectedVal || this.equals(actualVal, expectedVal);
const passed = !expectedVal || this.equals(actualVal, expectedVal);

return [matches, key, expectedVal, actualVal];
return { passed, key, expectedVal, actualVal };
})
.filter(([passed]) => !passed)
.filter(({ passed }) => !passed)
: [];

const payloadPassed = Boolean(lastCall && (!expected || results.length === 0));
const pass = Boolean(call && (!expected || results.length === 0));

return {
pass,
call,
results,
};
}

/**
* Checks all calls to `fetch` and ensures a replay was uploaded by
* checking the `fetch()` request's body.
*/
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
const toHaveSentReplay = function (
_received: jest.Mocked<ReplayContainer>,
expected?: SentReplayExpected | { sample: SentReplayExpected; inverse: boolean },
) {
const { calls } = (getCurrentHub().getClient()?.getTransport()?.send as MockTransport).mock;

let result: CheckCallForSentReplayResult;

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

// @ts-ignore use before assigned
const { results, call, pass } = result;

const options = {
isNot: this.isNot,
promise: this.promise,
};

const allPass = payloadPassed;

return {
pass: allPass,
pass,
message: () =>
!lastCall
? allPass
!call
? pass
? '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, expected, actual]) =>
`Expected (key: ${key}): ${payloadPassed ? 'not ' : ''}${this.utils.printExpected(expected)}\n` +
`Received (key: ${key}): ${this.utils.printReceived(actual)}`,
({ key, expectedVal, actualVal }: Result) =>
`Expected (key: ${key}): ${pass ? 'not ' : ''}${this.utils.printExpected(expectedVal)}\n` +
`Received (key: ${key}): ${this.utils.printReceived(actualVal)}`,
)
.join('\n')}`,
};
};

/**
* Checks the last call to `fetch` and ensures a replay was uploaded by
* checking the `fetch()` request's body.
*/
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
const toHaveLastSentReplay = function (
_received: jest.Mocked<ReplayContainer>,
expected?: SentReplayExpected | { sample: SentReplayExpected; inverse: boolean },
) {
const { calls } = (getCurrentHub().getClient()?.getTransport()?.send as MockTransport).mock;
const lastCall = calls[calls.length - 1]?.[0];

const { results, call, pass } = checkCallForSentReplay.call(this, lastCall, expected);

const options = {
isNot: this.isNot,
promise: this.promise,
};

return {
pass,
message: () =>
!call
? pass
? '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)}`,
)
.join('\n')}`,
};
Expand All @@ -152,17 +229,20 @@ const toHaveSentReplay = function (
expect.extend({
toHaveSameSession,
toHaveSentReplay,
toHaveLastSentReplay,
});

declare global {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace jest {
interface AsymmetricMatchers {
toHaveSentReplay(expected?: SentReplayExpected): void;
toHaveLastSentReplay(expected?: SentReplayExpected): void;
toHaveSameSession(expected: undefined | Session): void;
}
interface Matchers<R> {
toHaveSentReplay(expected?: SentReplayExpected): R;
toHaveLastSentReplay(expected?: SentReplayExpected): R;
toHaveSameSession(expected: undefined | Session): R;
}
}
Expand Down
Loading