From f45bceeb548eeb634f843c9ef40f8ceed6da1fe2 Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Mon, 17 Oct 2022 11:39:25 +0200 Subject: [PATCH 1/9] fix(utils): Rename global.ts -> globalobject.ts --- packages/utils/src/browser.ts | 2 +- packages/utils/src/{global.ts => globalobject.ts} | 3 +++ packages/utils/src/index.ts | 2 +- packages/utils/src/logger.ts | 2 +- packages/utils/src/misc.ts | 2 +- packages/utils/test/{global.test.ts => globalobject.test.ts} | 2 +- 6 files changed, 8 insertions(+), 5 deletions(-) rename packages/utils/src/{global.ts => globalobject.ts} (94%) rename packages/utils/test/{global.test.ts => globalobject.test.ts} (84%) diff --git a/packages/utils/src/browser.ts b/packages/utils/src/browser.ts index 30cd546c6ad8..992d18254d21 100644 --- a/packages/utils/src/browser.ts +++ b/packages/utils/src/browser.ts @@ -1,4 +1,4 @@ -import { GLOBAL_OBJ } from './global'; +import { GLOBAL_OBJ } from './globalobject'; import { isString } from './is'; /** diff --git a/packages/utils/src/global.ts b/packages/utils/src/globalobject.ts similarity index 94% rename from packages/utils/src/global.ts rename to packages/utils/src/globalobject.ts index 72163749214a..04ac73f3d97e 100644 --- a/packages/utils/src/global.ts +++ b/packages/utils/src/globalobject.ts @@ -1,6 +1,9 @@ /** * NOTE: In order to avoid circular dependencies, if you add a function to this module and it needs to print something, * you must either a) use `console.log` rather than the logger, or b) put your function elsewhere. + * + * Note: This file was originally called `global.ts`, but was changed to unblock users which might be doing + * string replaces with bundlers like Vite for `global` (would break imports that rely on importing from utils/src/global). */ /* eslint-disable @typescript-eslint/no-explicit-any */ diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts index b55e3b0303be..5e8b3e05ff3e 100644 --- a/packages/utils/src/index.ts +++ b/packages/utils/src/index.ts @@ -1,7 +1,7 @@ export * from './browser'; export * from './dsn'; export * from './error'; -export * from './global'; +export * from './globalobject'; export * from './instrument'; export * from './is'; export * from './logger'; diff --git a/packages/utils/src/logger.ts b/packages/utils/src/logger.ts index 031c969a930d..51d7f05d25c0 100644 --- a/packages/utils/src/logger.ts +++ b/packages/utils/src/logger.ts @@ -1,6 +1,6 @@ import { WrappedFunction } from '@sentry/types'; -import { getGlobalSingleton, GLOBAL_OBJ } from './global'; +import { getGlobalSingleton, GLOBAL_OBJ } from './globalobject'; /** Prefix for logging strings */ const PREFIX = 'Sentry Logger '; diff --git a/packages/utils/src/misc.ts b/packages/utils/src/misc.ts index 8c24439a47d4..287f9cb82c59 100644 --- a/packages/utils/src/misc.ts +++ b/packages/utils/src/misc.ts @@ -1,7 +1,7 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ import { Event, Exception, Mechanism, StackFrame } from '@sentry/types'; -import { GLOBAL_OBJ } from './global'; +import { GLOBAL_OBJ } from './globalobject'; import { addNonEnumerableProperty } from './object'; import { snipLine } from './string'; diff --git a/packages/utils/test/global.test.ts b/packages/utils/test/globalobject.test.ts similarity index 84% rename from packages/utils/test/global.test.ts rename to packages/utils/test/globalobject.test.ts index 722abf678bb0..1a9d7f6044ec 100644 --- a/packages/utils/test/global.test.ts +++ b/packages/utils/test/globalobject.test.ts @@ -1,4 +1,4 @@ -import { GLOBAL_OBJ } from '../src/global'; +import { GLOBAL_OBJ } from '../src/globalobject'; describe('GLOBAL_OBJ', () => { test('should return the same object', () => { From 9f99428f217351433d3146745e53e5f2fb6a8818 Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Mon, 17 Oct 2022 09:43:34 +0000 Subject: [PATCH 2/9] ref(build): Remove `constToVarPlugin` --- rollup/npmHelpers.js | 3 --- rollup/plugins/npmPlugins.js | 26 -------------------------- 2 files changed, 29 deletions(-) diff --git a/rollup/npmHelpers.js b/rollup/npmHelpers.js index 2a5724a9363c..b7d0fd187e14 100644 --- a/rollup/npmHelpers.js +++ b/rollup/npmHelpers.js @@ -8,7 +8,6 @@ import * as path from 'path'; import deepMerge from 'deepmerge'; import { - makeConstToVarPlugin, makeExtractPolyfillsPlugin, makeNodeResolvePlugin, makeCleanupPlugin, @@ -30,7 +29,6 @@ export function makeBaseNPMConfig(options = {}) { const nodeResolvePlugin = makeNodeResolvePlugin(); const sucrasePlugin = makeSucrasePlugin(); const debugBuildStatementReplacePlugin = makeDebugBuildStatementReplacePlugin(); - const constToVarPlugin = makeConstToVarPlugin(); const cleanupPlugin = makeCleanupPlugin(); const extractPolyfillsPlugin = makeExtractPolyfillsPlugin(); @@ -81,7 +79,6 @@ export function makeBaseNPMConfig(options = {}) { nodeResolvePlugin, sucrasePlugin, debugBuildStatementReplacePlugin, - constToVarPlugin, cleanupPlugin, extractPolyfillsPlugin, ], diff --git a/rollup/plugins/npmPlugins.js b/rollup/plugins/npmPlugins.js index 27e5d96ae59c..ec162615623f 100644 --- a/rollup/plugins/npmPlugins.js +++ b/rollup/plugins/npmPlugins.js @@ -22,32 +22,6 @@ export function makeSucrasePlugin() { }); } -/** - * Create a plugin to switch all instances of `const` to `var`, both to prevent problems when we shadow `global` and - * because it's fewer characters. - * - * Note that the generated plugin runs the replacement both before and after rollup does its code manipulation, to - * increase the chances that nothing is missed. - * - * TODO This is pretty brute-force-y. Perhaps we could switch to using a parser, the way we (will) do for both our jest - * transformer and the polyfill build script. - * - * @returns An instance of the `@rollup/plugin-replace` plugin - */ -export function makeConstToVarPlugin() { - return replace({ - // TODO `preventAssignment` will default to true in version 5.x of the replace plugin, at which point we can get rid - // of this. (It actually makes no difference in this case whether it's true or false, since we never assign to - // `const`, but if we don't give it a value, it will spam with warnings.) - preventAssignment: true, - values: { - // Include a space at the end to guarantee we're not accidentally catching the beginning of the words "constant," - // "constantly," etc. - 'const ': 'var ', - }, - }); -} - /** * Create a plugin which can be used to pause the build process at the given hook. * From a01d1a35ffced2c317087c5adaf81beef50731ab Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Mon, 17 Oct 2022 11:49:45 +0200 Subject: [PATCH 3/9] Use worldwide instead of global lol --- packages/utils/src/browser.ts | 2 +- packages/utils/src/index.ts | 2 +- packages/utils/src/logger.ts | 2 +- packages/utils/src/misc.ts | 2 +- packages/utils/src/{globalobject.ts => worldwide.ts} | 4 ++++ .../utils/test/{globalobject.test.ts => worldwide.test.ts} | 2 +- 6 files changed, 9 insertions(+), 5 deletions(-) rename packages/utils/src/{globalobject.ts => worldwide.ts} (99%) rename packages/utils/test/{globalobject.test.ts => worldwide.test.ts} (84%) diff --git a/packages/utils/src/browser.ts b/packages/utils/src/browser.ts index 992d18254d21..9b31bce4f2e1 100644 --- a/packages/utils/src/browser.ts +++ b/packages/utils/src/browser.ts @@ -1,4 +1,4 @@ -import { GLOBAL_OBJ } from './globalobject'; +import { GLOBAL_OBJ } from './worldwide'; import { isString } from './is'; /** diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts index 5e8b3e05ff3e..260360522685 100644 --- a/packages/utils/src/index.ts +++ b/packages/utils/src/index.ts @@ -1,7 +1,7 @@ export * from './browser'; export * from './dsn'; export * from './error'; -export * from './globalobject'; +export * from './worldwide'; export * from './instrument'; export * from './is'; export * from './logger'; diff --git a/packages/utils/src/logger.ts b/packages/utils/src/logger.ts index 51d7f05d25c0..efa8f49716b9 100644 --- a/packages/utils/src/logger.ts +++ b/packages/utils/src/logger.ts @@ -1,6 +1,6 @@ import { WrappedFunction } from '@sentry/types'; -import { getGlobalSingleton, GLOBAL_OBJ } from './globalobject'; +import { getGlobalSingleton, GLOBAL_OBJ } from './worldwide'; /** Prefix for logging strings */ const PREFIX = 'Sentry Logger '; diff --git a/packages/utils/src/misc.ts b/packages/utils/src/misc.ts index 287f9cb82c59..0cc9629663d7 100644 --- a/packages/utils/src/misc.ts +++ b/packages/utils/src/misc.ts @@ -1,7 +1,7 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ import { Event, Exception, Mechanism, StackFrame } from '@sentry/types'; -import { GLOBAL_OBJ } from './globalobject'; +import { GLOBAL_OBJ } from './worldwide'; import { addNonEnumerableProperty } from './object'; import { snipLine } from './string'; diff --git a/packages/utils/src/globalobject.ts b/packages/utils/src/worldwide.ts similarity index 99% rename from packages/utils/src/globalobject.ts rename to packages/utils/src/worldwide.ts index 04ac73f3d97e..b882136689d0 100644 --- a/packages/utils/src/globalobject.ts +++ b/packages/utils/src/worldwide.ts @@ -4,6 +4,10 @@ * * Note: This file was originally called `global.ts`, but was changed to unblock users which might be doing * string replaces with bundlers like Vite for `global` (would break imports that rely on importing from utils/src/global). + * + * Why worldwide? + * + * Why not? */ /* eslint-disable @typescript-eslint/no-explicit-any */ diff --git a/packages/utils/test/globalobject.test.ts b/packages/utils/test/worldwide.test.ts similarity index 84% rename from packages/utils/test/globalobject.test.ts rename to packages/utils/test/worldwide.test.ts index 1a9d7f6044ec..6137c95c093b 100644 --- a/packages/utils/test/globalobject.test.ts +++ b/packages/utils/test/worldwide.test.ts @@ -1,4 +1,4 @@ -import { GLOBAL_OBJ } from '../src/globalobject'; +import { GLOBAL_OBJ } from '../src/worldwide'; describe('GLOBAL_OBJ', () => { test('should return the same object', () => { From 77d51a603ce74219d3404ff2960828b445e8fbe4 Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Mon, 17 Oct 2022 10:08:31 +0000 Subject: [PATCH 4/9] Remove jest transformer --- jest/transformers/constReplacer.ts | 84 ------------------------------ scripts/test.ts | 38 -------------- 2 files changed, 122 deletions(-) delete mode 100644 jest/transformers/constReplacer.ts diff --git a/jest/transformers/constReplacer.ts b/jest/transformers/constReplacer.ts deleted file mode 100644 index 725aa9ac48c8..000000000000 --- a/jest/transformers/constReplacer.ts +++ /dev/null @@ -1,84 +0,0 @@ -/** - * This is a transformer which `ts-jest` applies during the compilation process, which switches all of the `const`s out - * for `var`s. Unlike in our package builds, where we do the same substiution for bundle size reasons, here we do it - * because otherwise `const global = getGlobalObject()` throws an error about redifining `global`. (This didn't used to - * be a problem because our down-compilation did the `const`-`var` substitution for us, but now that we're ES6-only, we - * have to do it ourselves.) - * - * Note: If you ever have to change this, and are testing it locally in the process, be sure to call - * `yarn jest --clearCache` - * before each test run, as transformation results are cached between runs. - */ - -import { - createVariableDeclarationList, - getCombinedNodeFlags, - isVariableDeclarationList, - Node, - NodeFlags, - SourceFile, - TransformationContext, - Transformer, - TransformerFactory, - visitEachChild, - visitNode, - VisitResult, -} from 'typescript'; - -// These can be anything - they're just used to construct a cache key for the transformer returned by the factory below. -// This really only matters when you're testing the transformer itself, as changing these values gives you a quick way -// to invalidate the cache and ensure that changes you've made to the code here are immediately picked up on and used. -export const name = 'const-to-var'; -export const version = '1.0'; - -/** - * Check whether the given AST node represents a `const` token. - * - * This function comes from the TS compiler, and is copied here to get around the fact that it's not exported by the - * `typescript` package. - * - * @param node The node to check - * @returns A boolean indicating if the node is a `const` token. - */ -function isVarConst(node: Node): boolean { - // eslint-disable-next-line no-bitwise - return !!(getCombinedNodeFlags(node) & NodeFlags.Const); -} - -/** - * Return a set of nested factory functions, which ultimately creates an AST-node visitor function, which can modify - * each visited node as it sees fit, and uses it to walk the AST, returning the results. - * - * In our case, we're modifying all `const` variable declarations to use `var` instead. - */ -export function factory(): TransformerFactory { - // Create the transformer - function transformerFactory(context: TransformationContext): Transformer { - // Create a visitor function and use it to walk the AST - function transformer(sourceFile: SourceFile): SourceFile { - // This visitor function can either return a node, in which case the subtree rooted at the returned node is - // substituted for the subtree rooted at the visited node, or can use the recursive `visitEachChild` function - // provided by TS to continue traversing the tree. - function visitor(node: Node): VisitResult { - // If we've found a `const` declaration, return a `var` declaration in its place - if (isVariableDeclarationList(node) && isVarConst(node)) { - // A declaration list with a `None` flag defaults to using `var` - return createVariableDeclarationList(node.declarations, NodeFlags.None); - } - - // This wasn't a node we're interested in, so keep walking down the tree. - return visitEachChild(node, visitor, context); - } - - // Having defined our visitor, pass it to the TS-provided `visitNode` function, which will use it to walk the AST, - // and return the results of that walk. - return visitNode(sourceFile, visitor); - } - - // Back in the transformer factory, return the transformer we just created - return transformer; - } - - // Finally, we're back in `factory`, and can return the whole nested system - return transformerFactory; -} diff --git a/scripts/test.ts b/scripts/test.ts index a4904abe50f6..d52e57450f8b 100644 --- a/scripts/test.ts +++ b/scripts/test.ts @@ -62,42 +62,6 @@ function installLegacyDeps(legacyDeps: string[] = []): void { run(`yarn add --dev --ignore-engines --ignore-scripts --ignore-workspace-root-check ${legacyDeps.join(' ')}`); } -/** - * Add a tranformer to our jest config, to do the same `const`-to-`var` replacement as our rollup plugin does. - * - * This is needed because Node 8 doesn't like the way we shadow `global` (`const global = getGlobalObject()`). Changing - * it to a `var` solves this by making it redeclarable. - * - */ -function addJestTransformer(): void { - // Though newer `ts-jest` versions support transformers written in TS, the legacy version does not. - run('yarn tsc --skipLibCheck jest/transformers/constReplacer.ts'); - - // Loading the existing Jest config will error out unless the config file has an accompanying types file, so we have - // to create that before we can load it. - run('yarn tsc --allowJs --skipLibCheck --declaration --emitDeclarationOnly jest/jest.config.js'); - // eslint-disable-next-line @typescript-eslint/no-var-requires - const jestConfig = require('../jest/jest.config.js'); - - // Inject the transformer - jestConfig.globals['ts-jest'].astTransformers = ['/../../jest/transformers/constReplacer.js']; - - // When we required the jest config file above, all expressions it contained were evaluated. Specifically, the - // `rootDir: process.cwd()` - // entry was replaced with - // `rootDir: ""`, - // Though it's a little brute-force-y, the easiest way to fix this is to just stringify the code and perform the - // substitution in reverse. - const stringifiedConfig = JSON.stringify(jestConfig, null, 2).replace( - `"rootDir": "${process.cwd()}"`, - 'rootDir: process.cwd()', - ); - - // Now we just have to convert it back to a module and write it to disk - const code = `module.exports = ${stringifiedConfig}`; - fs.writeFileSync(path.resolve('jest/jest.config.js'), code); -} - /** * Modify a json file on disk. * @@ -151,8 +115,6 @@ function runWithIgnores(skipPackages: string[] = []): void { function runTests(): void { if (CURRENT_NODE_VERSION === '8') { installLegacyDeps(NODE_8_LEGACY_DEPENDENCIES); - // Inject a `const`-to-`var` transformer, in order to stop Node 8 from complaining when we shadow `global` - addJestTransformer(); // TODO Right now, this just skips incompatible tests, but it could be skipping more (hence the aspirational name), // and not just in Node 8. See `skipNonNodeTests`'s docstring. skipNonNodeTests(); From 911aa81de08b6141e56ec46d3a34241c97252f0f Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Mon, 17 Oct 2022 10:09:15 +0000 Subject: [PATCH 5/9] Remove assignments to `global` --- packages/browser/src/integrations/trycatch.ts | 4 ++-- packages/nextjs/src/performance/client.ts | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/browser/src/integrations/trycatch.ts b/packages/browser/src/integrations/trycatch.ts index af06c54d6290..b1ef94a89e1d 100644 --- a/packages/browser/src/integrations/trycatch.ts +++ b/packages/browser/src/integrations/trycatch.ts @@ -183,9 +183,9 @@ function _wrapXHR(originalSend: () => void): () => void { /** JSDoc */ function _wrapEventTarget(target: string): void { // eslint-disable-next-line @typescript-eslint/no-explicit-any - const global = WINDOW as { [key: string]: any }; + const globalObject = WINDOW as { [key: string]: any }; // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access - const proto = global[target] && global[target].prototype; + const proto = globalObject[target] && globalObject[target].prototype; // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, no-prototype-builtins if (!proto || !proto.hasOwnProperty || !proto.hasOwnProperty('addEventListener')) { diff --git a/packages/nextjs/src/performance/client.ts b/packages/nextjs/src/performance/client.ts index 0702a35f8f67..6c918644f940 100644 --- a/packages/nextjs/src/performance/client.ts +++ b/packages/nextjs/src/performance/client.ts @@ -11,7 +11,7 @@ import type { NEXT_DATA as NextData } from 'next/dist/next-server/lib/utils'; import { default as Router } from 'next/router'; import type { ParsedUrlQuery } from 'querystring'; -const global = WINDOW as typeof WINDOW & { +const globalObject = WINDOW as typeof WINDOW & { __BUILD_MANIFEST?: { sortedPages?: string[]; }; @@ -57,7 +57,7 @@ function extractNextDataTagInformation(): NextDataTagInfo { let nextData: SentryEnhancedNextData | undefined; // Let's be on the safe side and actually check first if there is really a __NEXT_DATA__ script tag on the page. // Theoretically this should always be the case though. - const nextDataTag = global.document.getElementById('__NEXT_DATA__'); + const nextDataTag = globalObject.document.getElementById('__NEXT_DATA__'); if (nextDataTag && nextDataTag.innerHTML) { try { nextData = JSON.parse(nextDataTag.innerHTML); @@ -122,7 +122,7 @@ export function nextRouterInstrumentation( startTransactionOnLocationChange: boolean = true, ): void { const { route, traceParentData, baggage, params } = extractNextDataTagInformation(); - prevLocationName = route || global.location.pathname; + prevLocationName = route || globalObject.location.pathname; if (startTransactionOnPageLoad) { const source = route ? 'route' : 'url'; @@ -197,7 +197,7 @@ export function nextRouterInstrumentation( } function getNextRouteFromPathname(pathname: string): string | undefined { - const pageRoutes = (global.__BUILD_MANIFEST || {}).sortedPages; + const pageRoutes = (globalObject.__BUILD_MANIFEST || {}).sortedPages; // Page route should in 99.999% of the cases be defined by now but just to be sure we make a check here if (!pageRoutes) { From f168c92cf9c39601f8c229608e8646495393d741 Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Mon, 17 Oct 2022 12:12:46 +0200 Subject: [PATCH 6/9] run linter --- packages/utils/src/browser.ts | 2 +- packages/utils/src/misc.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/utils/src/browser.ts b/packages/utils/src/browser.ts index 9b31bce4f2e1..25ccc443a03c 100644 --- a/packages/utils/src/browser.ts +++ b/packages/utils/src/browser.ts @@ -1,5 +1,5 @@ -import { GLOBAL_OBJ } from './worldwide'; import { isString } from './is'; +import { GLOBAL_OBJ } from './worldwide'; /** * TODO: Move me to @sentry/browser when @sentry/utils no longer contains any browser code diff --git a/packages/utils/src/misc.ts b/packages/utils/src/misc.ts index 0cc9629663d7..aacb4de786e0 100644 --- a/packages/utils/src/misc.ts +++ b/packages/utils/src/misc.ts @@ -1,9 +1,9 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ import { Event, Exception, Mechanism, StackFrame } from '@sentry/types'; -import { GLOBAL_OBJ } from './worldwide'; import { addNonEnumerableProperty } from './object'; import { snipLine } from './string'; +import { GLOBAL_OBJ } from './worldwide'; interface CryptoInternal { getRandomValues(array: Uint8Array): Uint8Array; From f9a461015a7d0c475bfa3403538c280f3d54b9ff Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Mon, 17 Oct 2022 10:22:43 +0000 Subject: [PATCH 7/9] Remove unused import --- scripts/test.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/scripts/test.ts b/scripts/test.ts index d52e57450f8b..0ed419da8ea7 100644 --- a/scripts/test.ts +++ b/scripts/test.ts @@ -1,6 +1,5 @@ import * as childProcess from 'child_process'; import * as fs from 'fs'; -import * as path from 'path'; const CURRENT_NODE_VERSION = process.version.replace('v', '').split('.')[0]; From 8596f545e665a22b34b3a35621d7baf86f1f07e4 Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Mon, 17 Oct 2022 10:56:26 +0000 Subject: [PATCH 8/9] Try and fix tests --- packages/browser/test/unit/index.test.ts | 23 +++++------ packages/browser/test/unit/sdk.test.ts | 2 - packages/core/test/lib/base.test.ts | 4 +- packages/core/test/lib/sdk.test.ts | 5 +-- packages/hub/test/exports.test.ts | 50 ++++++++++++------------ packages/node/test/index.test.ts | 15 +++---- packages/node/test/sdk.test.ts | 5 +-- 7 files changed, 44 insertions(+), 60 deletions(-) diff --git a/packages/browser/test/unit/index.test.ts b/packages/browser/test/unit/index.test.ts index 37fa972512a1..d3e856a8d7f7 100644 --- a/packages/browser/test/unit/index.test.ts +++ b/packages/browser/test/unit/index.test.ts @@ -21,9 +21,6 @@ import { makeSimpleTransport } from './mocks/simpletransport'; const dsn = 'https://53039209a22b4ec1bcc296a3c9fdecd6@sentry.io/4291'; -// eslint-disable-next-line no-var -declare var global: any; - jest.mock('@sentry/core', () => { const original = jest.requireActual('@sentry/core'); return { @@ -57,7 +54,7 @@ describe('SentryBrowser', () => { configureScope((scope: Scope) => { scope.setExtra('abc', { def: [1] }); }); - expect(global.__SENTRY__.hub._stack[1].scope._extra).toEqual({ + expect((global as any).__SENTRY__.hub._stack[1].scope._extra).toEqual({ abc: { def: [1] }, }); }); @@ -66,7 +63,7 @@ describe('SentryBrowser', () => { configureScope((scope: Scope) => { scope.setTag('abc', 'def'); }); - expect(global.__SENTRY__.hub._stack[1].scope._tags).toEqual({ + expect((global as any).__SENTRY__.hub._stack[1].scope._tags).toEqual({ abc: 'def', }); }); @@ -75,7 +72,7 @@ describe('SentryBrowser', () => { configureScope((scope: Scope) => { scope.setUser({ id: 'def' }); }); - expect(global.__SENTRY__.hub._stack[1].scope._user).toEqual({ + expect((global as any).__SENTRY__.hub._stack[1].scope._user).toEqual({ id: 'def', }); }); @@ -217,22 +214,22 @@ describe('SentryBrowser', () => { describe('SentryBrowser initialization', () => { it('should use window.SENTRY_RELEASE to set release on initialization if available', () => { - global.SENTRY_RELEASE = { id: 'foobar' }; + (global as any).SENTRY_RELEASE = { id: 'foobar' }; init({ dsn }); - expect(global.__SENTRY__.hub._stack[0].client.getOptions().release).toBe('foobar'); - delete global.SENTRY_RELEASE; + expect((global as any).__SENTRY__.hub._stack[0].client.getOptions().release).toBe('foobar'); + delete (global as any).SENTRY_RELEASE; }); it('should use initialScope', () => { init({ dsn, initialScope: { tags: { a: 'b' } } }); - expect(global.__SENTRY__.hub._stack[0].scope._tags).toEqual({ a: 'b' }); + expect((global as any).__SENTRY__.hub._stack[0].scope._tags).toEqual({ a: 'b' }); }); it('should use initialScope Scope', () => { const scope = new Scope(); scope.setTags({ a: 'b' }); init({ dsn, initialScope: scope }); - expect(global.__SENTRY__.hub._stack[0].scope._tags).toEqual({ a: 'b' }); + expect((global as any).__SENTRY__.hub._stack[0].scope._tags).toEqual({ a: 'b' }); }); it('should use initialScope callback', () => { @@ -243,13 +240,13 @@ describe('SentryBrowser initialization', () => { return scope; }, }); - expect(global.__SENTRY__.hub._stack[0].scope._tags).toEqual({ a: 'b' }); + expect((global as any).__SENTRY__.hub._stack[0].scope._tags).toEqual({ a: 'b' }); }); it('should have initialization proceed as normal if window.SENTRY_RELEASE is not set', () => { // This is mostly a happy-path test to ensure that the initialization doesn't throw an error. init({ dsn }); - expect(global.__SENTRY__.hub._stack[0].client.getOptions().release).toBeUndefined(); + expect((global as any).__SENTRY__.hub._stack[0].client.getOptions().release).toBeUndefined(); }); describe('SDK metadata', () => { diff --git a/packages/browser/test/unit/sdk.test.ts b/packages/browser/test/unit/sdk.test.ts index 5d03d26d3312..cae32cc9a70f 100644 --- a/packages/browser/test/unit/sdk.test.ts +++ b/packages/browser/test/unit/sdk.test.ts @@ -7,8 +7,6 @@ import { resolvedSyncPromise } from '@sentry/utils'; import { BrowserOptions } from '../../src'; import { init } from '../../src/sdk'; -// eslint-disable-next-line no-var -declare var global: any; const PUBLIC_DSN = 'https://username@domain/123'; diff --git a/packages/core/test/lib/base.test.ts b/packages/core/test/lib/base.test.ts index beb4d412a2f7..6e941023df3c 100644 --- a/packages/core/test/lib/base.test.ts +++ b/packages/core/test/lib/base.test.ts @@ -8,8 +8,6 @@ import { TestIntegration } from '../mocks/integration'; import { makeFakeTransport } from '../mocks/transport'; const PUBLIC_DSN = 'https://username@domain/123'; -// eslint-disable-next-line no-var -declare var global: any; const clientEventFromException = jest.spyOn(TestClient.prototype, 'eventFromException'); const clientProcess = jest.spyOn(TestClient.prototype as any, '_process'); @@ -1181,7 +1179,7 @@ describe('BaseClient', () => { describe('integrations', () => { beforeEach(() => { - global.__SENTRY__ = {}; + (global as any).__SENTRY__ = {}; }); test('setup each one of them on setupIntegration call', () => { diff --git a/packages/core/test/lib/sdk.test.ts b/packages/core/test/lib/sdk.test.ts index 84dc75fbc3a2..215d7de09e2b 100644 --- a/packages/core/test/lib/sdk.test.ts +++ b/packages/core/test/lib/sdk.test.ts @@ -5,9 +5,6 @@ import { installedIntegrations } from '../../src/integration'; import { initAndBind } from '../../src/sdk'; import { getDefaultTestClientOptions, TestClient } from '../mocks/client'; -// eslint-disable-next-line no-var -declare var global: any; - const PUBLIC_DSN = 'https://username@domain/123'; jest.mock('@sentry/core', () => { @@ -45,7 +42,7 @@ export class MockIntegration implements Integration { describe('SDK', () => { beforeEach(() => { - global.__SENTRY__ = {}; + (global as any).__SENTRY__ = {}; installedIntegrations.splice(0); }); diff --git a/packages/hub/test/exports.test.ts b/packages/hub/test/exports.test.ts index a0fabf8061f6..fd2c20beea9b 100644 --- a/packages/hub/test/exports.test.ts +++ b/packages/hub/test/exports.test.ts @@ -35,12 +35,12 @@ export function init(options: Record): void { getCurrentHub().bindClient(new TestClient(options) as any); } -// eslint-disable-next-line no-var -declare var global: any; +// @ts-ignore global is defined +const globalObject: any = global; describe('Top Level API', () => { beforeEach(() => { - global.__SENTRY__ = { + globalObject.__SENTRY__ = { hub: undefined, }; }); @@ -141,7 +141,7 @@ describe('Top Level API', () => { configureScope((scope: Scope) => { scope.setUser({ id: '1234' }); }); - expect(global.__SENTRY__.hub._stack[1].scope._user).toEqual({ + expect(globalObject.__SENTRY__.hub._stack[1].scope._user).toEqual({ id: '1234', }); getCurrentHub().popScope(); @@ -154,7 +154,7 @@ describe('Top Level API', () => { configureScope((scope: Scope) => { scope.setExtra('id', '1234'); }); - expect(global.__SENTRY__.hub._stack[1].scope._extra).toEqual({ + expect(globalObject.__SENTRY__.hub._stack[1].scope._extra).toEqual({ id: '1234', }); getCurrentHub().popScope(); @@ -165,7 +165,7 @@ describe('Top Level API', () => { configureScope((scope: Scope) => { scope.setTag('id', '1234'); }); - expect(global.__SENTRY__.hub._stack[0].scope._tags).toEqual({ + expect(globalObject.__SENTRY__.hub._stack[0].scope._tags).toEqual({ id: '1234', }); }); @@ -177,7 +177,7 @@ describe('Top Level API', () => { configureScope((scope: Scope) => { scope.setFingerprint(['abcd']); }); - expect(global.__SENTRY__.hub._stack[1].scope._fingerprint).toEqual(['abcd']); + expect(globalObject.__SENTRY__.hub._stack[1].scope._fingerprint).toEqual(['abcd']); }); test('Level', () => { @@ -185,7 +185,7 @@ describe('Top Level API', () => { const scope = getCurrentHub().pushScope(); getCurrentHub().bindClient(client); scope.setLevel('warning'); - expect(global.__SENTRY__.hub._stack[1].scope._level).toEqual('warning'); + expect(globalObject.__SENTRY__.hub._stack[1].scope._level).toEqual('warning'); }); }); @@ -193,17 +193,17 @@ describe('Top Level API', () => { const client: any = new TestClient({}); getCurrentHub().withScope(() => { getCurrentHub().bindClient(client); - expect(global.__SENTRY__.hub._stack.length).toBe(2); + expect(globalObject.__SENTRY__.hub._stack.length).toBe(2); configureScope((scope: Scope) => { scope.setUser({ id: '1234' }); }); - expect(global.__SENTRY__.hub._stack[1].scope._user).toEqual({ + expect(globalObject.__SENTRY__.hub._stack[1].scope._user).toEqual({ id: '1234', }); configureScope((scope: Scope) => { scope.clear(); }); - expect(global.__SENTRY__.hub._stack[1].scope._user).toEqual({}); + expect(globalObject.__SENTRY__.hub._stack[1].scope._user).toEqual({}); }); }); @@ -260,54 +260,54 @@ describe('Top Level API', () => { scope2.setFingerprint(['2']); withScope(scope3 => { scope3.clear(); - expect(global.__SENTRY__.hub._stack[1].scope._level).toEqual('warning'); - expect(global.__SENTRY__.hub._stack[1].scope._fingerprint).toEqual(['1']); - expect(global.__SENTRY__.hub._stack[2].scope._level).toEqual('info'); - expect(global.__SENTRY__.hub._stack[2].scope._fingerprint).toEqual(['2']); - expect(global.__SENTRY__.hub._stack[3].scope._level).toBeUndefined(); + expect(globalObject.__SENTRY__.hub._stack[1].scope._level).toEqual('warning'); + expect(globalObject.__SENTRY__.hub._stack[1].scope._fingerprint).toEqual(['1']); + expect(globalObject.__SENTRY__.hub._stack[2].scope._level).toEqual('info'); + expect(globalObject.__SENTRY__.hub._stack[2].scope._fingerprint).toEqual(['2']); + expect(globalObject.__SENTRY__.hub._stack[3].scope._level).toBeUndefined(); }); - expect(global.__SENTRY__.hub._stack).toHaveLength(3); + expect(globalObject.__SENTRY__.hub._stack).toHaveLength(3); }); - expect(global.__SENTRY__.hub._stack).toHaveLength(2); + expect(globalObject.__SENTRY__.hub._stack).toHaveLength(2); }); - expect(global.__SENTRY__.hub._stack).toHaveLength(1); + expect(globalObject.__SENTRY__.hub._stack).toHaveLength(1); }); test('setExtras', () => { init({}); setExtras({ a: 'b' }); - expect(global.__SENTRY__.hub._stack[0].scope._extra).toEqual({ a: 'b' }); + expect(globalObject.__SENTRY__.hub._stack[0].scope._extra).toEqual({ a: 'b' }); }); test('setTags', () => { init({}); setTags({ a: 'b' }); - expect(global.__SENTRY__.hub._stack[0].scope._tags).toEqual({ a: 'b' }); + expect(globalObject.__SENTRY__.hub._stack[0].scope._tags).toEqual({ a: 'b' }); }); test('setExtra', () => { init({}); setExtra('a', 'b'); // prettier-ignore - expect(global.__SENTRY__.hub._stack[0].scope._extra).toEqual({ 'a': 'b' }); + expect(globalObject.__SENTRY__.hub._stack[0].scope._extra).toEqual({ 'a': 'b' }); }); test('setTag', () => { init({}); setTag('a', 'b'); // prettier-ignore - expect(global.__SENTRY__.hub._stack[0].scope._tags).toEqual({ 'a': 'b' }); + expect(globalObject.__SENTRY__.hub._stack[0].scope._tags).toEqual({ 'a': 'b' }); }); test('setUser', () => { init({}); setUser({ id: 'b' }); - expect(global.__SENTRY__.hub._stack[0].scope._user).toEqual({ id: 'b' }); + expect(globalObject.__SENTRY__.hub._stack[0].scope._user).toEqual({ id: 'b' }); }); test('setContext', () => { init({}); setContext('test', { id: 'b' }); - expect(global.__SENTRY__.hub._stack[0].scope._contexts).toEqual({ test: { id: 'b' } }); + expect(globalObject.__SENTRY__.hub._stack[0].scope._contexts).toEqual({ test: { id: 'b' } }); }); }); diff --git a/packages/node/test/index.test.ts b/packages/node/test/index.test.ts index 7e5fc929beea..5ac0cfccd908 100644 --- a/packages/node/test/index.test.ts +++ b/packages/node/test/index.test.ts @@ -28,9 +28,6 @@ jest.mock('@sentry/core', () => { const dsn = 'https://53039209a22b4ec1bcc296a3c9fdecd6@sentry.io/4291'; -// eslint-disable-next-line no-var -declare var global: any; - describe('SentryNode', () => { beforeAll(() => { init({ dsn }); @@ -50,7 +47,7 @@ describe('SentryNode', () => { configureScope((scope: Scope) => { scope.setExtra('abc', { def: [1] }); }); - expect(global.__SENTRY__.hub._stack[1].scope._extra).toEqual({ + expect((global as any).__SENTRY__.hub._stack[1].scope._extra).toEqual({ abc: { def: [1] }, }); }); @@ -59,7 +56,7 @@ describe('SentryNode', () => { configureScope((scope: Scope) => { scope.setTag('abc', 'def'); }); - expect(global.__SENTRY__.hub._stack[1].scope._tags).toEqual({ + expect((global as any).__SENTRY__.hub._stack[1].scope._tags).toEqual({ abc: 'def', }); }); @@ -68,7 +65,7 @@ describe('SentryNode', () => { configureScope((scope: Scope) => { scope.setUser({ id: 'def' }); }); - expect(global.__SENTRY__.hub._stack[1].scope._user).toEqual({ + expect((global as any).__SENTRY__.hub._stack[1].scope._user).toEqual({ id: 'def', }); }); @@ -351,11 +348,11 @@ describe('SentryNode initialization', () => { }); test('global.SENTRY_RELEASE is used to set release on initialization if available', () => { - global.SENTRY_RELEASE = { id: 'foobar' }; + (global as any).SENTRY_RELEASE = { id: 'foobar' }; init({ dsn }); - expect(global.__SENTRY__.hub._stack[0].client.getOptions().release).toEqual('foobar'); + expect((global as any).__SENTRY__.hub._stack[0].client.getOptions().release).toEqual('foobar'); // Unsure if this is needed under jest. - global.SENTRY_RELEASE = undefined; + (global as any).SENTRY_RELEASE = undefined; }); describe('SDK metadata', () => { diff --git a/packages/node/test/sdk.test.ts b/packages/node/test/sdk.test.ts index 48e5accee439..8b31c1136914 100644 --- a/packages/node/test/sdk.test.ts +++ b/packages/node/test/sdk.test.ts @@ -3,9 +3,6 @@ import { Integration } from '@sentry/types'; import { init } from '../src/sdk'; import * as sdk from '../src/sdk'; -// eslint-disable-next-line no-var -declare var global: any; - const PUBLIC_DSN = 'https://username@domain/123'; class MockIntegration implements Integration { @@ -20,7 +17,7 @@ const defaultIntegrationsBackup = sdk.defaultIntegrations; describe('init()', () => { beforeEach(() => { - global.__SENTRY__ = {}; + (global as any).__SENTRY__ = {}; }); afterEach(() => { From 6e44bad5bc8e1e9e01de2ae5bfad3ed41826eb14 Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Mon, 17 Oct 2022 14:39:45 +0000 Subject: [PATCH 9/9] Revert unnecessary test changes --- packages/browser/test/unit/index.test.ts | 23 ++++++----- packages/browser/test/unit/sdk.test.ts | 2 + packages/core/test/lib/base.test.ts | 4 +- packages/core/test/lib/sdk.test.ts | 5 ++- packages/hub/test/exports.test.ts | 50 ++++++++++++------------ packages/node/test/index.test.ts | 15 ++++--- packages/node/test/sdk.test.ts | 5 ++- 7 files changed, 60 insertions(+), 44 deletions(-) diff --git a/packages/browser/test/unit/index.test.ts b/packages/browser/test/unit/index.test.ts index d3e856a8d7f7..37fa972512a1 100644 --- a/packages/browser/test/unit/index.test.ts +++ b/packages/browser/test/unit/index.test.ts @@ -21,6 +21,9 @@ import { makeSimpleTransport } from './mocks/simpletransport'; const dsn = 'https://53039209a22b4ec1bcc296a3c9fdecd6@sentry.io/4291'; +// eslint-disable-next-line no-var +declare var global: any; + jest.mock('@sentry/core', () => { const original = jest.requireActual('@sentry/core'); return { @@ -54,7 +57,7 @@ describe('SentryBrowser', () => { configureScope((scope: Scope) => { scope.setExtra('abc', { def: [1] }); }); - expect((global as any).__SENTRY__.hub._stack[1].scope._extra).toEqual({ + expect(global.__SENTRY__.hub._stack[1].scope._extra).toEqual({ abc: { def: [1] }, }); }); @@ -63,7 +66,7 @@ describe('SentryBrowser', () => { configureScope((scope: Scope) => { scope.setTag('abc', 'def'); }); - expect((global as any).__SENTRY__.hub._stack[1].scope._tags).toEqual({ + expect(global.__SENTRY__.hub._stack[1].scope._tags).toEqual({ abc: 'def', }); }); @@ -72,7 +75,7 @@ describe('SentryBrowser', () => { configureScope((scope: Scope) => { scope.setUser({ id: 'def' }); }); - expect((global as any).__SENTRY__.hub._stack[1].scope._user).toEqual({ + expect(global.__SENTRY__.hub._stack[1].scope._user).toEqual({ id: 'def', }); }); @@ -214,22 +217,22 @@ describe('SentryBrowser', () => { describe('SentryBrowser initialization', () => { it('should use window.SENTRY_RELEASE to set release on initialization if available', () => { - (global as any).SENTRY_RELEASE = { id: 'foobar' }; + global.SENTRY_RELEASE = { id: 'foobar' }; init({ dsn }); - expect((global as any).__SENTRY__.hub._stack[0].client.getOptions().release).toBe('foobar'); - delete (global as any).SENTRY_RELEASE; + expect(global.__SENTRY__.hub._stack[0].client.getOptions().release).toBe('foobar'); + delete global.SENTRY_RELEASE; }); it('should use initialScope', () => { init({ dsn, initialScope: { tags: { a: 'b' } } }); - expect((global as any).__SENTRY__.hub._stack[0].scope._tags).toEqual({ a: 'b' }); + expect(global.__SENTRY__.hub._stack[0].scope._tags).toEqual({ a: 'b' }); }); it('should use initialScope Scope', () => { const scope = new Scope(); scope.setTags({ a: 'b' }); init({ dsn, initialScope: scope }); - expect((global as any).__SENTRY__.hub._stack[0].scope._tags).toEqual({ a: 'b' }); + expect(global.__SENTRY__.hub._stack[0].scope._tags).toEqual({ a: 'b' }); }); it('should use initialScope callback', () => { @@ -240,13 +243,13 @@ describe('SentryBrowser initialization', () => { return scope; }, }); - expect((global as any).__SENTRY__.hub._stack[0].scope._tags).toEqual({ a: 'b' }); + expect(global.__SENTRY__.hub._stack[0].scope._tags).toEqual({ a: 'b' }); }); it('should have initialization proceed as normal if window.SENTRY_RELEASE is not set', () => { // This is mostly a happy-path test to ensure that the initialization doesn't throw an error. init({ dsn }); - expect((global as any).__SENTRY__.hub._stack[0].client.getOptions().release).toBeUndefined(); + expect(global.__SENTRY__.hub._stack[0].client.getOptions().release).toBeUndefined(); }); describe('SDK metadata', () => { diff --git a/packages/browser/test/unit/sdk.test.ts b/packages/browser/test/unit/sdk.test.ts index cae32cc9a70f..5d03d26d3312 100644 --- a/packages/browser/test/unit/sdk.test.ts +++ b/packages/browser/test/unit/sdk.test.ts @@ -7,6 +7,8 @@ import { resolvedSyncPromise } from '@sentry/utils'; import { BrowserOptions } from '../../src'; import { init } from '../../src/sdk'; +// eslint-disable-next-line no-var +declare var global: any; const PUBLIC_DSN = 'https://username@domain/123'; diff --git a/packages/core/test/lib/base.test.ts b/packages/core/test/lib/base.test.ts index 6e941023df3c..beb4d412a2f7 100644 --- a/packages/core/test/lib/base.test.ts +++ b/packages/core/test/lib/base.test.ts @@ -8,6 +8,8 @@ import { TestIntegration } from '../mocks/integration'; import { makeFakeTransport } from '../mocks/transport'; const PUBLIC_DSN = 'https://username@domain/123'; +// eslint-disable-next-line no-var +declare var global: any; const clientEventFromException = jest.spyOn(TestClient.prototype, 'eventFromException'); const clientProcess = jest.spyOn(TestClient.prototype as any, '_process'); @@ -1179,7 +1181,7 @@ describe('BaseClient', () => { describe('integrations', () => { beforeEach(() => { - (global as any).__SENTRY__ = {}; + global.__SENTRY__ = {}; }); test('setup each one of them on setupIntegration call', () => { diff --git a/packages/core/test/lib/sdk.test.ts b/packages/core/test/lib/sdk.test.ts index 215d7de09e2b..84dc75fbc3a2 100644 --- a/packages/core/test/lib/sdk.test.ts +++ b/packages/core/test/lib/sdk.test.ts @@ -5,6 +5,9 @@ import { installedIntegrations } from '../../src/integration'; import { initAndBind } from '../../src/sdk'; import { getDefaultTestClientOptions, TestClient } from '../mocks/client'; +// eslint-disable-next-line no-var +declare var global: any; + const PUBLIC_DSN = 'https://username@domain/123'; jest.mock('@sentry/core', () => { @@ -42,7 +45,7 @@ export class MockIntegration implements Integration { describe('SDK', () => { beforeEach(() => { - (global as any).__SENTRY__ = {}; + global.__SENTRY__ = {}; installedIntegrations.splice(0); }); diff --git a/packages/hub/test/exports.test.ts b/packages/hub/test/exports.test.ts index fd2c20beea9b..a0fabf8061f6 100644 --- a/packages/hub/test/exports.test.ts +++ b/packages/hub/test/exports.test.ts @@ -35,12 +35,12 @@ export function init(options: Record): void { getCurrentHub().bindClient(new TestClient(options) as any); } -// @ts-ignore global is defined -const globalObject: any = global; +// eslint-disable-next-line no-var +declare var global: any; describe('Top Level API', () => { beforeEach(() => { - globalObject.__SENTRY__ = { + global.__SENTRY__ = { hub: undefined, }; }); @@ -141,7 +141,7 @@ describe('Top Level API', () => { configureScope((scope: Scope) => { scope.setUser({ id: '1234' }); }); - expect(globalObject.__SENTRY__.hub._stack[1].scope._user).toEqual({ + expect(global.__SENTRY__.hub._stack[1].scope._user).toEqual({ id: '1234', }); getCurrentHub().popScope(); @@ -154,7 +154,7 @@ describe('Top Level API', () => { configureScope((scope: Scope) => { scope.setExtra('id', '1234'); }); - expect(globalObject.__SENTRY__.hub._stack[1].scope._extra).toEqual({ + expect(global.__SENTRY__.hub._stack[1].scope._extra).toEqual({ id: '1234', }); getCurrentHub().popScope(); @@ -165,7 +165,7 @@ describe('Top Level API', () => { configureScope((scope: Scope) => { scope.setTag('id', '1234'); }); - expect(globalObject.__SENTRY__.hub._stack[0].scope._tags).toEqual({ + expect(global.__SENTRY__.hub._stack[0].scope._tags).toEqual({ id: '1234', }); }); @@ -177,7 +177,7 @@ describe('Top Level API', () => { configureScope((scope: Scope) => { scope.setFingerprint(['abcd']); }); - expect(globalObject.__SENTRY__.hub._stack[1].scope._fingerprint).toEqual(['abcd']); + expect(global.__SENTRY__.hub._stack[1].scope._fingerprint).toEqual(['abcd']); }); test('Level', () => { @@ -185,7 +185,7 @@ describe('Top Level API', () => { const scope = getCurrentHub().pushScope(); getCurrentHub().bindClient(client); scope.setLevel('warning'); - expect(globalObject.__SENTRY__.hub._stack[1].scope._level).toEqual('warning'); + expect(global.__SENTRY__.hub._stack[1].scope._level).toEqual('warning'); }); }); @@ -193,17 +193,17 @@ describe('Top Level API', () => { const client: any = new TestClient({}); getCurrentHub().withScope(() => { getCurrentHub().bindClient(client); - expect(globalObject.__SENTRY__.hub._stack.length).toBe(2); + expect(global.__SENTRY__.hub._stack.length).toBe(2); configureScope((scope: Scope) => { scope.setUser({ id: '1234' }); }); - expect(globalObject.__SENTRY__.hub._stack[1].scope._user).toEqual({ + expect(global.__SENTRY__.hub._stack[1].scope._user).toEqual({ id: '1234', }); configureScope((scope: Scope) => { scope.clear(); }); - expect(globalObject.__SENTRY__.hub._stack[1].scope._user).toEqual({}); + expect(global.__SENTRY__.hub._stack[1].scope._user).toEqual({}); }); }); @@ -260,54 +260,54 @@ describe('Top Level API', () => { scope2.setFingerprint(['2']); withScope(scope3 => { scope3.clear(); - expect(globalObject.__SENTRY__.hub._stack[1].scope._level).toEqual('warning'); - expect(globalObject.__SENTRY__.hub._stack[1].scope._fingerprint).toEqual(['1']); - expect(globalObject.__SENTRY__.hub._stack[2].scope._level).toEqual('info'); - expect(globalObject.__SENTRY__.hub._stack[2].scope._fingerprint).toEqual(['2']); - expect(globalObject.__SENTRY__.hub._stack[3].scope._level).toBeUndefined(); + expect(global.__SENTRY__.hub._stack[1].scope._level).toEqual('warning'); + expect(global.__SENTRY__.hub._stack[1].scope._fingerprint).toEqual(['1']); + expect(global.__SENTRY__.hub._stack[2].scope._level).toEqual('info'); + expect(global.__SENTRY__.hub._stack[2].scope._fingerprint).toEqual(['2']); + expect(global.__SENTRY__.hub._stack[3].scope._level).toBeUndefined(); }); - expect(globalObject.__SENTRY__.hub._stack).toHaveLength(3); + expect(global.__SENTRY__.hub._stack).toHaveLength(3); }); - expect(globalObject.__SENTRY__.hub._stack).toHaveLength(2); + expect(global.__SENTRY__.hub._stack).toHaveLength(2); }); - expect(globalObject.__SENTRY__.hub._stack).toHaveLength(1); + expect(global.__SENTRY__.hub._stack).toHaveLength(1); }); test('setExtras', () => { init({}); setExtras({ a: 'b' }); - expect(globalObject.__SENTRY__.hub._stack[0].scope._extra).toEqual({ a: 'b' }); + expect(global.__SENTRY__.hub._stack[0].scope._extra).toEqual({ a: 'b' }); }); test('setTags', () => { init({}); setTags({ a: 'b' }); - expect(globalObject.__SENTRY__.hub._stack[0].scope._tags).toEqual({ a: 'b' }); + expect(global.__SENTRY__.hub._stack[0].scope._tags).toEqual({ a: 'b' }); }); test('setExtra', () => { init({}); setExtra('a', 'b'); // prettier-ignore - expect(globalObject.__SENTRY__.hub._stack[0].scope._extra).toEqual({ 'a': 'b' }); + expect(global.__SENTRY__.hub._stack[0].scope._extra).toEqual({ 'a': 'b' }); }); test('setTag', () => { init({}); setTag('a', 'b'); // prettier-ignore - expect(globalObject.__SENTRY__.hub._stack[0].scope._tags).toEqual({ 'a': 'b' }); + expect(global.__SENTRY__.hub._stack[0].scope._tags).toEqual({ 'a': 'b' }); }); test('setUser', () => { init({}); setUser({ id: 'b' }); - expect(globalObject.__SENTRY__.hub._stack[0].scope._user).toEqual({ id: 'b' }); + expect(global.__SENTRY__.hub._stack[0].scope._user).toEqual({ id: 'b' }); }); test('setContext', () => { init({}); setContext('test', { id: 'b' }); - expect(globalObject.__SENTRY__.hub._stack[0].scope._contexts).toEqual({ test: { id: 'b' } }); + expect(global.__SENTRY__.hub._stack[0].scope._contexts).toEqual({ test: { id: 'b' } }); }); }); diff --git a/packages/node/test/index.test.ts b/packages/node/test/index.test.ts index 5ac0cfccd908..7e5fc929beea 100644 --- a/packages/node/test/index.test.ts +++ b/packages/node/test/index.test.ts @@ -28,6 +28,9 @@ jest.mock('@sentry/core', () => { const dsn = 'https://53039209a22b4ec1bcc296a3c9fdecd6@sentry.io/4291'; +// eslint-disable-next-line no-var +declare var global: any; + describe('SentryNode', () => { beforeAll(() => { init({ dsn }); @@ -47,7 +50,7 @@ describe('SentryNode', () => { configureScope((scope: Scope) => { scope.setExtra('abc', { def: [1] }); }); - expect((global as any).__SENTRY__.hub._stack[1].scope._extra).toEqual({ + expect(global.__SENTRY__.hub._stack[1].scope._extra).toEqual({ abc: { def: [1] }, }); }); @@ -56,7 +59,7 @@ describe('SentryNode', () => { configureScope((scope: Scope) => { scope.setTag('abc', 'def'); }); - expect((global as any).__SENTRY__.hub._stack[1].scope._tags).toEqual({ + expect(global.__SENTRY__.hub._stack[1].scope._tags).toEqual({ abc: 'def', }); }); @@ -65,7 +68,7 @@ describe('SentryNode', () => { configureScope((scope: Scope) => { scope.setUser({ id: 'def' }); }); - expect((global as any).__SENTRY__.hub._stack[1].scope._user).toEqual({ + expect(global.__SENTRY__.hub._stack[1].scope._user).toEqual({ id: 'def', }); }); @@ -348,11 +351,11 @@ describe('SentryNode initialization', () => { }); test('global.SENTRY_RELEASE is used to set release on initialization if available', () => { - (global as any).SENTRY_RELEASE = { id: 'foobar' }; + global.SENTRY_RELEASE = { id: 'foobar' }; init({ dsn }); - expect((global as any).__SENTRY__.hub._stack[0].client.getOptions().release).toEqual('foobar'); + expect(global.__SENTRY__.hub._stack[0].client.getOptions().release).toEqual('foobar'); // Unsure if this is needed under jest. - (global as any).SENTRY_RELEASE = undefined; + global.SENTRY_RELEASE = undefined; }); describe('SDK metadata', () => { diff --git a/packages/node/test/sdk.test.ts b/packages/node/test/sdk.test.ts index 8b31c1136914..48e5accee439 100644 --- a/packages/node/test/sdk.test.ts +++ b/packages/node/test/sdk.test.ts @@ -3,6 +3,9 @@ import { Integration } from '@sentry/types'; import { init } from '../src/sdk'; import * as sdk from '../src/sdk'; +// eslint-disable-next-line no-var +declare var global: any; + const PUBLIC_DSN = 'https://username@domain/123'; class MockIntegration implements Integration { @@ -17,7 +20,7 @@ const defaultIntegrationsBackup = sdk.defaultIntegrations; describe('init()', () => { beforeEach(() => { - (global as any).__SENTRY__ = {}; + global.__SENTRY__ = {}; }); afterEach(() => {