From 2b0d67b61307afaabf97880c4228749c4c675d34 Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Tue, 5 Dec 2023 11:44:26 +0100 Subject: [PATCH 1/2] fix(node): Guard `process.env.NODE_ENV` access in Spotlight integration --- packages/node/src/integrations/spotlight.ts | 4 +-- .../node/test/integrations/spotlight.test.ts | 36 +++++++++++++++++-- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/packages/node/src/integrations/spotlight.ts b/packages/node/src/integrations/spotlight.ts index cef0c27e2a4a..55fabc284cad 100644 --- a/packages/node/src/integrations/spotlight.ts +++ b/packages/node/src/integrations/spotlight.ts @@ -41,8 +41,8 @@ export class Spotlight implements Integration { * Sets up forwarding envelopes to the Spotlight Sidecar */ public setup(client: Client): void { - if (process.env.NODE_ENV !== 'development') { - logger.warn("[Spotlight] It seems you're not in dev mode. Do you really want to have Spoltight enabled?"); + if (typeof process === 'object' && process.env && process.env.NODE_ENV !== 'development') { + logger.warn("[Spotlight] It seems you're not in dev mode. Do you really want to have Spotlight enabled?"); } connectToSpotlight(client, this._options); } diff --git a/packages/node/test/integrations/spotlight.test.ts b/packages/node/test/integrations/spotlight.test.ts index 71af071cbf3c..890cb029ec43 100644 --- a/packages/node/test/integrations/spotlight.test.ts +++ b/packages/node/test/integrations/spotlight.test.ts @@ -138,7 +138,7 @@ describe('Spotlight', () => { integration.setup(client); expect(loggerSpy).toHaveBeenCalledWith( - expect.stringContaining("It seems you're not in dev mode. Do you really want to have Spoltight enabled?"), + expect.stringContaining("It seems you're not in dev mode. Do you really want to have Spotlight enabled?"), ); process.env.NODE_ENV = oldEnvValue; @@ -152,9 +152,41 @@ describe('Spotlight', () => { integration.setup(client); expect(loggerSpy).not.toHaveBeenCalledWith( - expect.stringContaining("It seems you're not in dev mode. Do you really want to have Spoltight enabled?"), + expect.stringContaining("It seems you're not in dev mode. Do you really want to have Spotlight enabled?"), ); process.env.NODE_ENV = oldEnvValue; }); + + it('handles `process` not being available', () => { + const originalProcess = process; + + // @ts-expect-error - TS complains but we explicitly wanna test this + delete globalThis.process; + + const integration = new Spotlight({ sidecarUrl: 'http://localhost:8969' }); + integration.setup(client); + + expect(loggerSpy).not.toHaveBeenCalledWith( + expect.stringContaining("It seems you're not in dev mode. Do you really want to have Spotlight enabled?"), + ); + + globalThis.process = originalProcess; + }); + + it('handles `process.env` not being available', () => { + const originalEnv = process.env; + + // @ts-expect-error - TS complains but we explicitly wanna test this + delete process.env; + + const integration = new Spotlight({ sidecarUrl: 'http://localhost:8969' }); + integration.setup(client); + + expect(loggerSpy).not.toHaveBeenCalledWith( + expect.stringContaining("It seems you're not in dev mode. Do you really want to have Spotlight enabled?"), + ); + + process.env = originalEnv; + }); }); From 970b522b74529cf1ef2137baf2df453bc02c64eb Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Tue, 5 Dec 2023 14:47:14 +0100 Subject: [PATCH 2/2] fix tests pls --- packages/node/test/integrations/spotlight.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/node/test/integrations/spotlight.test.ts b/packages/node/test/integrations/spotlight.test.ts index 890cb029ec43..266d64b5710a 100644 --- a/packages/node/test/integrations/spotlight.test.ts +++ b/packages/node/test/integrations/spotlight.test.ts @@ -162,7 +162,7 @@ describe('Spotlight', () => { const originalProcess = process; // @ts-expect-error - TS complains but we explicitly wanna test this - delete globalThis.process; + delete global.process; const integration = new Spotlight({ sidecarUrl: 'http://localhost:8969' }); integration.setup(client); @@ -171,7 +171,7 @@ describe('Spotlight', () => { expect.stringContaining("It seems you're not in dev mode. Do you really want to have Spotlight enabled?"), ); - globalThis.process = originalProcess; + global.process = originalProcess; }); it('handles `process.env` not being available', () => {