-
-
Notifications
You must be signed in to change notification settings - Fork 344
feat(expo): Add RNSentrySDK APIs support to @sentry/react-native/expo plugin #4633
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
antonis
wants to merge
23
commits into
capture-app-start-errors
Choose a base branch
from
antonis/4625-expo-useNativeInit
base: capture-app-start-errors
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 13 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
313e844
useNativeInit Android implementation
antonis 2e97acc
Adds changelog
antonis 6eedaae
useNativeInit iOS implementation
antonis 9ae5475
Fix indentation
antonis 566550e
Extend test cases with realistic data
antonis 770c9f4
Adds code sample in the changelog
antonis f8b37b5
Fix CHANGELOG.md
antonis d25db30
Warn if RESentySDK.init/start wasn't injected
antonis adc81a5
Make useNativeInit opt-in
antonis 8c2cd73
Make Android failure warning more clear
antonis a2b5575
Make Android no update warning more clear
antonis 5f4f7c5
Use path.basename to get last path component
antonis 0431cc3
Update tests to account for the new warnings
antonis 62d39cc
Explicitly check for kotlin
antonis 235f3ef
Add filename in the warning message
antonis 369cce7
Import only if init injection succeeds
antonis a53c7f4
Explicitly check for Objective-C
antonis 5e4a98f
Add filename in the warning
antonis dce74b2
Make iOS file not found warning more clear
antonis 0ffd26c
Import only if init injection succeeds
antonis 744993c
Reset test mock config in a function
antonis 5447be9
Lint issue
antonis 0b3423f
Add missing quote
antonis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
177 changes: 177 additions & 0 deletions
177
packages/core/test/expo-plugin/modifyAppDelegate.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
import type { ExpoConfig } from '@expo/config-types'; | ||
|
||
import { warnOnce } from '../../plugin/src/utils'; | ||
import { modifyAppDelegate } from '../../plugin/src/withSentryIOS'; | ||
|
||
// Mock dependencies | ||
jest.mock('@expo/config-plugins', () => ({ | ||
...jest.requireActual('@expo/config-plugins'), | ||
withAppDelegate: jest.fn((config, callback) => callback(config)), | ||
})); | ||
|
||
jest.mock('../../plugin/src/utils', () => ({ | ||
warnOnce: jest.fn(), | ||
})); | ||
|
||
interface MockedExpoConfig extends ExpoConfig { | ||
modResults: { | ||
path: string; | ||
contents: string; | ||
language: 'swift' | 'objc'; | ||
}; | ||
} | ||
|
||
const objcContents = `#import "AppDelegate.h" | ||
|
||
@implementation AppDelegate | ||
|
||
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions | ||
{ | ||
self.moduleName = @"main"; | ||
|
||
// You can add your custom initial props in the dictionary below. | ||
// They will be passed down to the ViewController used by React Native. | ||
self.initialProps = @{}; | ||
|
||
return [super application:application didFinishLaunchingWithOptions:launchOptions]; | ||
} | ||
|
||
@end | ||
`; | ||
|
||
const objcExpected = `#import "AppDelegate.h" | ||
#import <RNSentry/RNSentry.h> | ||
|
||
@implementation AppDelegate | ||
|
||
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions | ||
{ | ||
[RNSentrySDK start]; | ||
self.moduleName = @"main"; | ||
|
||
// You can add your custom initial props in the dictionary below. | ||
// They will be passed down to the ViewController used by React Native. | ||
self.initialProps = @{}; | ||
|
||
return [super application:application didFinishLaunchingWithOptions:launchOptions]; | ||
} | ||
|
||
@end | ||
`; | ||
|
||
const swiftContents = `import React | ||
import React_RCTAppDelegate | ||
import ReactAppDependencyProvider | ||
import UIKit | ||
|
||
@main | ||
class AppDelegate: RCTAppDelegate { | ||
override func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool { | ||
self.moduleName = "sentry-react-native-sample" | ||
self.dependencyProvider = RCTAppDependencyProvider() | ||
return super.application(application, didFinishLaunchingWithOptions: launchOptions) | ||
} | ||
}`; | ||
|
||
const swiftExpected = `import React | ||
import React_RCTAppDelegate | ||
import ReactAppDependencyProvider | ||
import UIKit | ||
import RNSentry | ||
|
||
@main | ||
class AppDelegate: RCTAppDelegate { | ||
override func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool { | ||
RNSentrySDK.start() | ||
self.moduleName = "sentry-react-native-sample" | ||
self.dependencyProvider = RCTAppDependencyProvider() | ||
return super.application(application, didFinishLaunchingWithOptions: launchOptions) | ||
} | ||
}`; | ||
|
||
describe('modifyAppDelegate', () => { | ||
let config: MockedExpoConfig; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
// Reset to a mocked Swift config after each test | ||
config = { | ||
krystofwoldrich marked this conversation as resolved.
Show resolved
Hide resolved
|
||
name: 'test', | ||
slug: 'test', | ||
modResults: { | ||
path: 'samples/react-native/ios/AppDelegate.swift', | ||
contents: swiftContents, | ||
language: 'swift', | ||
}, | ||
}; | ||
}); | ||
|
||
it('should skip modification if modResults or path is missing', async () => { | ||
config.modResults.path = undefined; | ||
|
||
const result = await modifyAppDelegate(config); | ||
|
||
expect(warnOnce).toHaveBeenCalledWith('Skipping AppDelegate modification because the file does not exist.'); | ||
expect(result).toBe(config); // No modification | ||
}); | ||
|
||
it('should warn if RNSentrySDK.start() is already present in a Swift project', async () => { | ||
config.modResults.contents = 'RNSentrySDK.start();'; | ||
|
||
const result = await modifyAppDelegate(config); | ||
|
||
expect(warnOnce).toHaveBeenCalledWith(`Your 'AppDelegate.swift' already contains 'RNSentrySDK.start()'.`); | ||
expect(result).toBe(config); // No modification | ||
}); | ||
|
||
it('should warn if [RNSentrySDK start] is already present in an Objective-C project', async () => { | ||
config.modResults.language = 'objc'; | ||
config.modResults.path = 'samples/react-native/ios/AppDelegate.mm'; | ||
config.modResults.contents = '[RNSentrySDK start];'; | ||
|
||
const result = await modifyAppDelegate(config); | ||
|
||
expect(warnOnce).toHaveBeenCalledWith(`Your 'AppDelegate.mm' already contains '[RNSentrySDK start]'.`); | ||
expect(result).toBe(config); // No modification | ||
}); | ||
|
||
it('should modify a Swift file by adding the RNSentrySDK import and start', async () => { | ||
const result = (await modifyAppDelegate(config)) as MockedExpoConfig; | ||
|
||
expect(result.modResults.contents).toContain('import RNSentry'); | ||
expect(result.modResults.contents).toContain('RNSentrySDK.start()'); | ||
expect(result.modResults.contents).toBe(swiftExpected); | ||
}); | ||
|
||
it('should modify an Objective-C file by adding the RNSentrySDK import and start', async () => { | ||
config.modResults.language = 'objc'; | ||
config.modResults.contents = objcContents; | ||
|
||
const result = (await modifyAppDelegate(config)) as MockedExpoConfig; | ||
|
||
expect(result.modResults.contents).toContain('#import <RNSentry/RNSentry.h>'); | ||
expect(result.modResults.contents).toContain('[RNSentrySDK start];'); | ||
expect(result.modResults.contents).toBe(objcExpected); | ||
}); | ||
|
||
it('should insert import statements only once in an Swift project', async () => { | ||
config.modResults.contents = | ||
'import UIKit\nimport RNSentrySDK\n\noverride func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {'; | ||
|
||
const result = (await modifyAppDelegate(config)) as MockedExpoConfig; | ||
|
||
const importCount = (result.modResults.contents.match(/import RNSentrySDK/g) || []).length; | ||
expect(importCount).toBe(1); | ||
}); | ||
|
||
it('should insert import statements only once in an Objective-C project', async () => { | ||
config.modResults.language = 'objc'; | ||
config.modResults.contents = | ||
'#import "AppDelegate.h"\n#import <RNSentry/RNSentry.h>\n\n- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {'; | ||
|
||
const result = (await modifyAppDelegate(config)) as MockedExpoConfig; | ||
|
||
const importCount = (result.modResults.contents.match(/#import <RNSentry\/RNSentry.h>/g) || []).length; | ||
expect(importCount).toBe(1); | ||
}); | ||
}); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.