Skip to content

Commit

Permalink
chore(angular): Add Angular version to event contexts (#5260)
Browse files Browse the repository at this point in the history
Query and send the Angular version in events to Sentry to let us know which Angular versions are used by our users
  • Loading branch information
Lms24 authored Jun 15, 2022
1 parent d69cafc commit 7573f19
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 8 deletions.
6 changes: 6 additions & 0 deletions packages/angular/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const baseConfig = require('../../jest/jest.config.js');

module.exports = {
...baseConfig,
testEnvironment: 'jsdom',
};
5 changes: 4 additions & 1 deletion packages/angular/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
17 changes: 10 additions & 7 deletions packages/angular/src/sdk.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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 });
}
}
16 changes: 16 additions & 0 deletions packages/angular/test/sdk.test.ts
Original file line number Diff line number Diff line change
@@ -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 });
});
});

0 comments on commit 7573f19

Please sign in to comment.