Skip to content

Commit 9971db8

Browse files
authored
fix(nuxt): Respect configured environment settings (#19243)
Closes #19238
1 parent 75e0bb6 commit 9971db8

File tree

4 files changed

+132
-4
lines changed

4 files changed

+132
-4
lines changed

packages/nuxt/src/client/sdk.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ import type { SentryNuxtClientOptions } from '../common/types';
99
* @param options Configuration options for the SDK.
1010
*/
1111
export function init(options: SentryNuxtClientOptions): Client | undefined {
12+
const envFallback = import.meta.dev ? DEV_ENVIRONMENT : DEFAULT_ENVIRONMENT;
1213
const sentryOptions = {
1314
/* BrowserTracing is added later with the Nuxt client plugin */
1415
defaultIntegrations: [...getBrowserDefaultIntegrations(options)],
15-
environment: import.meta.dev ? DEV_ENVIRONMENT : DEFAULT_ENVIRONMENT,
16+
environment: options.environment ?? process.env.SENTRY_ENVIRONMENT ?? envFallback,
1617
...options,
1718
};
1819

packages/nuxt/src/server/sdk.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ import type { SentryNuxtServerOptions } from '../common/types';
2525
* @param options Configuration options for the SDK.
2626
*/
2727
export function init(options: SentryNuxtServerOptions): Client | undefined {
28+
const envFallback = !isCjs() && import.meta.dev ? DEV_ENVIRONMENT : DEFAULT_ENVIRONMENT;
2829
const sentryOptions = {
29-
environment: !isCjs() && import.meta.dev ? DEV_ENVIRONMENT : DEFAULT_ENVIRONMENT,
30+
environment: options.environment ?? process.env.SENTRY_ENVIRONMENT ?? envFallback,
3031
defaultIntegrations: getNuxtDefaultIntegrations(options),
3132
...options,
3233
};

packages/nuxt/test/client/sdk.test.ts

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as SentryBrowser from '@sentry/browser';
22
import { SDK_VERSION } from '@sentry/vue';
3-
import { beforeEach, describe, expect, it, vi } from 'vitest';
3+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
44
import { init } from '../../src/client';
55

66
const browserInit = vi.spyOn(SentryBrowser, 'init');
@@ -77,5 +77,68 @@ describe('Nuxt Client SDK', () => {
7777
expect(callArgs).toBeDefined();
7878
expect(callArgs?.defaultIntegrations).toBe(false);
7979
});
80+
81+
describe('environment option', () => {
82+
const originalEnv = process.env.SENTRY_ENVIRONMENT;
83+
84+
beforeEach(() => {
85+
delete process.env.SENTRY_ENVIRONMENT;
86+
});
87+
88+
afterEach(() => {
89+
if (originalEnv !== undefined) {
90+
process.env.SENTRY_ENVIRONMENT = originalEnv;
91+
} else {
92+
delete process.env.SENTRY_ENVIRONMENT;
93+
}
94+
});
95+
96+
it('uses environment from options when provided', () => {
97+
init({
98+
dsn: 'https://public@dsn.ingest.sentry.io/1337',
99+
environment: 'custom-env',
100+
});
101+
102+
expect(browserInit).toHaveBeenCalledTimes(1);
103+
const callArgs = browserInit.mock.calls[0]?.[0];
104+
expect(callArgs?.environment).toBe('custom-env');
105+
});
106+
107+
it('uses SENTRY_ENVIRONMENT env var when options.environment is not provided', () => {
108+
process.env.SENTRY_ENVIRONMENT = 'env-from-variable';
109+
110+
init({
111+
dsn: 'https://public@dsn.ingest.sentry.io/1337',
112+
});
113+
114+
expect(browserInit).toHaveBeenCalledTimes(1);
115+
const callArgs = browserInit.mock.calls[0]?.[0];
116+
expect(callArgs?.environment).toBe('env-from-variable');
117+
});
118+
119+
it('uses fallback environment when neither options.environment nor SENTRY_ENVIRONMENT is provided', () => {
120+
init({
121+
dsn: 'https://public@dsn.ingest.sentry.io/1337',
122+
});
123+
124+
expect(browserInit).toHaveBeenCalledTimes(1);
125+
const callArgs = browserInit.mock.calls[0]?.[0];
126+
// In test environment, import.meta.dev should be checked, but we can just verify it's set
127+
expect(callArgs?.environment).toBeDefined();
128+
});
129+
130+
it('prioritizes options.environment over SENTRY_ENVIRONMENT env var', () => {
131+
process.env.SENTRY_ENVIRONMENT = 'env-from-variable';
132+
133+
init({
134+
dsn: 'https://public@dsn.ingest.sentry.io/1337',
135+
environment: 'options-env',
136+
});
137+
138+
expect(browserInit).toHaveBeenCalledTimes(1);
139+
const callArgs = browserInit.mock.calls[0]?.[0];
140+
expect(callArgs?.environment).toBe('options-env');
141+
});
142+
});
80143
});
81144
});

