1
+ /* eslint-disable @typescript-eslint/no-unsafe-member-access */
1
2
import { getCurrentHub } from '@sentry/core' ;
2
3
import { Transport } from '@sentry/types' ;
3
4
4
- import { Session } from './src/session/Session' ;
5
5
import { Replay } from './src' ;
6
+ import { Session } from './src/session/Session' ;
6
7
7
8
// @ts -ignore TS error, this is replaced in prod builds bc of rollup
8
9
global . __SENTRY_REPLAY_VERSION__ = 'version:Test' ;
@@ -40,10 +41,8 @@ type SentReplayExpected = {
40
41
events ?: string | Uint8Array ;
41
42
} ;
42
43
43
- const toHaveSameSession = function (
44
- received : jest . Mocked < Replay > ,
45
- expected : undefined | Session
46
- ) {
44
+ // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
45
+ const toHaveSameSession = function ( received : jest . Mocked < Replay > , expected : undefined | Session ) {
47
46
const pass = this . equals ( received . session ?. id , expected ?. id ) as boolean ;
48
47
49
48
const options = {
@@ -54,13 +53,7 @@ const toHaveSameSession = function (
54
53
return {
55
54
pass,
56
55
message : ( ) =>
57
- this . utils . matcherHint (
58
- 'toHaveSameSession' ,
59
- undefined ,
60
- undefined ,
61
- options
62
- ) +
63
- '\n\n' +
56
+ `${ this . utils . matcherHint ( 'toHaveSameSession' , undefined , undefined , options ) } \n\n` +
64
57
`Expected: ${ pass ? 'not ' : '' } ${ this . utils . printExpected ( expected ) } \n` +
65
58
`Received: ${ this . utils . printReceived ( received . session ) } ` ,
66
59
} ;
@@ -70,23 +63,17 @@ const toHaveSameSession = function (
70
63
* Checks the last call to `fetch` and ensures a replay was uploaded by
71
64
* checking the `fetch()` request's body.
72
65
*/
66
+ // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
73
67
const toHaveSentReplay = function (
74
68
_received : jest . Mocked < Replay > ,
75
- expected ?:
76
- | SentReplayExpected
77
- | { sample : SentReplayExpected ; inverse : boolean }
69
+ expected ?: SentReplayExpected | { sample : SentReplayExpected ; inverse : boolean } ,
78
70
) {
79
- const { calls } = (
80
- getCurrentHub ( ) . getClient ( ) ?. getTransport ( ) ?. send as MockTransport
81
- ) . mock ;
71
+ const { calls } = ( getCurrentHub ( ) . getClient ( ) ?. getTransport ( ) ?. send as MockTransport ) . mock ;
82
72
const lastCall = calls [ calls . length - 1 ] ?. [ 0 ] ;
83
73
84
74
const envelopeHeader = lastCall ?. [ 0 ] ;
85
75
const envelopeItems = lastCall ?. [ 1 ] || [ [ ] , [ ] ] ;
86
- const [
87
- [ replayEventHeader , replayEventPayload ] ,
88
- [ recordingHeader , recordingPayload ] = [ ] ,
89
- ] = envelopeItems ;
76
+ const [ [ replayEventHeader , replayEventPayload ] , [ recordingHeader , recordingPayload ] = [ ] ] = envelopeItems ;
90
77
91
78
// @ts -ignore recordingPayload is always a string in our tests
92
79
const [ recordingPayloadHeader , events ] = recordingPayload ?. split ( '\n' ) || [ ] ;
@@ -100,39 +87,28 @@ const toHaveSentReplay = function (
100
87
replayEventPayload : replayEventPayload ,
101
88
// @ts -ignore Custom envelope
102
89
recordingHeader : recordingHeader ,
103
- recordingPayloadHeader :
104
- recordingPayloadHeader && JSON . parse ( recordingPayloadHeader ) ,
90
+ recordingPayloadHeader : recordingPayloadHeader && JSON . parse ( recordingPayloadHeader ) ,
105
91
events,
106
92
} ;
107
93
108
- const isObjectContaining =
109
- expected && 'sample' in expected && 'inverse' in expected ;
94
+ const isObjectContaining = expected && 'sample' in expected && 'inverse' in expected ;
110
95
const expectedObj = isObjectContaining
111
96
? ( expected as { sample : SentReplayExpected } ) . sample
112
97
: ( expected as SentReplayExpected ) ;
113
98
114
99
if ( isObjectContaining ) {
115
- console . warn (
116
- '`expect.objectContaining` is unnecessary when using the `toHaveSentReplay` matcher'
117
- ) ;
100
+ console . warn ( '`expect.objectContaining` is unnecessary when using the `toHaveSentReplay` matcher' ) ;
118
101
}
119
102
120
103
const results = expected
121
104
? Object . entries ( actualObj )
122
105
. map ( ( [ key , val ] : [ keyof SentReplayExpected , any ] ) => {
123
- return [
124
- ! expectedObj ?. [ key ] || this . equals ( expectedObj [ key ] , val ) ,
125
- key ,
126
- expectedObj ?. [ key ] ,
127
- val ,
128
- ] ;
106
+ return [ ! expectedObj ?. [ key ] || this . equals ( expectedObj [ key ] , val ) , key , expectedObj ?. [ key ] , val ] ;
129
107
} )
130
108
. filter ( ( [ passed ] ) => ! passed )
131
109
: [ ] ;
132
110
133
- const payloadPassed = Boolean (
134
- lastCall && ( ! expected || results . length === 0 )
135
- ) ;
111
+ const payloadPassed = Boolean ( lastCall && ( ! expected || results . length === 0 ) ) ;
136
112
137
113
const options = {
138
114
isNot : this . isNot ,
@@ -148,22 +124,13 @@ const toHaveSentReplay = function (
148
124
? allPass
149
125
? 'Expected Replay to not have been sent, but a request was attempted'
150
126
: 'Expected Replay to have been sent, but a request was not attempted'
151
- : this . utils . matcherHint (
152
- 'toHaveSentReplay' ,
153
- undefined ,
154
- undefined ,
155
- options
156
- ) +
157
- '\n\n' +
158
- results
127
+ : `${ this . utils . matcherHint ( 'toHaveSentReplay' , undefined , undefined , options ) } \n\n${ results
159
128
. map (
160
129
( [ , key , expected , actual ] ) =>
161
- `Expected (key: ${ key } ): ${
162
- payloadPassed ? 'not ' : ''
163
- } ${ this . utils . printExpected ( expected ) } \n` +
164
- `Received (key: ${ key } ): ${ this . utils . printReceived ( actual ) } `
130
+ `Expected (key: ${ key } ): ${ payloadPassed ? 'not ' : '' } ${ this . utils . printExpected ( expected ) } \n` +
131
+ `Received (key: ${ key } ): ${ this . utils . printReceived ( actual ) } ` ,
165
132
)
166
- . join ( '\n' ) ,
133
+ . join ( '\n' ) } ` ,
167
134
} ;
168
135
} ;
169
136
0 commit comments