Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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',
}),
],
Expand Down
7 changes: 3 additions & 4 deletions packages/astro/src/integration/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
41 changes: 41 additions & 0 deletions packages/astro/test/integration/index.files.test.ts
Copy link
Member Author

@Lms24 Lms24 Jan 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had to add a second test file here to avoid globally mocking the fs.existsSync implementation for the other tests which relied on its default implementation.

Original file line number Diff line number Diff line change
@@ -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"/),
);
});
});
7 changes: 5 additions & 2 deletions packages/astro/test/integration/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'])(
Expand Down