Skip to content

Commit 396b555

Browse files
committed
fix tests
1 parent 27a8fcf commit 396b555

File tree

3 files changed

+85
-54
lines changed

3 files changed

+85
-54
lines changed

packages/sveltekit/src/vite/sourceMaps.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ export async function makeCustomSentryVitePlugins(options?: CustomSentryVitePlug
9797
};
9898

9999
const customPlugin: Plugin = {
100-
name: 'sentry-upload-source-maps',
100+
name: 'sentry-upload-sveltekit-source-maps',
101101
apply: 'build', // only apply this plugin at build time
102102
enforce: 'post', // this needs to be set to post, otherwise we don't pick up the output from the SvelteKit adapter
103103

@@ -150,7 +150,7 @@ export async function makeCustomSentryVitePlugins(options?: CustomSentryVitePlug
150150

151151
if (isServerHooksFile) {
152152
const ms = new MagicString(code);
153-
ms.append(`\n; import "${VIRTUAL_GLOBAL_VALUES_FILE}";`);
153+
ms.append(`\n; import "${VIRTUAL_GLOBAL_VALUES_FILE}";\n`);
154154
return {
155155
code: ms.toString(),
156156
map: ms.generateMap({ hires: true }),

packages/sveltekit/test/vite/sentrySvelteKitPlugins.test.ts

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { vi } from 'vitest';
22

3+
import type { Plugin } from 'vite';
34
import * as autoInstrument from '../../src/vite/autoInstrument';
45
import { sentrySvelteKit } from '../../src/vite/sentryVitePlugins';
56
import * as sourceMaps from '../../src/vite/sourceMaps';
@@ -24,32 +25,53 @@ vi.spyOn(console, 'warn').mockImplementation(() => {
2425
/* noop */
2526
});
2627

28+
function getSentrySvelteKitPlugins(options?: Parameters<typeof sentrySvelteKit>[0]): Promise<Plugin[]> {
29+
return sentrySvelteKit({
30+
sourceMapsUploadOptions: {
31+
authToken: 'token',
32+
org: 'org',
33+
project: 'project',
34+
...options?.sourceMapsUploadOptions,
35+
},
36+
...options,
37+
});
38+
}
39+
2740
describe('sentrySvelteKit()', () => {
2841
it('returns an array of Vite plugins', async () => {
29-
const plugins = await sentrySvelteKit();
42+
const plugins = await getSentrySvelteKitPlugins();
3043

3144
expect(plugins).toBeInstanceOf(Array);
32-
expect(plugins).toHaveLength(2);
45+
// 1 auto instrument plugin + 5 source maps plugins
46+
expect(plugins).toHaveLength(6);
3347
});
3448

35-
it('returns the custom sentry source maps plugin and the auto-instrument plugin by default', async () => {
36-
const plugins = await sentrySvelteKit();
37-
const instrumentPlugin = plugins[0];
38-
const sourceMapsPlugin = plugins[1];
39-
expect(instrumentPlugin.name).toEqual('sentry-auto-instrumentation');
40-
expect(sourceMapsPlugin.name).toEqual('sentry-upload-source-maps');
49+
it('returns the custom sentry source maps upload plugin, unmodified sourcemaps plugins and the auto-instrument plugin by default', async () => {
50+
const plugins = await getSentrySvelteKitPlugins();
51+
const pluginNames = plugins.map(plugin => plugin.name);
52+
expect(pluginNames).toEqual([
53+
// auto instrument plugin:
54+
'sentry-auto-instrumentation',
55+
// default source maps plugins:
56+
'sentry-telemetry-plugin',
57+
'sentry-vite-release-injection-plugin',
58+
'sentry-debug-id-upload-plugin',
59+
'sentry-vite-debug-id-injection-plugin',
60+
// custom source maps plugin:
61+
'sentry-upload-sveltekit-source-maps',
62+
]);
4163
});
4264

43-
it("doesn't return the custom sentry source maps plugin if autoUploadSourcemaps is `false`", async () => {
44-
const plugins = await sentrySvelteKit({ autoUploadSourceMaps: false });
65+
it("doesn't return the sentry source maps plugins if autoUploadSourcemaps is `false`", async () => {
66+
const plugins = await getSentrySvelteKitPlugins({ autoUploadSourceMaps: false });
4567
expect(plugins).toHaveLength(1);
4668
});
4769

48-
it("doesn't return the custom sentry source maps plugin if `NODE_ENV` is development", async () => {
70+
it("doesn't return the sentry source maps plugins if `NODE_ENV` is development", async () => {
4971
const previousEnv = process.env.NODE_ENV;
5072

5173
process.env.NODE_ENV = 'development';
52-
const plugins = await sentrySvelteKit({ autoUploadSourceMaps: true, autoInstrument: true });
74+
const plugins = await getSentrySvelteKitPlugins({ autoUploadSourceMaps: true, autoInstrument: true });
5375
const instrumentPlugin = plugins[0];
5476

5577
expect(plugins).toHaveLength(1);
@@ -59,35 +81,33 @@ describe('sentrySvelteKit()', () => {
5981
});
6082

6183
it("doesn't return the auto instrument plugin if autoInstrument is `false`", async () => {
62-
const plugins = await sentrySvelteKit({ autoInstrument: false });
63-
expect(plugins).toHaveLength(1);
84+
const plugins = await getSentrySvelteKitPlugins({ autoInstrument: false });
85+
const pluginNames = plugins.map(plugin => plugin.name);
86+
expect(plugins).toHaveLength(5);
87+
expect(pluginNames).not.toContain('sentry-upload-source-maps');
6488
});
6589

66-
it('passes user-specified vite pugin options to the custom sentry source maps plugin', async () => {
67-
const makePluginSpy = vi.spyOn(sourceMaps, 'makeCustomSentryVitePlugin');
68-
const plugins = await sentrySvelteKit({
90+
it('passes user-specified vite plugin options to the custom sentry source maps plugin', async () => {
91+
const makePluginSpy = vi.spyOn(sourceMaps, 'makeCustomSentryVitePlugins');
92+
await getSentrySvelteKitPlugins({
6993
debug: true,
7094
sourceMapsUploadOptions: {
71-
include: ['foo.js'],
72-
ignore: ['bar.js'],
95+
assets: ['foo/*.js'],
7396
},
7497
autoInstrument: false,
7598
adapter: 'vercel',
7699
});
77-
const plugin = plugins[0];
78100

79-
expect(plugin.name).toEqual('sentry-upload-source-maps');
80101
expect(makePluginSpy).toHaveBeenCalledWith({
81102
debug: true,
82-
ignore: ['bar.js'],
83-
include: ['foo.js'],
103+
assets: ['foo/*.js'],
84104
adapter: 'vercel',
85105
});
86106
});
87107

88108
it('passes user-specified options to the auto instrument plugin', async () => {
89109
const makePluginSpy = vi.spyOn(autoInstrument, 'makeAutoInstrumentationPlugin');
90-
const plugins = await sentrySvelteKit({
110+
const plugins = await getSentrySvelteKitPlugins({
91111
debug: true,
92112
autoInstrument: {
93113
load: true,

packages/sveltekit/test/vite/sourceMaps.test.ts

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import { vi } from 'vitest';
22

3-
import { makeCustomSentryVitePlugin } from '../../src/vite/sourceMaps';
3+
import type { Plugin } from 'vite';
4+
import { makeCustomSentryVitePlugins } from '../../src/vite/sourceMaps';
45

56
const mockedSentryVitePlugin = {
6-
buildStart: vi.fn(),
7-
resolveId: vi.fn(),
8-
renderChunk: vi.fn(),
9-
transform: vi.fn().mockImplementation((code: string, _id: string) => code),
7+
name: 'sentry-vite-debug-id-upload-plugin',
108
writeBundle: vi.fn(),
119
};
1210

@@ -15,34 +13,45 @@ vi.mock('@sentry/vite-plugin', async () => {
1513

1614
return {
1715
...original,
18-
sentryVitePlugin: () => mockedSentryVitePlugin,
16+
sentryVitePlugin: () => [mockedSentryVitePlugin],
1917
};
2018
});
2119

2220
beforeEach(() => {
2321
vi.clearAllMocks();
2422
});
2523

24+
async function getCustomSentryViteUploadSourcemapsPlugin(): Promise<Plugin | undefined> {
25+
const plugins = await makeCustomSentryVitePlugins({
26+
authToken: 'token',
27+
org: 'org',
28+
project: 'project',
29+
adapter: 'other',
30+
});
31+
return plugins.find(plugin => plugin.name === 'sentry-upload-sveltekit-source-maps');
32+
}
33+
2634
describe('makeCustomSentryVitePlugin()', () => {
2735
it('returns the custom sentry source maps plugin', async () => {
28-
const plugin = await makeCustomSentryVitePlugin();
29-
expect(plugin.name).toEqual('sentry-upload-source-maps');
30-
expect(plugin.apply).toEqual('build');
31-
expect(plugin.enforce).toEqual('post');
32-
33-
expect(plugin.buildStart).toBeInstanceOf(Function);
34-
expect(plugin.resolveId).toBeInstanceOf(Function);
35-
expect(plugin.renderChunk).toBeInstanceOf(Function);
36-
expect(plugin.transform).toBeInstanceOf(Function);
37-
38-
expect(plugin.config).toBeInstanceOf(Function);
39-
expect(plugin.configResolved).toBeInstanceOf(Function);
40-
expect(plugin.closeBundle).toBeInstanceOf(Function);
36+
const plugin = await getCustomSentryViteUploadSourcemapsPlugin();
37+
expect(plugin?.name).toEqual('sentry-upload-sveltekit-source-maps');
38+
expect(plugin?.apply).toEqual('build');
39+
expect(plugin?.enforce).toEqual('post');
40+
41+
expect(plugin?.resolveId).toBeInstanceOf(Function);
42+
expect(plugin?.transform).toBeInstanceOf(Function);
43+
44+
expect(plugin?.config).toBeInstanceOf(Function);
45+
expect(plugin?.configResolved).toBeInstanceOf(Function);
46+
47+
// instead of writeBundle, this plugin uses closeBundle
48+
expect(plugin?.closeBundle).toBeInstanceOf(Function);
49+
expect(plugin?.writeBundle).toBeUndefined();
4150
});
4251

4352
describe('Custom sentry vite plugin', () => {
4453
it('enables source map generation', async () => {
45-
const plugin = await makeCustomSentryVitePlugin();
54+
const plugin = await getCustomSentryViteUploadSourcemapsPlugin();
4655
// @ts-expect-error this function exists!
4756
const sentrifiedConfig = plugin.config({ build: { foo: {} }, test: {} });
4857
expect(sentrifiedConfig).toEqual({
@@ -55,16 +64,18 @@ describe('makeCustomSentryVitePlugin()', () => {
5564
});
5665

5766
it('injects the output dir into the server hooks file', async () => {
58-
const plugin = await makeCustomSentryVitePlugin();
67+
const plugin = await getCustomSentryViteUploadSourcemapsPlugin();
5968
// @ts-expect-error this function exists!
60-
const transformedCode = await plugin.transform('foo', '/src/hooks.server.ts');
61-
const expectedtransformedCode = 'foo\n; import "\0sentry-inject-global-values-file";\n';
62-
expect(mockedSentryVitePlugin.transform).toHaveBeenCalledWith(expectedtransformedCode, '/src/hooks.server.ts');
63-
expect(transformedCode).toEqual(expectedtransformedCode);
69+
const transformOutput = await plugin.transform('foo', '/src/hooks.server.ts');
70+
const transformedCode = transformOutput.code;
71+
const transformedSourcemap = transformOutput.map;
72+
const expectedTransformedCode = 'foo\n; import "\0sentry-inject-global-values-file";\n';
73+
expect(transformedCode).toEqual(expectedTransformedCode);
74+
expect(transformedSourcemap).toBeDefined();
6475
});
6576

6677
it('uploads source maps during the SSR build', async () => {
67-
const plugin = await makeCustomSentryVitePlugin();
78+
const plugin = await getCustomSentryViteUploadSourcemapsPlugin();
6879
// @ts-expect-error this function exists!
6980
plugin.configResolved({ build: { ssr: true } });
7081
// @ts-expect-error this function exists!
@@ -73,7 +84,7 @@ describe('makeCustomSentryVitePlugin()', () => {
7384
});
7485

7586
it("doesn't upload source maps during the non-SSR builds", async () => {
76-
const plugin = await makeCustomSentryVitePlugin();
87+
const plugin = await getCustomSentryViteUploadSourcemapsPlugin();
7788

7889
// @ts-expect-error this function exists!
7990
plugin.configResolved({ build: { ssr: false } });
@@ -91,7 +102,7 @@ describe('makeCustomSentryVitePlugin()', () => {
91102
const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
92103
const consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
93104

94-
const plugin = await makeCustomSentryVitePlugin();
105+
const plugin = await getCustomSentryViteUploadSourcemapsPlugin();
95106

96107
// @ts-expect-error this function exists!
97108
expect(plugin.closeBundle).not.toThrow();

0 commit comments

Comments
 (0)