import type { Nuxt } from '@nuxt/schema'; import * as path from 'path'; import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; import type { SentryNuxtModuleOptions } from '../../src/common/types'; import { changeNuxtSourceMapSettings, getPluginOptions, validateNitroSourceMapSettings, } from '../../src/vite/sourceMaps'; import { extractNuxtSourceMapSetting, type SourceMapSetting, validateDifferentSourceMapSettings, } from '../../src/vite/sourceMapSettings'; vi.mock('@sentry/core', async importOriginal => { const actual = await importOriginal(); return { // eslint-disable-next-line @typescript-eslint/no-explicit-any ...(actual as any), consoleSandbox: (callback: () => void) => callback(), }; }); const NODE_SERVER_DIR = path.join(process.cwd(), '.output', 'server'); describe('getPluginOptions', () => { beforeEach(() => { vi.resetModules(); process.env = {}; }); it('uses environment variables as fallback when no moduleOptions are provided', () => { process.env = { SENTRY_ORG: 'default-org', SENTRY_PROJECT: 'default-project', SENTRY_AUTH_TOKEN: 'default-token', SENTRY_URL: 'https://santry.io', }; const options = getPluginOptions({} as SentryNuxtModuleOptions); expect(options).toEqual( expect.objectContaining({ org: 'default-org', project: 'default-project', authToken: 'default-token', url: 'https://santry.io', telemetry: true, debug: false, sourcemaps: expect.objectContaining({ rewriteSources: expect.any(Function), }), _metaOptions: expect.objectContaining({ telemetry: expect.objectContaining({ metaFramework: 'nuxt', }), }), }), ); }); it('returns default options when no moduleOptions are provided', () => { const options = getPluginOptions({} as SentryNuxtModuleOptions); expect(options.org).toBeUndefined(); expect(options.project).toBeUndefined(); expect(options.authToken).toBeUndefined(); expect(options).toEqual( expect.objectContaining({ telemetry: true, debug: false, sourcemaps: expect.objectContaining({ rewriteSources: expect.any(Function), }), _metaOptions: expect.objectContaining({ telemetry: expect.objectContaining({ metaFramework: 'nuxt', }), }), }), ); }); it('merges custom moduleOptions with default options', () => { const customOptions: SentryNuxtModuleOptions = { org: 'custom-org', project: 'custom-project', authToken: 'custom-token', telemetry: false, sourcemaps: { assets: ['custom-assets/**/*'], ignore: ['ignore-this.js'], filesToDeleteAfterUpload: ['delete-this.js'], }, debug: true, }; const options = getPluginOptions(customOptions, { client: true, server: false }); expect(options).toEqual( expect.objectContaining({ org: 'custom-org', project: 'custom-project', authToken: 'custom-token', telemetry: false, sourcemaps: expect.objectContaining({ assets: ['custom-assets/**/*'], ignore: ['ignore-this.js'], filesToDeleteAfterUpload: ['delete-this.js'], rewriteSources: expect.any(Function), }), _metaOptions: expect.objectContaining({ telemetry: expect.objectContaining({ metaFramework: 'nuxt', }), }), debug: true, }), ); }); it('resolves root-level options into plugin options', () => { const errorHandler = (err: Error): void => { throw err; }; const result = getPluginOptions({ org: 'my-org', project: 'my-project', authToken: 'my-token', sentryUrl: 'https://my.sentry.io', telemetry: false, silent: true, errorHandler, headers: { 'X-Foo': 'bar' }, release: { name: '1.2.3' }, } as SentryNuxtModuleOptions); expect(result).toMatchObject({ org: 'my-org', project: 'my-project', authToken: 'my-token', url: 'https://my.sentry.io', // sentryUrl is resolved to the plugin's `url` option telemetry: false, silent: true, errorHandler, headers: { 'X-Foo': 'bar' }, release: { name: '1.2.3' }, }); }); it('normalizes source paths via rewriteSources', () => { const options = getPluginOptions({} as SentryNuxtModuleOptions, undefined); const rewrite = options.sourcemaps?.rewriteSources as ((s: string) => string) | undefined; expect(rewrite).toBeTypeOf('function'); expect(rewrite!('../../../foo/bar')).toBe('./foo/bar'); expect(rewrite!('./local')).toBe('./local'); }); it('allows user to override rewriteSources', () => { const customRewrite = (source: string) => source.replace(/^src\//, 'custom/'); const options = getPluginOptions({ sourcemaps: { rewriteSources: customRewrite, }, } as SentryNuxtModuleOptions); expect(options.sourcemaps?.rewriteSources).toBe(customRewrite); }); it('passes applicationKey to plugin options', () => { const options: SentryNuxtModuleOptions = { applicationKey: 'my-app-key', }; const result = getPluginOptions(options); expect(result).toMatchObject({ applicationKey: 'my-app-key', }); }); it('supports bundleSizeOptimizations', () => { const options: SentryNuxtModuleOptions = { bundleSizeOptimizations: { excludeDebugStatements: true, excludeTracing: true, excludeReplayShadowDom: true, excludeReplayIframe: true, excludeReplayWorker: true, }, }; const result = getPluginOptions(options); expect(result.bundleSizeOptimizations).toEqual({ excludeDebugStatements: true, excludeTracing: true, excludeReplayShadowDom: true, excludeReplayIframe: true, excludeReplayWorker: true, }); }); it('passes moduleMetadata and sourcemaps.resolveSourceMap through to the plugin', () => { const resolveSourceMap = (artifactPath: string): string => `${artifactPath}.map`; const result = getPluginOptions({ moduleMetadata: { team: 'sdk' }, sourcemaps: { resolveSourceMap }, }); expect(result).toMatchObject({ moduleMetadata: { team: 'sdk' }, sourcemaps: expect.objectContaining({ resolveSourceMap }), }); }); it.each([ { name: 'both client and server fallback are true', clientFallback: true, serverFallback: true, expected: ['.output/server/**/*.map'], }, { name: 'only client fallback is true', clientFallback: true, serverFallback: false, expected: undefined, }, { name: 'only server fallback is true', clientFallback: false, serverFallback: true, expected: ['.output/server/**/*.map'], }, { name: 'both fallbacks are true, but Nitro is not set up yet', clientFallback: true, serverFallback: true, serverDir: null, expected: undefined, }, { name: 'no fallback, but custom filesToDeleteAfterUpload is provided', clientFallback: false, serverFallback: false, customOptions: { sourcemaps: { filesToDeleteAfterUpload: ['new-custom/path/**/*.map'] }, }, expected: ['new-custom/path/**/*.map'], }, { name: 'no fallback, both source maps explicitly false and no custom filesToDeleteAfterUpload', clientFallback: false, serverFallback: false, customOptions: {}, expected: undefined, }, ])( 'sets filesToDeleteAfterUpload correctly when $name', ({ clientFallback, serverFallback, customOptions = {}, serverDir = NODE_SERVER_DIR, expected }) => { const options = getPluginOptions(customOptions as SentryNuxtModuleOptions, { client: clientFallback, server: serverFallback, serverDir: serverDir ?? undefined, }); expect(options?.sourcemaps?.filesToDeleteAfterUpload).toEqual(expected); }, ); }); describe('validateDifferentSourceMapSettings', () => { let consoleWarnSpy: ReturnType; beforeEach(() => { consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}); }); afterEach(() => { consoleWarnSpy.mockRestore(); }); it('does not warn when both settings match', () => { validateDifferentSourceMapSettings({ nuxtSettingKey: 'sourcemap.server', nuxtSettingValue: true, otherSettingKey: 'nitro.sourceMap', otherSettingValue: true, }); expect(consoleWarnSpy).not.toHaveBeenCalled(); }); it('warns when settings conflict', () => { validateDifferentSourceMapSettings({ nuxtSettingKey: 'sourcemap.server', nuxtSettingValue: true, otherSettingKey: 'nitro.sourceMap', otherSettingValue: false, }); expect(consoleWarnSpy).toHaveBeenCalledWith(expect.stringContaining('sourcemap.server')); expect(consoleWarnSpy).toHaveBeenCalledWith(expect.stringContaining('nitro.sourceMap')); }); }); describe('extractNuxtSourceMapSetting', () => { it.each<{ runtime: 'client' | 'server' | undefined; sourcemap: SourceMapSetting | { client?: SourceMapSetting; server?: SourceMapSetting }; expected: SourceMapSetting | undefined; }>([ { runtime: undefined, sourcemap: true, expected: undefined }, { runtime: 'client', sourcemap: true, expected: true }, { runtime: 'server', sourcemap: 'hidden', expected: 'hidden' }, { runtime: 'client', sourcemap: { client: true, server: false }, expected: true }, { runtime: 'server', sourcemap: { client: true, server: 'hidden' }, expected: 'hidden' }, ])('returns correct value for runtime=$runtime and sourcemap type', ({ runtime, sourcemap, expected }) => { const nuxt = { options: { sourcemap } }; expect(extractNuxtSourceMapSetting(nuxt as Parameters[0], runtime)).toBe( expected, ); }); }); describe('validate sourcemap settings', () => { let consoleWarnSpy: ReturnType; let consoleLogSpy: ReturnType; beforeEach(() => { consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}); consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => {}); }); afterEach(() => { consoleWarnSpy.mockRestore(); consoleLogSpy.mockRestore(); }); describe('should handle nitroConfig.rollupConfig.output.sourcemap settings', () => { type MinimalNitroConfig = { sourceMap?: SourceMapSetting; rollupConfig?: { output?: { sourcemap?: SourceMapSetting; sourcemapExcludeSources?: boolean }; }; experimental?: { asyncContext?: boolean; sourcemapMinify?: false }; }; type MinimalNuxtConfig = { options: { sourcemap?: SourceMapSetting | { server?: SourceMapSetting; client?: SourceMapSetting } }; }; const getNitroConfig = ( nitroSourceMap?: SourceMapSetting, rollupSourceMap?: SourceMapSetting, ): MinimalNitroConfig => ({ sourceMap: nitroSourceMap, rollupConfig: { output: { sourcemap: rollupSourceMap } }, }); const getNuxtConfig = (nuxtSourceMap?: SourceMapSetting): MinimalNuxtConfig => ({ options: { sourcemap: { server: nuxtSourceMap } }, }); it('should log a warning when Nuxt and Nitro source map settings differ', () => { const nuxt = getNuxtConfig(true); const nitroConfig = getNitroConfig(false); validateNitroSourceMapSettings(nuxt, nitroConfig, { debug: true }); expect(consoleWarnSpy).toHaveBeenCalledWith( "[Sentry] Source map generation settings are conflicting. Sentry uses `sourcemap.server: true`. However, a conflicting setting was discovered (`nitro.sourceMap: false`). This setting was probably explicitly set in your configuration. Sentry won't override this setting but it may affect source maps generation and upload. Without source maps, code snippets on the Sentry Issues page will remain minified.", ); }); it('should set sourcemapExcludeSources to false', () => { const nitroConfig = getNitroConfig(true); validateNitroSourceMapSettings(getNuxtConfig(true), nitroConfig, { debug: true }); expect(nitroConfig?.rollupConfig?.output?.sourcemapExcludeSources).toBe(false); }); it("should disable Nitro's source map minification and keep other experimental options", () => { const nitroConfig = { ...getNitroConfig(true), experimental: { asyncContext: true } }; validateNitroSourceMapSettings(getNuxtConfig(true), nitroConfig, { debug: true }); expect(nitroConfig.experimental).toEqual({ asyncContext: true, sourcemapMinify: false }); }); it('should not show console.warn when rollup sourcemap is undefined', () => { const nitroConfig = getNitroConfig(true); validateNitroSourceMapSettings(getNuxtConfig(true), nitroConfig, { debug: true }); expect(consoleWarnSpy).not.toHaveBeenCalled(); }); }); }); describe('change Nuxt source map settings', () => { let nuxt: { options: { sourcemap: { client: boolean | 'hidden'; server: boolean | 'hidden' } } }; let sentryModuleOptions: SentryNuxtModuleOptions; let consoleLogSpy: ReturnType; beforeEach(() => { consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => {}); // @ts-expect-error - Nuxt types don't accept `undefined` but we want to test this case nuxt = { options: { sourcemap: { client: undefined } } }; sentryModuleOptions = {}; vi.stubEnv('SENTRY_AUTH_TOKEN', 'token'); vi.stubEnv('SENTRY_ORG', 'org'); vi.stubEnv('SENTRY_PROJECT', 'project'); }); afterEach(() => { consoleLogSpy.mockRestore(); vi.unstubAllEnvs(); }); it.each([ { clientSourcemap: false, expectedSourcemap: false, expectedReturn: 'disabled' }, { clientSourcemap: 'hidden', expectedSourcemap: 'hidden', expectedReturn: 'enabled' }, { clientSourcemap: true, expectedSourcemap: true, expectedReturn: 'enabled' }, { clientSourcemap: undefined, expectedSourcemap: 'hidden', expectedReturn: 'unset' }, ])( 'should handle client sourcemap setting: $clientSourcemap', ({ clientSourcemap, expectedSourcemap, expectedReturn }) => { // @ts-expect-error - Nuxt types don't accept `undefined` but we want to test this case nuxt.options.sourcemap.client = clientSourcemap; const previousUserSourcemapSetting = changeNuxtSourceMapSettings(nuxt as Nuxt, sentryModuleOptions); expect(nuxt.options.sourcemap.client).toBe(expectedSourcemap); expect(previousUserSourcemapSetting.client).toBe(expectedReturn); }, ); it.each([ { serverSourcemap: false, expectedSourcemap: false, expectedReturn: 'disabled' }, { serverSourcemap: 'hidden', expectedSourcemap: 'hidden', expectedReturn: 'enabled' }, { serverSourcemap: true, expectedSourcemap: true, expectedReturn: 'enabled' }, { serverSourcemap: undefined, expectedSourcemap: 'hidden', expectedReturn: 'unset' }, ])( 'should handle server sourcemap setting: $serverSourcemap', ({ serverSourcemap, expectedSourcemap, expectedReturn }) => { // @ts-expect-error server available nuxt.options.sourcemap.server = serverSourcemap; const previousUserSourcemapSetting = changeNuxtSourceMapSettings(nuxt as Nuxt, sentryModuleOptions); expect(nuxt.options.sourcemap.server).toBe(expectedSourcemap); expect(previousUserSourcemapSetting.server).toBe(expectedReturn); }, ); describe('should handle nuxt.options.sourcemap as a boolean', () => { it.each([ { sourcemap: false, expectedSourcemap: false, expectedReturn: 'disabled' }, { sourcemap: true, expectedSourcemap: true, expectedReturn: 'enabled' }, { sourcemap: 'hidden', expectedSourcemap: 'hidden', expectedReturn: 'enabled' }, ])('keeps nuxt.options.sourcemap setting: $sourcemap', ({ sourcemap, expectedSourcemap, expectedReturn }) => { // @ts-expect-error string type is possible in Nuxt (but type says differently) nuxt.options.sourcemap = sourcemap; const previousUserSourcemapSetting = changeNuxtSourceMapSettings(nuxt as Nuxt, { debug: true }); expect(nuxt.options.sourcemap).toBe(expectedSourcemap); expect(previousUserSourcemapSetting.client).toBe(expectedReturn); expect(previousUserSourcemapSetting.server).toBe(expectedReturn); }); it.each([ [{ upload: false }, undefined], [{ injectDebugIds: false }, undefined], [{ upload: false, delete: true }, undefined], [{ injectDebugIds: false, delete: false }, undefined], [{ upload: false, delete: false }, 'hidden'], [{ delete: true }, 'hidden'], [{ disable: 'disable-upload' }, 'hidden'], [{ disable: true, upload: true }, 'hidden'], ] as const)('with unset source maps and `sourcemaps: %j` it sets both sides to %s', (sourcemaps, expected) => { // @ts-expect-error - Nuxt types don't accept `undefined` but we want to test this case nuxt.options.sourcemap = { client: undefined, server: undefined }; const previousUserSourcemapSetting = changeNuxtSourceMapSettings(nuxt as Nuxt, { sourcemaps }); expect(nuxt.options.sourcemap).toEqual({ client: expected, server: expected }); expect(previousUserSourcemapSetting).toEqual({ client: 'unset', server: 'unset' }); }); it.each([ [{ SENTRY_AUTH_TOKEN: '' }, {}, undefined], [{ SENTRY_ORG: '' }, {}, undefined], [{ SENTRY_PROJECT: '' }, {}, undefined], [{ SENTRY_AUTH_TOKEN: '' }, { authToken: 'token' }, 'hidden'], [{ SENTRY_ORG: '', SENTRY_AUTH_TOKEN: 'sntrys_token' }, {}, 'hidden'], [{ SENTRY_AUTH_TOKEN: '' }, { sourcemaps: { delete: false } }, 'hidden'], [{ SENTRY_AUTH_TOKEN: '' }, { sourcemaps: { disable: 'disable-upload' } }, 'hidden'], ] as const)( 'with the environment %j and the options %j it sets unset source maps to %s', (env, moduleOptions, expected) => { for (const [key, value] of Object.entries(env)) { vi.stubEnv(key, value); } // @ts-expect-error - Nuxt types don't accept `undefined` but we want to test this case nuxt.options.sourcemap = { client: undefined, server: undefined }; changeNuxtSourceMapSettings(nuxt as Nuxt, moduleOptions); expect(nuxt.options.sourcemap).toEqual({ client: expected, server: expected }); }, ); it('leaves `nuxt.options.sourcemap` unset when Sentry turns no source maps on', () => { // @ts-expect-error - Nuxt types don't accept `undefined` but we want to test this case nuxt.options.sourcemap = undefined; changeNuxtSourceMapSettings(nuxt as Nuxt, { sourcemaps: { upload: false } }); expect(nuxt.options.sourcemap).toEqual({ client: undefined, server: undefined }); }); it("sets client and server to 'hidden' if nuxt.options.sourcemap not set", () => { // @ts-expect-error - Nuxt types don't accept `undefined` but we want to test this case nuxt.options.sourcemap = undefined; const previousUserSourcemapSetting = changeNuxtSourceMapSettings(nuxt as Nuxt, sentryModuleOptions); expect(nuxt.options.sourcemap.client).toBe('hidden'); expect(nuxt.options.sourcemap.server).toBe('hidden'); expect(previousUserSourcemapSetting.client).toBe('unset'); expect(previousUserSourcemapSetting.server).toBe('unset'); }); it('should log a message when source maps are enabled and debug is true', () => { const settingKey = 'sourcemap.client'; const settingValue = 'hidden'; nuxt.options.sourcemap.client = settingValue; changeNuxtSourceMapSettings(nuxt as Nuxt, { debug: true }); expect(consoleLogSpy).toHaveBeenCalledWith( `[Sentry] \`${settingKey}\` is enabled with \`${settingValue}\`. This will correctly un-minify the code snippet on the Sentry Issue Details page.`, ); }); it('should log a message when one of the source maps are unset', () => { nuxt.options.sourcemap.server = true; const { client, server } = changeNuxtSourceMapSettings(nuxt as Nuxt, { debug: true }); expect(client).toBe('unset'); expect(server).toBe('enabled'); expect(consoleLogSpy).toHaveBeenCalledWith( '[Sentry] Enabled source map generation in the build options with `sourcemap.client: hidden`.', ); }); it('should not log a message when client/server source maps are defined and debug is false', () => { nuxt.options.sourcemap.client = false; nuxt.options.sourcemap.server = true; const { client, server } = changeNuxtSourceMapSettings(nuxt as Nuxt, { debug: false }); expect(client).toBe('disabled'); expect(server).toBe('enabled'); expect(consoleLogSpy).not.toHaveBeenCalled(); }); }); });