Skip to content

Commit

Permalink
fix(node): Guard process.env.NODE_ENV access in Spotlight integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Lms24 committed Dec 5, 2023
1 parent 8744898 commit b127120
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 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
38 changes: 35 additions & 3 deletions packages/node/test/integrations/spotlight.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as http from 'http';
import type { Envelope, EventEnvelope } from '@sentry/types';
import { createEnvelope, logger } from '@sentry/utils';
import { GLOBAL_OBJ, createEnvelope, logger } from '@sentry/utils';

import { NodeClient } from '../../src';
import { Spotlight } from '../../src/integrations';
Expand Down 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_OBJ.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;
});
});

0 comments on commit b127120

Please sign in to comment.