11import { getDefaultTestClientOptions , TestClient } from '../mocks/client' ;
22import { AddAttachmentTestIntegration } from '../mocks/integration' ;
33import { initAndBind } from '../../src/sdk' ;
4- import { captureEvent } from '@sentry/hub' ;
4+ import { captureEvent , configureScope } from '@sentry/hub' ;
5+ import { getGlobalObject } from '@sentry/utils' ;
56
67const PUBLIC_DSN = 'https://username@domain/123' ;
78const sendEvent = jest . spyOn ( TestClient . prototype , 'sendEvent' ) ;
@@ -14,73 +15,87 @@ describe('Hint', () => {
1415
1516 afterEach ( ( ) => {
1617 jest . clearAllMocks ( ) ;
18+ delete getGlobalObject ( ) . __SENTRY__ ;
1719 } ) ;
1820
19- test ( 'can be mutated in beforeSend' , ( ) => {
20- expect . assertions ( 1 ) ;
21+ describe ( 'attachments' , ( ) => {
22+ test ( 'can be mutated in beforeSend' , ( ) => {
23+ expect . assertions ( 1 ) ;
2124
22- const options = getDefaultTestClientOptions ( {
23- dsn : PUBLIC_DSN ,
24- beforeSend : ( event , hint ) => {
25- if ( hint ) {
26- hint . attachments = [ ...( hint ?. attachments || [ ] ) , { filename : 'another.file' , data : 'more text' } ] ;
27- }
25+ const options = getDefaultTestClientOptions ( {
26+ dsn : PUBLIC_DSN ,
27+ beforeSend : ( event , hint ) => {
28+ if ( hint ) {
29+ hint . attachments = [ ...( hint ?. attachments || [ ] ) , { filename : 'another.file' , data : 'more text' } ] ;
30+ }
2831
29- return event ;
30- } ,
31- } ) ;
32-
33- const client = new TestClient ( options ) ;
34- client . captureEvent ( { } ) ;
32+ return event ;
33+ } ,
34+ } ) ;
3535
36- const [ , hint ] = sendEvent . mock . calls [ 0 ] ;
36+ const client = new TestClient ( options ) ;
37+ client . captureEvent ( { } ) ;
3738
38- expect ( hint ) . toEqual ( {
39- attachments : [ { filename : 'another.file' , data : 'more text' } ] ,
39+ const [ , hint ] = sendEvent . mock . calls [ 0 ] ;
40+ expect ( hint ) . toEqual ( { attachments : [ { filename : 'another.file' , data : 'more text' } ] } ) ;
4041 } ) ;
41- } ) ;
4242
43- test ( 'gets passed through to beforeSend and can be further mutated' , ( ) => {
44- expect . assertions ( 1 ) ;
45-
46- const options = getDefaultTestClientOptions ( {
47- dsn : PUBLIC_DSN ,
48- beforeSend : ( event , hint ) => {
49- if ( hint ) {
50- hint . attachments = [ ...( hint ?. attachments || [ ] ) , { filename : 'another.file' , data : 'more text' } ] ;
51- }
52-
53- return event ;
54- } ,
43+ test ( 'gets passed through to beforeSend and can be further mutated' , ( ) => {
44+ expect . assertions ( 1 ) ;
45+
46+ const options = getDefaultTestClientOptions ( {
47+ dsn : PUBLIC_DSN ,
48+ beforeSend : ( event , hint ) => {
49+ if ( hint ) {
50+ hint . attachments = [ ...( hint ?. attachments || [ ] ) , { filename : 'another.file' , data : 'more text' } ] ;
51+ }
52+
53+ return event ;
54+ } ,
55+ } ) ;
56+
57+ const client = new TestClient ( options ) ;
58+ client . captureEvent ( { } , { attachments : [ { filename : 'some-file.txt' , data : 'Hello' } ] } ) ;
59+
60+ const [ , hint ] = sendEvent . mock . calls [ 0 ] ;
61+ expect ( hint ) . toEqual ( {
62+ attachments : [
63+ { filename : 'some-file.txt' , data : 'Hello' } ,
64+ { filename : 'another.file' , data : 'more text' } ,
65+ ] ,
66+ } ) ;
5567 } ) ;
5668
57- const client = new TestClient ( options ) ;
58- client . captureEvent ( { } , { attachments : [ { filename : 'some-file.txt' , data : 'Hello' } ] } ) ;
59-
60- const [ , hint ] = sendEvent . mock . calls [ 0 ] ;
69+ test ( 'can be mutated by an integration via event processor' , ( ) => {
70+ expect . assertions ( 1 ) ;
6171
62- expect ( hint ) . toEqual ( {
63- attachments : [
64- { filename : 'some-file.txt' , data : 'Hello' } ,
65- { filename : 'another.file' , data : 'more text' } ,
66- ] ,
67- } ) ;
68- } ) ;
72+ const options = getDefaultTestClientOptions ( {
73+ dsn : PUBLIC_DSN ,
74+ integrations : [ new AddAttachmentTestIntegration ( ) ] ,
75+ } ) ;
6976
70- test ( 'can be mutated by an integration via event processor' , ( ) => {
71- expect . assertions ( 1 ) ;
77+ initAndBind ( TestClient , options ) ;
78+ captureEvent ( { } ) ;
7279
73- const options = getDefaultTestClientOptions ( {
74- dsn : PUBLIC_DSN ,
75- integrations : [ new AddAttachmentTestIntegration ( ) ] ,
80+ const [ , hint ] = sendEvent . mock . calls [ 0 ] ;
81+ expect ( hint ?. attachments ) . toEqual ( [ { filename : 'integration.file' , data : 'great content!' } ] ) ;
7682 } ) ;
7783
78- initAndBind ( TestClient , options ) ;
84+ test ( 'get copied from scope to hint' , ( ) => {
85+ expect . assertions ( 1 ) ;
7986
80- captureEvent ( { } ) ;
87+ const options = getDefaultTestClientOptions ( { dsn : PUBLIC_DSN } ) ;
88+ initAndBind ( TestClient , options ) ;
8189
82- const [ , hint ] = sendEvent . mock . calls [ 0 ] ;
90+ configureScope ( scope => scope . addAttachment ( { filename : 'scope.file' , data : 'great content!' } ) ) ;
8391
84- expect ( hint ?. attachments ) . toEqual ( [ { filename : 'integration.file' , data : 'great content!' } ] ) ;
92+ captureEvent ( { } , { attachments : [ { filename : 'some-file.txt' , data : 'Hello' } ] } ) ;
93+
94+ const [ , hint ] = sendEvent . mock . calls [ 0 ] ;
95+ expect ( hint ?. attachments ) . toEqual ( [
96+ { filename : 'some-file.txt' , data : 'Hello' } ,
97+ { filename : 'scope.file' , data : 'great content!' } ,
98+ ] ) ;
99+ } ) ;
85100 } ) ;
86101} ) ;
0 commit comments