packages/nuxt/test/server/sdk.test.ts

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { Event, EventProcessor } from '@sentry/core';
22
import * as SentryNode from '@sentry/node';
33
import { getGlobalScope, Scope, SDK_VERSION } from '@sentry/node';
4-
import { beforeEach, describe, expect, it, vi } from 'vitest';
4+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
55
import { init } from '../../src/server';
66
import { clientSourceMapErrorFilter, lowQualityTransactionsFilter } from '../../src/server/sdk';
77

@@ -77,6 +77,69 @@ describe('Nuxt Server SDK', () => {
7777
expect(callArgs?.defaultIntegrations).toBe(false);
7878
});
7979

80+
describe('environment option', () => {
81+
const originalEnv = process.env.SENTRY_ENVIRONMENT;
82+
83+
beforeEach(() => {
84+
delete process.env.SENTRY_ENVIRONMENT;
85+
});
86+
87+
afterEach(() => {
88+
if (originalEnv !== undefined) {
89+
process.env.SENTRY_ENVIRONMENT = originalEnv;
90+
} else {
91+
delete process.env.SENTRY_ENVIRONMENT;
92+
}
93+
});
94+
95+
it('uses environment from options when provided', () => {
96+
init({
97+
dsn: 'https://public@dsn.ingest.sentry.io/1337',
98+
environment: 'custom-env',
99+
});
100+
101+
expect(nodeInit).toHaveBeenCalledTimes(1);
102+
const callArgs = nodeInit.mock.calls[0]?.[0];
103+
expect(callArgs?.environment).toBe('custom-env');
104+
});
105+
106+
it('uses SENTRY_ENVIRONMENT env var when options.environment is not provided', () => {
107+
process.env.SENTRY_ENVIRONMENT = 'env-from-variable';
108+
109+
init({
110+
dsn: 'https://public@dsn.ingest.sentry.io/1337',
111+
});
112+
113+
expect(nodeInit).toHaveBeenCalledTimes(1);
114+
const callArgs = nodeInit.mock.calls[0]?.[0];
115+
expect(callArgs?.environment).toBe('env-from-variable');
116+
});
117+
118+
it('uses fallback environment when neither options.environment nor SENTRY_ENVIRONMENT is provided', () => {
119+
init({
120+
dsn: 'https://public@dsn.ingest.sentry.io/1337',
121+
});
122+
123+
expect(nodeInit).toHaveBeenCalledTimes(1);
124+
const callArgs = nodeInit.mock.calls[0]?.[0];
125+
// Should fallback to either 'development' or 'production' depending on the environment
126+
expect(callArgs?.environment).toBeDefined();
127+
});
128+
129+
it('prioritizes options.environment over SENTRY_ENVIRONMENT env var', () => {
130+
process.env.SENTRY_ENVIRONMENT = 'env-from-variable';
131+
132+
init({
133+
dsn: 'https://public@dsn.ingest.sentry.io/1337',
134+
environment: 'options-env',
135+
});
136+
137+
expect(nodeInit).toHaveBeenCalledTimes(1);
138+
const callArgs = nodeInit.mock.calls[0]?.[0];
139+
expect(callArgs?.environment).toBe('options-env');
140+
});
141+
});
142+
80143
describe('lowQualityTransactionsFilter', () => {
81144
const options = { debug: false };
82145
const filter = lowQualityTransactionsFilter(options);

0 commit comments

Comments
 (0)