From 6caf42b24bfaf25f36ea377d936e9dc9447b88f9 Mon Sep 17 00:00:00 2001 From: GIancarlo Buenaflor Date: Wed, 18 Dec 2024 15:08:01 +0100 Subject: [PATCH] update tests --- .../integration_test/web_sdk_test.dart | 111 +++++++++--------- .../src/integrations/web_sdk_integration.dart | 11 +- .../web_sdk_integration_test.dart | 40 ++++++- 3 files changed, 99 insertions(+), 63 deletions(-) diff --git a/flutter/example/integration_test/web_sdk_test.dart b/flutter/example/integration_test/web_sdk_test.dart index 6792c9eec..b9b26a779 100644 --- a/flutter/example/integration_test/web_sdk_test.dart +++ b/flutter/example/integration_test/web_sdk_test.dart @@ -27,73 +27,72 @@ void main() { group('Web SDK Integration', () { IntegrationTestWidgetsFlutterBinding.ensureInitialized(); - tearDown(() async { - await Sentry.close(); - }); - - testWidgets('Sentry JS SDK is callable', (tester) async { - final completer = Completer(); - const expectedMessage = 'test message'; - String actualMessage = ''; - - await restoreFlutterOnErrorAfter(() async { - await SentryFlutter.init((options) { - options.dsn = fakeDsn; - }, appRunner: () async { - await tester.pumpWidget(const app.MyApp()); + group('enabled', () { + testWidgets('Sentry JS SDK is callable', (tester) async { + final completer = Completer(); + const expectedMessage = 'test message'; + + await restoreFlutterOnErrorAfter(() async { + await SentryFlutter.init((options) { + options.initializeNativeJsSdk = true; + options.automatedTestMode = true; + options.dsn = fakeDsn; + }, appRunner: () async { + await tester.pumpWidget(const app.MyApp()); + }); + + final beforeSendFn = (JSObject event, JSObject hint) { + completer.complete(event.getProperty('message'.toJS).toString()); + return event; + }.toJS; + + final options = { + 'dsn': app.exampleDsn, + 'beforeSend': beforeSendFn, + 'debug': true, + 'defaultIntegrations': [], + }.jsify(); + + _init(options); + _captureMessage(expectedMessage.toJS); }); - final beforeSendFn = (JSObject event, JSObject hint) { - actualMessage = event.getProperty('message'.toJS).toString(); - completer.complete(); - return event; - }.toJS; - - final options = { - 'dsn': app.exampleDsn, - 'beforeSend': beforeSendFn, - 'debug': true, - 'defaultIntegrations': [], - }.jsify(); - - _init(options); - _captureMessage(expectedMessage.toJS); - }); + final actualMessage = await completer.future + .timeout(const Duration(seconds: 5), onTimeout: () { + fail('beforeSend was not triggered'); + }); - await completer.future.timeout(const Duration(seconds: 5), onTimeout: () { - fail('beforeSend was not triggered'); + expect(actualMessage, equals(expectedMessage)); }); - expect(actualMessage, equals(expectedMessage)); - }); - - testWidgets('Default: JS SDK initialized', (tester) async { - await restoreFlutterOnErrorAfter(() async { - await SentryFlutter.init((options) { - options.dsn = fakeDsn; - }, appRunner: () async { - await tester.pumpWidget(const app.MyApp()); + testWidgets('Sentry JS SDK initialized', (tester) async { + await restoreFlutterOnErrorAfter(() async { + await SentryFlutter.init((options) { + options.initializeNativeJsSdk = true; + options.automatedTestMode = true; + options.dsn = fakeDsn; + }, appRunner: () async { + await tester.pumpWidget(const app.MyApp()); + }); }); - }); - expect(globalThis['Sentry'], isNotNull); + expect(globalThis['Sentry'], isNotNull); + }); }); - testWidgets('WebSdkIntegration removed: JS SDK not initialized', - (tester) async { - await restoreFlutterOnErrorAfter(() async { - await SentryFlutter.init((options) { - options.dsn = fakeDsn; - // Remove WebSdkIntegration - final integration = options.integrations.firstWhere((integration) => - integration.runtimeType.toString() == 'WebSdkIntegration'); - options.removeIntegration(integration); - }, appRunner: () async { - await tester.pumpWidget(const app.MyApp()); + group('disabled', () { + testWidgets('Sentry JS SDK is not initialized', (tester) async { + await restoreFlutterOnErrorAfter(() async { + await SentryFlutter.init((options) { + options.dsn = fakeDsn; + options.automatedTestMode = true; + }, appRunner: () async { + await tester.pumpWidget(const app.MyApp()); + }); }); - }); - expect(globalThis['Sentry'], isNull); + expect(globalThis['Sentry'], isNull); + }); }); }); } diff --git a/flutter/lib/src/integrations/web_sdk_integration.dart b/flutter/lib/src/integrations/web_sdk_integration.dart index d84fac5c0..d39d747c1 100644 --- a/flutter/lib/src/integrations/web_sdk_integration.dart +++ b/flutter/lib/src/integrations/web_sdk_integration.dart @@ -26,7 +26,7 @@ class WebSdkIntegration implements Integration { @override FutureOr call(Hub hub, SentryFlutterOptions options) async { - if (!options.initializeNativeJsSdk) { + if (!options.initializeNativeJsSdk || !options.autoInitializeNativeSdk) { return; } @@ -37,12 +37,11 @@ class WebSdkIntegration implements Integration { : productionScripts; await _scriptLoader.loadWebSdk(scripts); await _web.init(hub); - options.sdk.addIntegration(name); } catch (exception, stackTrace) { options.logger( SentryLevel.fatal, - '$name failed to be installed', + '$name failed to be installed.', exception: exception, stackTrace: stackTrace, ); @@ -58,7 +57,11 @@ class WebSdkIntegration implements Integration { await _web.close(); await _scriptLoader.close(); } catch (error, stackTrace) { - if (_options?.automatedTestMode == true) {} + _options?.logger(SentryLevel.warning, '$name failed to be closed.', + exception: error, stackTrace: stackTrace); + if (_options?.automatedTestMode == true) { + rethrow; + } } } } diff --git a/flutter/test/integrations/web_sdk_integration_test.dart b/flutter/test/integrations/web_sdk_integration_test.dart index 96f7c4020..3fc7014ee 100644 --- a/flutter/test/integrations/web_sdk_integration_test.dart +++ b/flutter/test/integrations/web_sdk_integration_test.dart @@ -22,8 +22,9 @@ void main() { when(fixture.web.close()).thenReturn(null); }); - group('initializeNativeJsSdk: true', () { + group('enabled', () { setUp(() { + fixture.options.autoInitializeNativeSdk = true; fixture.options.initializeNativeJsSdk = true; }); @@ -43,14 +44,21 @@ void main() { }); }); - // false by default - group('initializeNativeJsSdk: false', () { + // disabled by default + group('disabled', () { test('does not add integration', () async { await sut.call(fixture.hub, fixture.options); expect(fixture.options.sdk.integrations, isNot(contains(WebSdkIntegration.name))); }); + + test('does not load scripts and initialize web', () async { + await sut.call(fixture.hub, fixture.options); + + expect(fixture.scriptLoader.loadScriptsCalls, 0); + verify(fixture.web.init(fixture.hub)).called(0); + }); }); test('closes resources', () async { @@ -59,6 +67,32 @@ void main() { expect(fixture.scriptLoader.closeCalls, 1); verify(fixture.web.close()).called(1); }); + + group('initialization conditions', () { + test('disabled when only autoInitializeNativeSdk is true', () async { + fixture.options.autoInitializeNativeSdk = true; + fixture.options.initializeNativeJsSdk = false; + + await sut.call(fixture.hub, fixture.options); + + expect(fixture.options.sdk.integrations, + isNot(contains(WebSdkIntegration.name))); + expect(fixture.scriptLoader.loadScriptsCalls, 0); + verify(fixture.web.init(fixture.hub)).called(0); + }); + + test('disabled when only initializeNativeJsSdk is true', () async { + fixture.options.autoInitializeNativeSdk = false; + fixture.options.initializeNativeJsSdk = true; + + await sut.call(fixture.hub, fixture.options); + + expect(fixture.options.sdk.integrations, + isNot(contains(WebSdkIntegration.name))); + expect(fixture.scriptLoader.loadScriptsCalls, 0); + verify(fixture.web.init(fixture.hub)).called(0); + }); + }); }); }