Skip to content

Commit

Permalink
fix(node): Guard process.env.NODE_ENV access in Spotlight integrati…
Browse files Browse the repository at this point in the history
…on (#9748)
  • Loading branch information
Lms24 committed Dec 5, 2023
1 parent 4044447 commit 0f117e7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
4 changes: 2 additions & 2 deletions packages/node/src/integrations/spotlight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
36 changes: 34 additions & 2 deletions packages/node/test/integrations/spotlight.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 global.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?"),
);

global.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;
});
});

0 comments on commit 0f117e7

Please sign in to comment.