From 0d9dd95685fad079d526d62823eb8b9988bed271 Mon Sep 17 00:00:00 2001 From: Sai Kumar Battinoju Date: Thu, 7 Nov 2024 13:57:51 +0530 Subject: [PATCH] test: add unit tests for error utilities --- .../__tests__/utilities/errors.test.ts | 13 +++++++++++++ .../analytics-js/src/components/core/Analytics.ts | 3 ++- 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 packages/analytics-js-common/__tests__/utilities/errors.test.ts diff --git a/packages/analytics-js-common/__tests__/utilities/errors.test.ts b/packages/analytics-js-common/__tests__/utilities/errors.test.ts new file mode 100644 index 000000000..f682c69ce --- /dev/null +++ b/packages/analytics-js-common/__tests__/utilities/errors.test.ts @@ -0,0 +1,13 @@ +import { dispatchErrorEvent } from '../../src/utilities/errors'; + +describe('Errors - utilities', () => { + describe('dispatchErrorEvent', () => { + it('should dispatch an error event', () => { + const dispatchEvent = jest.fn(); + globalThis.dispatchEvent = dispatchEvent; + const error = new Error('Test error'); + dispatchErrorEvent(error); + expect(dispatchEvent).toHaveBeenCalledWith(new ErrorEvent('error', { error })); + }); + }); +}); diff --git a/packages/analytics-js/src/components/core/Analytics.ts b/packages/analytics-js/src/components/core/Analytics.ts index 2b28c1043..ed6986769 100644 --- a/packages/analytics-js/src/components/core/Analytics.ts +++ b/packages/analytics-js/src/components/core/Analytics.ts @@ -180,7 +180,8 @@ class Analytics implements IAnalytics { break; } } catch (err) { - this.errorHandler.onError(getMutatedError(err, 'Life cycle failure'), ANALYTICS_CORE); + const issue = 'Failed to load the SDK'; + this.errorHandler.onError(getMutatedError(err, issue), ANALYTICS_CORE); } }); }