diff --git a/packages/internal/metrics/src/utils/checkEnv.ts b/packages/internal/metrics/src/utils/checkEnv.ts index 7421abae62..25ed31755a 100644 --- a/packages/internal/metrics/src/utils/checkEnv.ts +++ b/packages/internal/metrics/src/utils/checkEnv.ts @@ -1,4 +1,11 @@ -export function isTestEnvironment() { +import { isBrowser } from './browser'; +import { errorBoundary } from './errorBoundary'; + +function isTestEnvironmentFn() { + if (isBrowser()) { + return false; + } + if (typeof process === 'undefined') { return false; } @@ -7,3 +14,5 @@ export function isTestEnvironment() { // Just use process.env.CI for now. return process.env.JEST_WORKER_ID !== undefined; } + +export const isTestEnvironment = errorBoundary(isTestEnvironmentFn, false); diff --git a/packages/internal/metrics/src/utils/errorBoundary.test.ts b/packages/internal/metrics/src/utils/errorBoundary.test.ts index 32bd23a85b..b1000d45f5 100644 --- a/packages/internal/metrics/src/utils/errorBoundary.test.ts +++ b/packages/internal/metrics/src/utils/errorBoundary.test.ts @@ -26,4 +26,16 @@ describe('errorBoundary', () => { }; expect(errorBoundary(testFn)).not.toThrowError(); }); + it('should return the fallback result for a function that throws', () => { + const testFn = (): number => { + throw new Error('test'); + }; + expect(errorBoundary(testFn, 3)()).toEqual(3); + }); + it('should return the fallback result for an async function that throws', () => { + const testFn = async (): Promise => { + throw new Error('test'); + }; + expect(errorBoundary(testFn, Promise.resolve(3))()).resolves.toEqual(3); + }); }); diff --git a/packages/internal/metrics/src/utils/errorBoundary.ts b/packages/internal/metrics/src/utils/errorBoundary.ts index baf8c9c973..80859dbd77 100644 --- a/packages/internal/metrics/src/utils/errorBoundary.ts +++ b/packages/internal/metrics/src/utils/errorBoundary.ts @@ -1,4 +1,8 @@ -export function errorBoundary any>(fn: T): T { +export function errorBoundary any>( + fn: T, + fallbackResult?: ReturnType, +): T { const wrappedFunction = ((...args: Parameters): ReturnType => { try { // Execute the original function @@ -7,13 +11,13 @@ export function errorBoundary any>(fn: T): T { if (result instanceof Promise) { // Silent fail for now, in future // we can send errors to a logging service - return result.catch(() => undefined) as ReturnType; + return result.catch(() => fallbackResult) as ReturnType; } return result; } catch (error) { // As above, fail silently for now - return undefined as ReturnType; + return fallbackResult as ReturnType; } }) as T; diff --git a/packages/internal/metrics/src/utils/request.ts b/packages/internal/metrics/src/utils/request.ts index 8c3e376d67..dec1cf89f2 100644 --- a/packages/internal/metrics/src/utils/request.ts +++ b/packages/internal/metrics/src/utils/request.ts @@ -1,6 +1,6 @@ import axios from 'axios'; -const IMTBL_API = 'https://api.x.immutable.com'; +const IMTBL_API = 'https://api.immutable.com'; export async function post(path: string, data: any) { const client = axios.create({ diff --git a/packages/passport/sdk/package.json b/packages/passport/sdk/package.json index ef6f91e20f..f94cdcb1b5 100644 --- a/packages/passport/sdk/package.json +++ b/packages/passport/sdk/package.json @@ -31,7 +31,6 @@ "@types/axios": "^0.14.0", "@types/jest": "^29.4.3", "@types/jwt-encode": "^1.0.1", - "@types/node": "^18.14.2", "@types/react": "^18.0.28", "@types/react-dom": "^18.0.11", "@typescript-eslint/eslint-plugin": "^5.57.1", diff --git a/packages/passport/sdk/src/utils/logger.ts b/packages/passport/sdk/src/utils/logger.ts index 4856f881b5..f812440c99 100644 --- a/packages/passport/sdk/src/utils/logger.ts +++ b/packages/passport/sdk/src/utils/logger.ts @@ -1,6 +1,9 @@ -const shouldLog: boolean = process?.env?.JEST_WORKER_ID === undefined; - const warn = (...args: any[]) => { + if (typeof process === 'undefined') { + return; + } + + const shouldLog: boolean = process?.env?.JEST_WORKER_ID === undefined; if (shouldLog) { // eslint-disable-next-line no-console console.warn(...args); diff --git a/sdk/rollup.config.js b/sdk/rollup.config.js index 9d36fa9aed..a513fa306a 100644 --- a/sdk/rollup.config.js +++ b/sdk/rollup.config.js @@ -132,7 +132,8 @@ export default [ __SDK_VERSION__: pkg.version, // This breaks in a dex dependency, so manually replacing it. - 'process.env.NODE_ENV': '"production"' + 'process.env.NODE_ENV': '"production"', + 'process': 'undefined' }), terser(), ], diff --git a/yarn.lock b/yarn.lock index d0d7d0069e..62513cb019 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3719,7 +3719,6 @@ __metadata: "@types/axios": ^0.14.0 "@types/jest": ^29.4.3 "@types/jwt-encode": ^1.0.1 - "@types/node": ^18.14.2 "@types/react": ^18.0.28 "@types/react-dom": ^18.0.11 "@typescript-eslint/eslint-plugin": ^5.57.1