Skip to content

Commit 5807116

Browse files
committed
add tests
1 parent ad88f83 commit 5807116

2 files changed

Lines changed: 125 additions & 1 deletion

File tree

packages/nuxt/src/vite/sourceMaps.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export function setupSourceMaps(moduleOptions: SentryNuxtModuleOptions, nuxt: Nu
7777
});
7878

7979
nuxt.hook('vite:extendConfig', async (viteConfig, env) => {
80-
if (sourceMapsEnabled && viteConfig.mode !== 'development') {
80+
if (sourceMapsEnabled && viteConfig.mode !== 'development' && !nuxt.options?._prepare) {
8181
const runtime = env.isServer ? 'server' : env.isClient ? 'client' : undefined;
8282
const nuxtSourceMapSetting = extractNuxtSourceMapSetting(nuxt, runtime);
8383

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
import type { Nuxt } from '@nuxt/schema';
2+
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
3+
import type { SourceMapSetting } from '../../src/vite/sourceMaps';
4+
5+
describe('setupSourceMaps hooks', () => {
6+
const mockSentryVitePlugin = vi.fn(() => ({ name: 'sentry-vite-plugin' }));
7+
const mockSentryRollupPlugin = vi.fn(() => ({ name: 'sentry-rollup-plugin' }));
8+
9+
const consoleLogSpy = vi.spyOn(console, 'log');
10+
const consoleWarnSpy = vi.spyOn(console, 'warn');
11+
12+
beforeAll(() => {
13+
vi.doMock('@sentry/vite-plugin', () => ({
14+
sentryVitePlugin: mockSentryVitePlugin,
15+
}));
16+
vi.doMock('@sentry/rollup-plugin', () => ({
17+
sentryRollupPlugin: mockSentryRollupPlugin,
18+
}));
19+
});
20+
21+
afterAll(() => {
22+
consoleLogSpy.mockRestore();
23+
consoleWarnSpy.mockRestore();
24+
vi.doUnmock('@sentry/vite-plugin');
25+
vi.doUnmock('@sentry/rollup-plugin');
26+
});
27+
28+
beforeEach(() => {
29+
consoleLogSpy.mockClear();
30+
consoleWarnSpy.mockClear();
31+
mockSentryVitePlugin.mockClear();
32+
mockSentryRollupPlugin.mockClear();
33+
});
34+
35+
type HookCallback = (...args: unknown[]) => void | Promise<void>;
36+
37+
function createMockNuxt(options: {
38+
_prepare?: boolean;
39+
dev?: boolean;
40+
sourcemap?: SourceMapSetting | { server?: SourceMapSetting; client?: SourceMapSetting };
41+
}) {
42+
const hooks: Record<string, HookCallback[]> = {};
43+
44+
return {
45+
options: {
46+
_prepare: options._prepare ?? false,
47+
dev: options.dev ?? false,
48+
sourcemap: options.sourcemap ?? { server: undefined, client: undefined },
49+
},
50+
hook: (name: string, callback: HookCallback) => {
51+
hooks[name] = hooks[name] || [];
52+
hooks[name].push(callback);
53+
},
54+
// Helper to trigger hooks in tests
55+
triggerHook: async (name: string, ...args: unknown[]) => {
56+
const callbacks = hooks[name] || [];
57+
for (const callback of callbacks) {
58+
await callback(...args);
59+
}
60+
},
61+
};
62+
}
63+
64+
it('should not call any source map related functions in nuxt prepare mode', async () => {
65+
const { setupSourceMaps } = await import('../../src/vite/sourceMaps');
66+
const mockNuxt = createMockNuxt({ _prepare: true });
67+
68+
setupSourceMaps({ debug: true }, mockNuxt as unknown as Nuxt);
69+
70+
await mockNuxt.triggerHook('modules:done');
71+
await mockNuxt.triggerHook(
72+
'vite:extendConfig',
73+
{ build: {}, plugins: [], mode: 'production' },
74+
{ isServer: true, isClient: false },
75+
);
76+
await mockNuxt.triggerHook('nitro:config', { rollupConfig: { plugins: [] }, dev: false });
77+
78+
expect(mockSentryVitePlugin).not.toHaveBeenCalled();
79+
expect(mockSentryRollupPlugin).not.toHaveBeenCalled();
80+
81+
expect(consoleLogSpy).not.toHaveBeenCalledWith(expect.stringContaining('[Sentry]'));
82+
});
83+
84+
it('should call source map related functions when not in prepare mode', async () => {
85+
const { setupSourceMaps } = await import('../../src/vite/sourceMaps');
86+
const mockNuxt = createMockNuxt({ _prepare: false, dev: false });
87+
88+
setupSourceMaps({ debug: true }, mockNuxt as unknown as Nuxt);
89+
90+
await mockNuxt.triggerHook('modules:done');
91+
92+
const viteConfig = { build: {}, plugins: [] as unknown[], mode: 'production' };
93+
await mockNuxt.triggerHook('vite:extendConfig', viteConfig, { isServer: true, isClient: false });
94+
95+
const nitroConfig = { rollupConfig: { plugins: [] as unknown[], output: {} }, dev: false };
96+
await mockNuxt.triggerHook('nitro:config', nitroConfig);
97+
98+
expect(mockSentryVitePlugin).toHaveBeenCalled();
99+
expect(mockSentryRollupPlugin).toHaveBeenCalled();
100+
101+
expect(viteConfig.plugins.length).toBeGreaterThan(0);
102+
expect(nitroConfig.rollupConfig.plugins.length).toBeGreaterThan(0);
103+
104+
expect(consoleLogSpy).toHaveBeenCalledWith(expect.stringContaining('[Sentry]'));
105+
});
106+
107+
it('should not call source map related functions in dev mode', async () => {
108+
const { setupSourceMaps } = await import('../../src/vite/sourceMaps');
109+
const mockNuxt = createMockNuxt({ _prepare: false, dev: true });
110+
111+
setupSourceMaps({ debug: true }, mockNuxt as unknown as Nuxt);
112+
113+
await mockNuxt.triggerHook('modules:done');
114+
await mockNuxt.triggerHook(
115+
'vite:extendConfig',
116+
{ build: {}, plugins: [], mode: 'development' },
117+
{ isServer: true, isClient: false },
118+
);
119+
await mockNuxt.triggerHook('nitro:config', { rollupConfig: { plugins: [] }, dev: true });
120+
121+
expect(mockSentryVitePlugin).not.toHaveBeenCalled();
122+
expect(mockSentryRollupPlugin).not.toHaveBeenCalled();
123+
});
124+
});

0 commit comments

Comments
 (0)