diff --git a/dev-packages/e2e-tests/test-applications/cloudflare-astro/astro.config.mjs b/dev-packages/e2e-tests/test-applications/cloudflare-astro/astro.config.mjs index 4e2493250a8f..36414cf24b7c 100644 --- a/dev-packages/e2e-tests/test-applications/cloudflare-astro/astro.config.mjs +++ b/dev-packages/e2e-tests/test-applications/cloudflare-astro/astro.config.mjs @@ -17,7 +17,8 @@ export default defineConfig({ sourceMapsUploadOptions: { enabled: false, }, - clientInitPath: 'sentry.client.mjs', + // purposefully setting the default name for client + // and a custom name for server to test both cases serverInitPath: 'sentry.server.mjs', }), ], diff --git a/dev-packages/e2e-tests/test-applications/cloudflare-astro/sentry.client.mjs b/dev-packages/e2e-tests/test-applications/cloudflare-astro/sentry.client.config.mjs similarity index 100% rename from dev-packages/e2e-tests/test-applications/cloudflare-astro/sentry.client.mjs rename to dev-packages/e2e-tests/test-applications/cloudflare-astro/sentry.client.config.mjs diff --git a/packages/astro/src/integration/index.ts b/packages/astro/src/integration/index.ts index 71e58811e0c8..15e48cb49f12 100644 --- a/packages/astro/src/integration/index.ts +++ b/packages/astro/src/integration/index.ts @@ -120,10 +120,9 @@ const possibleFileExtensions = ['ts', 'js', 'tsx', 'jsx', 'mjs', 'cjs', 'mts']; function findDefaultSdkInitFile(type: 'server' | 'client'): string | undefined { const cwd = process.cwd(); - return possibleFileExtensions.find(extension => { - const filename = path.resolve(path.join(cwd, `sentry.${type}.config.${extension}`)); - return fs.existsSync(filename); - }); + return possibleFileExtensions + .map(e => path.resolve(path.join(cwd, `sentry.${type}.config.${e}`))) + .find(filename => fs.existsSync(filename)); } function getSourcemapsAssetsGlob(config: AstroConfig): string { diff --git a/packages/astro/test/integration/index.files.test.ts b/packages/astro/test/integration/index.files.test.ts new file mode 100644 index 000000000000..f0b15f6a48c2 --- /dev/null +++ b/packages/astro/test/integration/index.files.test.ts @@ -0,0 +1,41 @@ +import { vi } from 'vitest'; + +import { sentryAstro } from '../../src/integration'; + +vi.mock('fs', async () => { + const actual = await vi.importActual('fs'); + return { + // @ts-expect-error - just mocking around + ...actual, + existsSync: vi.fn(p => p.endsWith('js')), + }; +}); + +const updateConfig = vi.fn(); +const injectScript = vi.fn(); +const config = { + root: new URL('file://path/to/project'), + outDir: new URL('file://path/to/project/out'), +}; + +describe('sentryAstro integration', () => { + afterEach(() => { + vi.clearAllMocks(); + }); + + it('injects client and server init scripts from default paths if they exist', () => { + const integration = sentryAstro({}); + + expect(integration.hooks['astro:config:setup']).toBeDefined(); + + // @ts-expect-error - the hook exists and we only need to pass what we actually use + integration.hooks['astro:config:setup']({ updateConfig, injectScript, config }); + + expect(injectScript).toHaveBeenCalledTimes(2); + expect(injectScript).toHaveBeenCalledWith('page', expect.stringMatching(/^import ".*\/sentry.client.config.js"/)); + expect(injectScript).toHaveBeenCalledWith( + 'page-ssr', + expect.stringMatching(/^import ".*\/sentry.server.config.js"/), + ); + }); +}); diff --git a/packages/astro/test/integration/index.test.ts b/packages/astro/test/integration/index.test.ts index 3e95c0c932f0..cda2d3b1aa8c 100644 --- a/packages/astro/test/integration/index.test.ts +++ b/packages/astro/test/integration/index.test.ts @@ -239,8 +239,11 @@ describe('sentryAstro integration', () => { await integration.hooks['astro:config:setup']({ updateConfig, injectScript, config }); expect(injectScript).toHaveBeenCalledTimes(2); - expect(injectScript).toHaveBeenCalledWith('page', expect.stringContaining('my-client-init-path.js')); - expect(injectScript).toHaveBeenCalledWith('page-ssr', expect.stringContaining('my-server-init-path.js')); + expect(injectScript).toHaveBeenCalledWith('page', expect.stringMatching(/^import ".*\/my-client-init-path.js"/)); + expect(injectScript).toHaveBeenCalledWith( + 'page-ssr', + expect.stringMatching(/^import ".*\/my-server-init-path.js"/), + ); }); it.each(['server', 'hybrid'])(