Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/nuxt/src/client/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import type { SentryNuxtClientOptions } from '../common/types';
* @param options Configuration options for the SDK.
*/
export function init(options: SentryNuxtClientOptions): Client | undefined {
const envFallback = import.meta.dev ? DEV_ENVIRONMENT : DEFAULT_ENVIRONMENT;
const sentryOptions = {
/* BrowserTracing is added later with the Nuxt client plugin */
defaultIntegrations: [...getBrowserDefaultIntegrations(options)],
environment: import.meta.dev ? DEV_ENVIRONMENT : DEFAULT_ENVIRONMENT,
environment: options.environment ?? process.env.SENTRY_ENVIRONMENT ?? envFallback,
...options,
};

Expand Down
3 changes: 2 additions & 1 deletion packages/nuxt/src/server/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ import type { SentryNuxtServerOptions } from '../common/types';
* @param options Configuration options for the SDK.
*/
export function init(options: SentryNuxtServerOptions): Client | undefined {
const envFallback = !isCjs() && import.meta.dev ? DEV_ENVIRONMENT : DEFAULT_ENVIRONMENT;
const sentryOptions = {
environment: !isCjs() && import.meta.dev ? DEV_ENVIRONMENT : DEFAULT_ENVIRONMENT,
environment: options.environment ?? process.env.SENTRY_ENVIRONMENT ?? envFallback,
defaultIntegrations: getNuxtDefaultIntegrations(options),
...options,
};
Expand Down
65 changes: 64 additions & 1 deletion packages/nuxt/test/client/sdk.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as SentryBrowser from '@sentry/browser';
import { SDK_VERSION } from '@sentry/vue';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { init } from '../../src/client';

const browserInit = vi.spyOn(SentryBrowser, 'init');
Expand Down Expand Up @@ -77,5 +77,68 @@ describe('Nuxt Client SDK', () => {
expect(callArgs).toBeDefined();
expect(callArgs?.defaultIntegrations).toBe(false);
});

describe('environment option', () => {
const originalEnv = process.env.SENTRY_ENVIRONMENT;

beforeEach(() => {
delete process.env.SENTRY_ENVIRONMENT;
});

afterEach(() => {
if (originalEnv !== undefined) {
process.env.SENTRY_ENVIRONMENT = originalEnv;
} else {
delete process.env.SENTRY_ENVIRONMENT;
}
});

it('uses environment from options when provided', () => {
init({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
environment: 'custom-env',
});

expect(browserInit).toHaveBeenCalledTimes(1);
const callArgs = browserInit.mock.calls[0]?.[0];
expect(callArgs?.environment).toBe('custom-env');
});

it('uses SENTRY_ENVIRONMENT env var when options.environment is not provided', () => {
process.env.SENTRY_ENVIRONMENT = 'env-from-variable';

init({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
});

expect(browserInit).toHaveBeenCalledTimes(1);
const callArgs = browserInit.mock.calls[0]?.[0];
expect(callArgs?.environment).toBe('env-from-variable');
});

it('uses fallback environment when neither options.environment nor SENTRY_ENVIRONMENT is provided', () => {
init({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
});

expect(browserInit).toHaveBeenCalledTimes(1);
const callArgs = browserInit.mock.calls[0]?.[0];
// In test environment, import.meta.dev should be checked, but we can just verify it's set
expect(callArgs?.environment).toBeDefined();
});

it('prioritizes options.environment over SENTRY_ENVIRONMENT env var', () => {
process.env.SENTRY_ENVIRONMENT = 'env-from-variable';

init({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
environment: 'options-env',
});

expect(browserInit).toHaveBeenCalledTimes(1);
const callArgs = browserInit.mock.calls[0]?.[0];
expect(callArgs?.environment).toBe('options-env');
});
});
});
});
65 changes: 64 additions & 1 deletion packages/nuxt/test/server/sdk.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Event, EventProcessor } from '@sentry/core';
import * as SentryNode from '@sentry/node';
import { getGlobalScope, Scope, SDK_VERSION } from '@sentry/node';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { init } from '../../src/server';
import { clientSourceMapErrorFilter, lowQualityTransactionsFilter } from '../../src/server/sdk';

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

describe('environment option', () => {
const originalEnv = process.env.SENTRY_ENVIRONMENT;

beforeEach(() => {
delete process.env.SENTRY_ENVIRONMENT;
});

afterEach(() => {
if (originalEnv !== undefined) {
process.env.SENTRY_ENVIRONMENT = originalEnv;
} else {
delete process.env.SENTRY_ENVIRONMENT;
}
});

it('uses environment from options when provided', () => {
init({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
environment: 'custom-env',
});

expect(nodeInit).toHaveBeenCalledTimes(1);
const callArgs = nodeInit.mock.calls[0]?.[0];
expect(callArgs?.environment).toBe('custom-env');
});

it('uses SENTRY_ENVIRONMENT env var when options.environment is not provided', () => {
process.env.SENTRY_ENVIRONMENT = 'env-from-variable';

init({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
});

expect(nodeInit).toHaveBeenCalledTimes(1);
const callArgs = nodeInit.mock.calls[0]?.[0];
expect(callArgs?.environment).toBe('env-from-variable');
});

it('uses fallback environment when neither options.environment nor SENTRY_ENVIRONMENT is provided', () => {
init({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
});

expect(nodeInit).toHaveBeenCalledTimes(1);
const callArgs = nodeInit.mock.calls[0]?.[0];
// Should fallback to either 'development' or 'production' depending on the environment
expect(callArgs?.environment).toBeDefined();
});

it('prioritizes options.environment over SENTRY_ENVIRONMENT env var', () => {
process.env.SENTRY_ENVIRONMENT = 'env-from-variable';

init({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
environment: 'options-env',
});

expect(nodeInit).toHaveBeenCalledTimes(1);
const callArgs = nodeInit.mock.calls[0]?.[0];
expect(callArgs?.environment).toBe('options-env');
});
});

describe('lowQualityTransactionsFilter', () => {
const options = { debug: false };
const filter = lowQualityTransactionsFilter(options);
Expand Down
Loading