Skip to content

Commit 7573f19

Browse files
authored
chore(angular): Add Angular version to event contexts (#5260)
Query and send the Angular version in events to Sentry to let us know which Angular versions are used by our users
1 parent d69cafc commit 7573f19

File tree

4 files changed

+36
-8
lines changed

4 files changed

+36
-8
lines changed

packages/angular/jest.config.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const baseConfig = require('../../jest/jest.config.js');
2+
3+
module.exports = {
4+
...baseConfig,
5+
testEnvironment: 'jsdom',
6+
};

packages/angular/package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@
5252
"fix:prettier": "prettier --write \"{src,test,scripts}/**/*.ts\"",
5353
"lint": "run-s lint:prettier lint:eslint",
5454
"lint:eslint": "eslint . --cache --cache-location '../../eslintcache/' --format stylish",
55-
"lint:prettier": "prettier --check \"{src,test,scripts}/**/*.ts\""
55+
"lint:prettier": "prettier --check \"{src,test,scripts}/**/*.ts\"",
56+
"test": "run-s test:unit",
57+
"test:unit": "jest",
58+
"test:unit:watch": "jest --watch"
5659
},
5760
"volta": {
5861
"extends": "../../package.json"

packages/angular/src/sdk.ts

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { VERSION } from '@angular/core';
2-
import { BrowserOptions, init as browserInit, SDK_VERSION } from '@sentry/browser';
2+
import { BrowserOptions, init as browserInit, SDK_VERSION, setContext } from '@sentry/browser';
33
import { logger } from '@sentry/utils';
44

55
import { ANGULAR_MINIMUM_VERSION } from './constants';
@@ -20,20 +20,23 @@ export function init(options: BrowserOptions): void {
2020
],
2121
version: SDK_VERSION,
2222
};
23-
checkAngularVersion();
23+
24+
checkAndSetAngularVersion();
2425
browserInit(options);
2526
}
2627

27-
function checkAngularVersion(): void {
28-
if (VERSION && VERSION.major) {
29-
const major = parseInt(VERSION.major, 10);
30-
if (major < ANGULAR_MINIMUM_VERSION) {
28+
function checkAndSetAngularVersion(): void {
29+
const angularVersion = VERSION && VERSION.major ? parseInt(VERSION.major, 10) : undefined;
30+
31+
if (angularVersion) {
32+
if (angularVersion < ANGULAR_MINIMUM_VERSION) {
3133
IS_DEBUG_BUILD &&
3234
logger.warn(
33-
`The Sentry SDK does not officially support Angular ${major}.`,
35+
`The Sentry SDK does not officially support Angular ${angularVersion}.`,
3436
`This version of the Sentry SDK supports Angular ${ANGULAR_MINIMUM_VERSION} and above.`,
3537
'Please consider upgrading your Angular version or downgrading the Sentry SDK.',
3638
);
3739
}
40+
setContext('angular', { version: angularVersion });
3841
}
3942
}

packages/angular/test/sdk.test.ts

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import * as SentryBrowser from '@sentry/browser';
2+
3+
import { init } from '../src/sdk';
4+
5+
describe('init', () => {
6+
it('sets the Angular version (if available) in the global scope', () => {
7+
const setContextSpy = jest.spyOn(SentryBrowser, 'setContext');
8+
9+
init({});
10+
11+
// In our case, the Angular version is 10 because that's the version we use for compilation
12+
// (and hence the dependency version of Angular core we installed (see package.json))
13+
expect(setContextSpy).toHaveBeenCalledTimes(1);
14+
expect(setContextSpy).toHaveBeenCalledWith('angular', { version: 10 });
15+
});
16+
});

0 commit comments

Comments
 (0)