Skip to content

Commit 9c5e1c9

Browse files
Fix native options test (#959)
* fix native options test * copy test * small refactor, sdkinfo * small refactor, sdkinfo * lots of test logic changes * make test more flexible * comment cleanup
1 parent 1276dfb commit 9c5e1c9

File tree

5 files changed

+68
-47
lines changed

5 files changed

+68
-47
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { Capacitor } from '@capacitor/core';
2+
3+
// Must be imported after the line that mocks Capacitor.
4+
export function expectPlatformWithReturn(platform: string) {
5+
const mockGetPlatform = Capacitor.getPlatform as jest.Mock;
6+
expect(mockGetPlatform).toHaveBeenCalled();
7+
expect(mockGetPlatform).toHaveReturnedWith(platform);
8+
}

test/integrations/sdkinfo.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ jest.mock('../../src/wrapper', () => {
3030

3131
describe('Sdk Info', () => {
3232
afterEach(() => {
33-
3433
NATIVE.platform = 'ios';
3534
});
3635

@@ -40,7 +39,7 @@ describe('Sdk Info', () => {
4039
const processedEvent = await processEvent(mockEvent);
4140

4241
expect(processedEvent?.sdk?.packages).toEqual(expect.arrayContaining([mockiOSPackage]));
43-
expect(processedEvent?.platform === 'javascript');
42+
expect(processedEvent?.platform).toBe('javascript');
4443
expect(mockedFetchNativeSdkInfo).toHaveBeenCalledTimes(1);
4544
});
4645

test/mocks/capacitor.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Shared mock for @capacitor/core to avoid conflicts between test files
2+
export const mockCapacitor = {
3+
WebPlugin: jest.fn(),
4+
registerPlugin: jest.fn(),
5+
Capacitor: {
6+
isPluginAvailable: jest.fn(() => true),
7+
getPlatform: jest.fn(() => 'android'),
8+
},
9+
};
10+
11+
export const setupCapacitorMock = () => {
12+
jest.doMock('@capacitor/core', () => mockCapacitor);
13+
};

test/nativeOptions.test.ts

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,44 @@
1-
import { Capacitor } from '@capacitor/core';
21
import type { StackParser } from '@sentry/core';
32
import 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

1413
describe('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
});

test/wrapper.test.ts

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,15 @@
22
import type { Envelope, EventEnvelope, EventItem, SeverityLevel, TransportMakeRequestResponse } from '@sentry/core';
33
import {createEnvelope, dropUndefinedKeys, logger} from '@sentry/core';
44
import { utf8ToBytes } from '../src/vendor';
5-
import { NATIVE } from '../src/wrapper';
65

76
let getStringBytesLengthValue = 1;
87

9-
function NumberArrayToString(numberArray: number[]): string {
10-
return new TextDecoder().decode(new Uint8Array(numberArray).buffer);
11-
}
8+
// Use shared mock to avoid conflicts
9+
import { setupCapacitorMock } from './mocks/capacitor';
1210

13-
jest.mock(
14-
'@capacitor/core',
15-
() => {
16-
const original = jest.requireActual('@capacitor/core');
17-
18-
return {
19-
WebPlugin: original.WebPlugin,
20-
registerPlugin: jest.fn(),
21-
Capacitor: {
22-
isPluginAvailable: jest.fn(() => true),
23-
getPlatform: jest.fn(() => 'android'),
24-
},
25-
};
26-
},
27-
/* virtual allows us to mock modules that aren't in package.json */
28-
{ virtual: true },
29-
);
11+
setupCapacitorMock();
3012

13+
// Mock the plugin before importing wrapper
3114
jest.mock('../src/plugin', () => {
3215
return {
3316
SentryCapacitor: {
@@ -65,7 +48,13 @@ jest.mock('../src/plugin', () => {
6548
};
6649
});
6750

51+
// Now import after mocks are set up
6852
import { SentryCapacitor } from '../src/plugin';
53+
import { NATIVE } from '../src/wrapper';
54+
55+
function NumberArrayToString(numberArray: number[]): string {
56+
return new TextDecoder().decode(new Uint8Array(numberArray).buffer);
57+
}
6958

7059
beforeEach(() => {
7160
getStringBytesLengthValue = 1;

0 commit comments

Comments
 (0)