1- import { TextEncoder } from 'util' ;
1+ import { printDiffOrStringify } from 'jest-matcher-utils' ;
2+ import { vi } from 'vitest' ;
3+ import type { Mocked , MockedFunction } from 'vitest' ;
4+
25/* eslint-disable @typescript-eslint/no-unsafe-member-access */
36import { getClient } from '@sentry/core' ;
47import type { ReplayRecordingData , Transport } from '@sentry/types' ;
58import * as SentryUtils from '@sentry/utils' ;
69
710import type { ReplayContainer , Session } from './src/types' ;
811
9- // eslint-disable-next-line @typescript-eslint/no-explicit-any
10- ( global as any ) . TextEncoder = TextEncoder ;
11-
12- type MockTransport = jest . MockedFunction < Transport [ 'send' ] > ;
12+ type MockTransport = MockedFunction < Transport [ 'send' ] > ;
1313
14- jest . spyOn ( SentryUtils , 'isBrowser' ) . mockImplementation ( ( ) => true ) ;
14+ vi . spyOn ( SentryUtils , 'isBrowser' ) . mockImplementation ( ( ) => true ) ;
1515
1616type EnvelopeHeader = {
1717 event_id : string ;
@@ -36,7 +36,7 @@ type SentReplayExpected = {
3636} ;
3737
3838// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
39- const toHaveSameSession = function ( received : jest . Mocked < ReplayContainer > , expected : undefined | Session ) {
39+ const toHaveSameSession = function ( received : Mocked < ReplayContainer > , expected : undefined | Session ) {
4040 const pass = this . equals ( received . session ?. id , expected ?. id ) as boolean ;
4141
4242 const options = {
@@ -47,12 +47,12 @@ const toHaveSameSession = function (received: jest.Mocked<ReplayContainer>, expe
4747 return {
4848 pass,
4949 message : ( ) =>
50- `${ this . utils . matcherHint (
51- 'toHaveSameSession' ,
52- undefined ,
53- undefined ,
54- options ,
55- ) } \n\n ${ this . utils . printDiffOrStringify ( expected , received . session , 'Expected' , 'Received' ) } `,
50+ `${ this . utils . matcherHint ( 'toHaveSameSession' , undefined , undefined , options ) } \n\n ${ printDiffOrStringify (
51+ expected ,
52+ received . session ,
53+ 'Expected' ,
54+ 'Received' ,
55+ ) } `,
5656 } ;
5757} ;
5858
@@ -101,6 +101,7 @@ function checkCallForSentReplay(
101101 : ( expected as SentReplayExpected ) ;
102102
103103 if ( isObjectContaining ) {
104+ // eslint-disable-next-line no-console
104105 console . warn ( '`expect.objectContaining` is unnecessary when using the `toHaveSentReplay` matcher' ) ;
105106 }
106107
@@ -152,7 +153,7 @@ function getReplayCalls(calls: any[][][]): any[][][] {
152153 */
153154// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
154155const toHaveSentReplay = function (
155- _received : jest . Mocked < ReplayContainer > ,
156+ _received : Mocked < ReplayContainer > ,
156157 expected ?: SentReplayExpected | { sample : SentReplayExpected ; inverse : boolean } ,
157158) {
158159 const { calls } = ( getClient ( ) ?. getTransport ( ) ?. send as MockTransport ) . mock ;
@@ -194,12 +195,7 @@ const toHaveSentReplay = function (
194195 : 'Expected Replay to have been sent, but a request was not attempted'
195196 : `${ this . utils . matcherHint ( 'toHaveSentReplay' , undefined , undefined , options ) } \n\n${ results
196197 . map ( ( { key, expectedVal, actualVal } : Result ) =>
197- this . utils . printDiffOrStringify (
198- expectedVal ,
199- actualVal ,
200- `Expected (key: ${ key } )` ,
201- `Received (key: ${ key } )` ,
202- ) ,
198+ printDiffOrStringify ( expectedVal , actualVal , `Expected (key: ${ key } )` , `Received (key: ${ key } )` ) ,
203199 )
204200 . join ( '\n' ) } `,
205201 } ;
@@ -211,7 +207,7 @@ const toHaveSentReplay = function (
211207 */
212208// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
213209const toHaveLastSentReplay = function (
214- _received : jest . Mocked < ReplayContainer > ,
210+ _received : Mocked < ReplayContainer > ,
215211 expected ?: SentReplayExpected | { sample : SentReplayExpected ; inverse : boolean } ,
216212) {
217213 const { calls } = ( getClient ( ) ?. getTransport ( ) ?. send as MockTransport ) . mock ;
@@ -235,12 +231,7 @@ const toHaveLastSentReplay = function (
235231 : 'Expected Replay to have last been sent, but a request was not attempted'
236232 : `${ this . utils . matcherHint ( 'toHaveSentReplay' , undefined , undefined , options ) } \n\n${ results
237233 . map ( ( { key, expectedVal, actualVal } : Result ) =>
238- this . utils . printDiffOrStringify (
239- expectedVal ,
240- actualVal ,
241- `Expected (key: ${ key } )` ,
242- `Received (key: ${ key } )` ,
243- ) ,
234+ printDiffOrStringify ( expectedVal , actualVal , `Expected (key: ${ key } )` , `Received (key: ${ key } )` ) ,
244235 )
245236 . join ( '\n' ) } `,
246237 } ;
@@ -252,18 +243,13 @@ expect.extend({
252243 toHaveLastSentReplay,
253244} ) ;
254245
255- declare global {
256- // eslint-disable-next-line @typescript-eslint/no-namespace
257- namespace jest {
258- interface AsymmetricMatchers {
259- toHaveSentReplay ( expected ?: SentReplayExpected ) : void ;
260- toHaveLastSentReplay ( expected ?: SentReplayExpected ) : void ;
261- toHaveSameSession ( expected : undefined | Session ) : void ;
262- }
263- interface Matchers < R > {
264- toHaveSentReplay ( expected ?: SentReplayExpected ) : R ;
265- toHaveLastSentReplay ( expected ?: SentReplayExpected ) : R ;
266- toHaveSameSession ( expected : undefined | Session ) : R ;
267- }
268- }
246+ interface CustomMatchers < R = unknown > {
247+ toHaveSentReplay ( expected ?: SentReplayExpected ) : R ;
248+ toHaveLastSentReplay ( expected ?: SentReplayExpected ) : R ;
249+ toHaveSameSession ( expected : undefined | Session ) : R ;
250+ }
251+
252+ declare module 'vitest' {
253+ type Assertion < T = any > = CustomMatchers < T > ;
254+ type AsymmetricMatchersContaining = CustomMatchers ;
269255}
0 commit comments