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
+
2
5
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
3
6
import { getClient } from '@sentry/core' ;
4
7
import type { ReplayRecordingData , Transport } from '@sentry/types' ;
5
8
import * as SentryUtils from '@sentry/utils' ;
6
9
7
10
import type { ReplayContainer , Session } from './src/types' ;
8
11
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' ] > ;
13
13
14
- jest . spyOn ( SentryUtils , 'isBrowser' ) . mockImplementation ( ( ) => true ) ;
14
+ vi . spyOn ( SentryUtils , 'isBrowser' ) . mockImplementation ( ( ) => true ) ;
15
15
16
16
type EnvelopeHeader = {
17
17
event_id : string ;
@@ -36,7 +36,7 @@ type SentReplayExpected = {
36
36
} ;
37
37
38
38
// 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 ) {
40
40
const pass = this . equals ( received . session ?. id , expected ?. id ) as boolean ;
41
41
42
42
const options = {
@@ -47,12 +47,12 @@ const toHaveSameSession = function (received: jest.Mocked<ReplayContainer>, expe
47
47
return {
48
48
pass,
49
49
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
+ ) } `,
56
56
} ;
57
57
} ;
58
58
@@ -101,6 +101,7 @@ function checkCallForSentReplay(
101
101
: ( expected as SentReplayExpected ) ;
102
102
103
103
if ( isObjectContaining ) {
104
+ // eslint-disable-next-line no-console
104
105
console . warn ( '`expect.objectContaining` is unnecessary when using the `toHaveSentReplay` matcher' ) ;
105
106
}
106
107
@@ -152,7 +153,7 @@ function getReplayCalls(calls: any[][][]): any[][][] {
152
153
*/
153
154
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
154
155
const toHaveSentReplay = function (
155
- _received : jest . Mocked < ReplayContainer > ,
156
+ _received : Mocked < ReplayContainer > ,
156
157
expected ?: SentReplayExpected | { sample : SentReplayExpected ; inverse : boolean } ,
157
158
) {
158
159
const { calls } = ( getClient ( ) ?. getTransport ( ) ?. send as MockTransport ) . mock ;
@@ -194,12 +195,7 @@ const toHaveSentReplay = function (
194
195
: 'Expected Replay to have been sent, but a request was not attempted'
195
196
: `${ this . utils . matcherHint ( 'toHaveSentReplay' , undefined , undefined , options ) } \n\n${ results
196
197
. 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 } )` ) ,
203
199
)
204
200
. join ( '\n' ) } `,
205
201
} ;
@@ -211,7 +207,7 @@ const toHaveSentReplay = function (
211
207
*/
212
208
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
213
209
const toHaveLastSentReplay = function (
214
- _received : jest . Mocked < ReplayContainer > ,
210
+ _received : Mocked < ReplayContainer > ,
215
211
expected ?: SentReplayExpected | { sample : SentReplayExpected ; inverse : boolean } ,
216
212
) {
217
213
const { calls } = ( getClient ( ) ?. getTransport ( ) ?. send as MockTransport ) . mock ;
@@ -235,12 +231,7 @@ const toHaveLastSentReplay = function (
235
231
: 'Expected Replay to have last been sent, but a request was not attempted'
236
232
: `${ this . utils . matcherHint ( 'toHaveSentReplay' , undefined , undefined , options ) } \n\n${ results
237
233
. 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 } )` ) ,
244
235
)
245
236
. join ( '\n' ) } `,
246
237
} ;
@@ -252,18 +243,13 @@ expect.extend({
252
243
toHaveLastSentReplay,
253
244
} ) ;
254
245
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 ;
269
255
}
0 commit comments