diff --git a/packages/angular/jest.config.js b/packages/angular/jest.config.js new file mode 100644 index 000000000000..cd02790794a7 --- /dev/null +++ b/packages/angular/jest.config.js @@ -0,0 +1,6 @@ +const baseConfig = require('../../jest/jest.config.js'); + +module.exports = { + ...baseConfig, + testEnvironment: 'jsdom', +}; diff --git a/packages/angular/package.json b/packages/angular/package.json index ebb30a2ccbe3..1e81abd3a9e5 100644 --- a/packages/angular/package.json +++ b/packages/angular/package.json @@ -52,7 +52,10 @@ "fix:prettier": "prettier --write \"{src,test,scripts}/**/*.ts\"", "lint": "run-s lint:prettier lint:eslint", "lint:eslint": "eslint . --cache --cache-location '../../eslintcache/' --format stylish", - "lint:prettier": "prettier --check \"{src,test,scripts}/**/*.ts\"" + "lint:prettier": "prettier --check \"{src,test,scripts}/**/*.ts\"", + "test": "run-s test:unit", + "test:unit": "jest", + "test:unit:watch": "jest --watch" }, "volta": { "extends": "../../package.json" diff --git a/packages/angular/src/sdk.ts b/packages/angular/src/sdk.ts index bcce7c85ccdb..93b6148f2943 100644 --- a/packages/angular/src/sdk.ts +++ b/packages/angular/src/sdk.ts @@ -1,5 +1,5 @@ import { VERSION } from '@angular/core'; -import { BrowserOptions, init as browserInit, SDK_VERSION } from '@sentry/browser'; +import { BrowserOptions, init as browserInit, SDK_VERSION, setContext } from '@sentry/browser'; import { logger } from '@sentry/utils'; import { ANGULAR_MINIMUM_VERSION } from './constants'; @@ -20,20 +20,23 @@ export function init(options: BrowserOptions): void { ], version: SDK_VERSION, }; - checkAngularVersion(); + + checkAndSetAngularVersion(); browserInit(options); } -function checkAngularVersion(): void { - if (VERSION && VERSION.major) { - const major = parseInt(VERSION.major, 10); - if (major < ANGULAR_MINIMUM_VERSION) { +function checkAndSetAngularVersion(): void { + const angularVersion = VERSION && VERSION.major ? parseInt(VERSION.major, 10) : undefined; + + if (angularVersion) { + if (angularVersion < ANGULAR_MINIMUM_VERSION) { IS_DEBUG_BUILD && logger.warn( - `The Sentry SDK does not officially support Angular ${major}.`, + `The Sentry SDK does not officially support Angular ${angularVersion}.`, `This version of the Sentry SDK supports Angular ${ANGULAR_MINIMUM_VERSION} and above.`, 'Please consider upgrading your Angular version or downgrading the Sentry SDK.', ); } + setContext('angular', { version: angularVersion }); } } diff --git a/packages/angular/test/sdk.test.ts b/packages/angular/test/sdk.test.ts new file mode 100644 index 000000000000..bf5ecabb0ac5 --- /dev/null +++ b/packages/angular/test/sdk.test.ts @@ -0,0 +1,16 @@ +import * as SentryBrowser from '@sentry/browser'; + +import { init } from '../src/sdk'; + +describe('init', () => { + it('sets the Angular version (if available) in the global scope', () => { + const setContextSpy = jest.spyOn(SentryBrowser, 'setContext'); + + init({}); + + // In our case, the Angular version is 10 because that's the version we use for compilation + // (and hence the dependency version of Angular core we installed (see package.json)) + expect(setContextSpy).toHaveBeenCalledTimes(1); + expect(setContextSpy).toHaveBeenCalledWith('angular', { version: 10 }); + }); +});