Skip to content

Commit 811fd0b

Browse files
committed
add tests
1 parent fd29a63 commit 811fd0b

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed

packages/astro/src/integration/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export const sentryAstro = (options: SentryOptions = {}): AstroIntegration => {
6060
if (pathToServerInit) {
6161
options.debug && console.log(`[sentry-astro] Using ${pathToServerInit} for server init.`);
6262
injectScript('page-ssr', buildSdkInitFileImportSnippet(pathToServerInit));
63+
} else {
6364
options.debug && console.log('[sentry-astro] Using default server init.');
6465
injectScript('page-ssr', buildServerSnippet(options || {}));
6566
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import { vi } from 'vitest';
2+
3+
import { sentryAstro } from '../../src/integration';
4+
5+
const sentryVitePluginSpy = vi.fn(() => 'sentryVitePlugin');
6+
7+
vi.mock('@sentry/vite-plugin', () => ({
8+
// @ts-expect-error - just mocking around
9+
sentryVitePlugin: vi.fn(args => sentryVitePluginSpy(args)),
10+
}));
11+
12+
vi.mock('vite', () => ({
13+
loadEnv: () => ({
14+
SENTRY_AUTH_TOKEN: 'my-token',
15+
}),
16+
}));
17+
18+
describe('sentryAstro integration', () => {
19+
afterEach(() => {
20+
vi.clearAllMocks();
21+
});
22+
23+
it('has a name', () => {
24+
const integration = sentryAstro({});
25+
expect(integration.name).toBe('@sentry/astro');
26+
});
27+
28+
it('enables source maps and adds the sentry vite plugin if an auth token is detected', async () => {
29+
const integration = sentryAstro({
30+
sourceMapsUploadOptions: { enabled: true, org: 'my-org', project: 'my-project', telemetry: false },
31+
});
32+
const updateConfig = vi.fn();
33+
const injectScript = vi.fn();
34+
35+
expect(integration.hooks['astro:config:setup']).toBeDefined();
36+
// @ts-expect-error - the hook exists and we only need to pass what we actually use
37+
await integration.hooks['astro:config:setup']({ updateConfig, injectScript });
38+
39+
expect(updateConfig).toHaveBeenCalledTimes(1);
40+
expect(updateConfig).toHaveBeenCalledWith({
41+
vite: {
42+
build: {
43+
sourcemap: true,
44+
},
45+
plugins: ['sentryVitePlugin'],
46+
},
47+
});
48+
49+
expect(sentryVitePluginSpy).toHaveBeenCalledTimes(1);
50+
expect(sentryVitePluginSpy).toHaveBeenCalledWith({
51+
authToken: 'my-token',
52+
org: 'my-org',
53+
project: 'my-project',
54+
telemetry: false,
55+
});
56+
});
57+
58+
it("doesn't enable source maps if `sourceMapsUploadOptions.enabled` is `false`", async () => {
59+
const integration = sentryAstro({
60+
sourceMapsUploadOptions: { enabled: false },
61+
});
62+
const updateConfig = vi.fn();
63+
const injectScript = vi.fn();
64+
65+
expect(integration.hooks['astro:config:setup']).toBeDefined();
66+
// @ts-expect-error - the hook exists and we only need to pass what we actually use
67+
await integration.hooks['astro:config:setup']({ updateConfig, injectScript });
68+
69+
expect(updateConfig).toHaveBeenCalledTimes(0);
70+
expect(sentryVitePluginSpy).toHaveBeenCalledTimes(0);
71+
});
72+
73+
it('injects client and server init scripts', async () => {
74+
const integration = sentryAstro({});
75+
const updateConfig = vi.fn();
76+
const injectScript = vi.fn();
77+
78+
expect(integration.hooks['astro:config:setup']).toBeDefined();
79+
// @ts-expect-error - the hook exists and we only need to pass what we actually use
80+
await integration.hooks['astro:config:setup']({ updateConfig, injectScript });
81+
82+
expect(injectScript).toHaveBeenCalledTimes(2);
83+
expect(injectScript).toHaveBeenCalledWith('page', expect.stringContaining('Sentry.init'));
84+
expect(injectScript).toHaveBeenCalledWith('page-ssr', expect.stringContaining('Sentry.init'));
85+
});
86+
87+
it('injects client and server init scripts from custom paths', async () => {
88+
const integration = sentryAstro({
89+
clientInitPath: 'my-client-init-path.js',
90+
serverInitPath: 'my-server-init-path.js',
91+
});
92+
93+
const updateConfig = vi.fn();
94+
const injectScript = vi.fn();
95+
96+
expect(integration.hooks['astro:config:setup']).toBeDefined();
97+
// @ts-expect-error - the hook exists and we only need to pass what we actually use
98+
await integration.hooks['astro:config:setup']({ updateConfig, injectScript });
99+
100+
expect(injectScript).toHaveBeenCalledTimes(2);
101+
expect(injectScript).toHaveBeenCalledWith('page', expect.stringContaining('my-client-init-path.js'));
102+
expect(injectScript).toHaveBeenCalledWith('page-ssr', expect.stringContaining('my-server-init-path.js'));
103+
});
104+
});

0 commit comments

Comments
 (0)