1- import { Capacitor } from '@capacitor/core' ;
21import type { StackParser } from '@sentry/core' ;
32import type { CapacitorOptions } from 'src' ;
4- import { FilterNativeOptions } from '../src/nativeOptions' ;
3+ // Use shared mock to avoid conflicts
4+ import { setupCapacitorMock } from './mocks/capacitor' ;
5+
6+ setupCapacitorMock ( ) ;
57
6- // Mock the Capacitor module
7- jest . mock ( '@capacitor/core' , ( ) => ( {
8- Capacitor : {
9- getPlatform : jest . fn ( )
10- }
11- } ) ) ;
8+ import { Capacitor } from '@capacitor/core' ;
9+ import { FilterNativeOptions } from '../src/nativeOptions' ;
10+ import { expectPlatformWithReturn } from './extensions/sentryCapacitorJest' ;
1211
1312
1413describe ( 'nativeOptions' , ( ) => {
14+ const mockGetPlatform = Capacitor . getPlatform as jest . Mock ;
15+
16+ beforeEach ( ( ) => {
17+ jest . clearAllMocks ( ) ;
18+ mockGetPlatform . mockReturnValue ( 'web2' ) ;
19+ } ) ;
20+
21+ afterEach ( ( ) => {
22+ jest . restoreAllMocks ( ) ;
23+ } ) ;
1524
16- test ( 'enableWatchdogTerminationTracking is set when defined' , async ( ) => {
25+ test ( 'enableWatchdogTerminationTracking is set when defined' , ( ) => {
1726 const nativeOptions = FilterNativeOptions (
1827 {
1928 enableWatchdogTerminationTracking : true
2029 } ) ;
2130 expect ( nativeOptions . enableWatchdogTerminationTracking ) . toBeTruthy ( ) ;
2231 } ) ;
2332
24- test ( 'enableCaptureFailedRequests is set when defined' , async ( ) => {
33+ test ( 'enableCaptureFailedRequests is set when defined' , ( ) => {
2534 const nativeOptions = FilterNativeOptions (
2635 {
2736 enableCaptureFailedRequests : true
2837 } ) ;
2938 expect ( nativeOptions . enableCaptureFailedRequests ) . toBeTruthy ( ) ;
3039 } ) ;
3140
32- test ( 'invalid types not included' , async ( ) => {
41+ test ( 'invalid types not included' , ( ) => {
3342 const nativeOptions = FilterNativeOptions (
3443 {
3544 _experiments : { key : true } ,
@@ -52,7 +61,6 @@ describe('nativeOptions', () => {
5261 enableNativeNagger : true ,
5362 enableNdkScopeSync : true ,
5463 enableWatchdogTerminationTracking : true ,
55- enableTracing : true ,
5664 enableCaptureFailedRequests : true ,
5765 environment : 'Prod' ,
5866 ignoreErrors : [ 'test' ] ,
@@ -93,22 +101,22 @@ describe('nativeOptions', () => {
93101 expect ( keysFilter . toString ( ) ) . toBe ( '' ) ;
94102 } ) ;
95103
96- test ( 'Should include iOS parameters when running on iOS' , async ( ) => {
97- ( Capacitor . getPlatform as jest . Mock ) . mockReturnValue ( 'ios' ) ;
98-
104+ test ( 'Should include iOS parameters when running on iOS' , ( ) => {
105+ mockGetPlatform . mockReturnValue ( 'ios' ) ;
99106 const expectedOptions : CapacitorOptions = {
100107 environment : 'abc' ,
101108 // iOS parameters
102109 enableAppHangTracking : true ,
103110 appHangTimeoutInterval : 123
104111 } ;
105112 const nativeOptions = FilterNativeOptions ( expectedOptions ) ;
113+
114+ expectPlatformWithReturn ( 'ios' ) ;
106115 expect ( JSON . stringify ( nativeOptions ) ) . toEqual ( JSON . stringify ( expectedOptions ) ) ;
107116 } ) ;
108117
109- test ( 'Should not include iOS parameters when running on android' , async ( ) => {
110- ( Capacitor . getPlatform as jest . Mock ) . mockReturnValue ( 'android' ) ;
111-
118+ test ( 'Should not include iOS parameters when running on android' , ( ) => {
119+ mockGetPlatform . mockReturnValue ( 'android' ) ;
112120 const expectedOptions = {
113121 environment : 'abc' ,
114122 } ;
@@ -118,12 +126,13 @@ describe('nativeOptions', () => {
118126 enableAppHangTracking : true
119127 }
120128 } ) ;
129+
130+ expectPlatformWithReturn ( 'android' ) ;
121131 expect ( nativeOptions ) . toEqual ( expectedOptions ) ;
122132 } ) ;
123133
124- test ( 'Set logger on Android' , async ( ) => {
125- ( Capacitor . getPlatform as jest . Mock ) . mockReturnValue ( 'android' ) ;
126-
134+ test ( 'Set logger on Android' , ( ) => {
135+ mockGetPlatform . mockReturnValue ( 'android' ) ;
127136 const filteredOptions : CapacitorOptions = {
128137 _experiments : { enableLogs : true }
129138 } ;
@@ -133,17 +142,20 @@ describe('nativeOptions', () => {
133142 } ;
134143
135144 const nativeOptions = FilterNativeOptions ( filteredOptions ) ;
145+
146+ expectPlatformWithReturn ( 'android' ) ;
136147 expect ( JSON . stringify ( nativeOptions ) ) . toEqual ( JSON . stringify ( expectedOptions ) ) ;
137148 } ) ;
138149
139- test ( 'Ignore logger on iOS' , async ( ) => {
140- ( Capacitor . getPlatform as jest . Mock ) . mockReturnValue ( 'ios' ) ;
141-
150+ test ( 'Ignore logger on iOS' , ( ) => {
151+ mockGetPlatform . mockReturnValue ( 'ios' ) ;
142152 const filteredOptions : CapacitorOptions = {
143153 _experiments : { enableLogs : true }
144154 } ;
145155
146156 const nativeOptions = FilterNativeOptions ( filteredOptions ) ;
157+
158+ expectPlatformWithReturn ( 'ios' ) ;
147159 expect ( nativeOptions ) . toEqual ( { } ) ;
148160 } ) ;
149161} ) ;
0 commit comments