From ea95e37644c6900a6e7384598b10ecefb32b20ba Mon Sep 17 00:00:00 2001 From: s1gr1d Date: Wed, 4 Sep 2024 12:06:50 +0200 Subject: [PATCH 1/5] feat(nuxt): Add server config to root folder --- .../test-applications/nuxt-3/package.json | 2 +- ...ent.server.mjs => sentry.server.config.ts} | 0 packages/nuxt/src/module.ts | 4 ++ packages/nuxt/src/vite/addServerConfig.ts | 50 +++++++++++++++++++ 4 files changed, 55 insertions(+), 1 deletion(-) rename dev-packages/e2e-tests/test-applications/nuxt-3/{public/instrument.server.mjs => sentry.server.config.ts} (100%) create mode 100644 packages/nuxt/src/vite/addServerConfig.ts diff --git a/dev-packages/e2e-tests/test-applications/nuxt-3/package.json b/dev-packages/e2e-tests/test-applications/nuxt-3/package.json index 93471fff7aab..9da11f778a6b 100644 --- a/dev-packages/e2e-tests/test-applications/nuxt-3/package.json +++ b/dev-packages/e2e-tests/test-applications/nuxt-3/package.json @@ -6,7 +6,7 @@ "build": "nuxt build", "dev": "nuxt dev", "generate": "nuxt generate", - "preview": "NODE_OPTIONS='--import ./public/instrument.server.mjs' nuxt preview", + "preview": "NODE_OPTIONS='--import ./server/instrument-sentry.mjs' nuxt preview", "clean": "npx nuxi cleanup", "test": "playwright test", "test:build": "pnpm install && npx playwright install && pnpm build", diff --git a/dev-packages/e2e-tests/test-applications/nuxt-3/public/instrument.server.mjs b/dev-packages/e2e-tests/test-applications/nuxt-3/sentry.server.config.ts similarity index 100% rename from dev-packages/e2e-tests/test-applications/nuxt-3/public/instrument.server.mjs rename to dev-packages/e2e-tests/test-applications/nuxt-3/sentry.server.config.ts diff --git a/packages/nuxt/src/module.ts b/packages/nuxt/src/module.ts index 5d529c99330c..9ded800c3c54 100644 --- a/packages/nuxt/src/module.ts +++ b/packages/nuxt/src/module.ts @@ -2,6 +2,7 @@ import * as fs from 'fs'; import * as path from 'path'; import { addPlugin, addPluginTemplate, addServerPlugin, createResolver, defineNuxtModule } from '@nuxt/kit'; import type { SentryNuxtModuleOptions } from './common/types'; +import { addServerConfig } from './vite/addServerConfig'; import { setupSourceMaps } from './vite/sourceMaps'; export type ModuleOptions = SentryNuxtModuleOptions; @@ -62,6 +63,9 @@ export default defineNuxtModule({ if (clientConfigFile || serverConfigFile) { setupSourceMaps(moduleOptions, nuxt); } + if (serverConfigFile) { + addServerConfig(moduleOptions, nuxt, serverConfigFile); + } }, }); diff --git a/packages/nuxt/src/vite/addServerConfig.ts b/packages/nuxt/src/vite/addServerConfig.ts new file mode 100644 index 000000000000..6e670ada3fd1 --- /dev/null +++ b/packages/nuxt/src/vite/addServerConfig.ts @@ -0,0 +1,50 @@ +import * as fs from 'fs'; +import * as path from 'path'; +import { createResolver } from '@nuxt/kit'; +import type { Nuxt } from '@nuxt/schema'; +import type { SentryNuxtModuleOptions } from '../common/types'; + +/** + * Adds the `server.config.ts` file to the `.output` directory to be able to reference this file in the node --import option. + * 1. Adding the file as a rollup import, so it is included in the build (automatically transpiles the file). + * 2. Copying the file to the `.output` directory after the build process is finished. + */ +export function addServerConfig(moduleOptions: SentryNuxtModuleOptions, nuxt: Nuxt, serverConfigFile: string): void { + nuxt.hook('vite:extendConfig', async (viteInlineConfig, _env) => { + if ( + typeof viteInlineConfig?.build?.rollupOptions?.input === 'object' && + 'server' in viteInlineConfig.build.rollupOptions.input + ) { + // Create a rollup entry for the server config to add it to the build + (viteInlineConfig.build.rollupOptions.input as { [entryName: string]: string })['instrument-sentry'] = + createResolver(nuxt.options.srcDir).resolve(`/${serverConfigFile}`); + } + + /** + * When the build process is finished, copy the `sentry.server.config` file to the `.output` directory. + * This is necessary because we need to reference this file path in the node --import option. + */ + nuxt.hook('close', async () => { + const source = path.resolve('.nuxt/dist/server/instrument-sentry.mjs'); + const destination = path.resolve('.output/server/instrument-sentry.mjs'); + + try { + await fs.promises.access(source, fs.constants.F_OK); + await fs.promises.copyFile(source, destination); + + if (moduleOptions.debug) { + // eslint-disable-next-line no-console + console.log('[Sentry] Successfully added the `sentry.server.config` file to the `.output` directory'); + } + } catch (error) { + if (moduleOptions.debug) { + // eslint-disable-next-line no-console + console.warn( + '[Sentry] An error occurred when trying to add the `sentry.server.config` file to the `.output` directory', + error, + ); + } + } + }); + }); +} From ae6d51c74ac35494b24ead781a78e88b68ad354b Mon Sep 17 00:00:00 2001 From: s1gr1d Date: Fri, 6 Sep 2024 16:10:37 +0200 Subject: [PATCH 2/5] use public file method in e2e test --- .../test-applications/nuxt-3/package.json | 6 +- .../instrument.server.mjs} | 0 packages/nuxt/src/module.ts | 26 +------ packages/nuxt/src/vite/addServerConfig.ts | 22 +++++- packages/nuxt/src/vite/utils.ts | 28 +++++++ packages/nuxt/test/vite/utils.test.ts | 73 +++++++++++++++++++ 6 files changed, 126 insertions(+), 29 deletions(-) rename dev-packages/e2e-tests/test-applications/nuxt-3/{sentry.server.config.ts => public/instrument.server.mjs} (100%) create mode 100644 packages/nuxt/src/vite/utils.ts create mode 100644 packages/nuxt/test/vite/utils.test.ts diff --git a/dev-packages/e2e-tests/test-applications/nuxt-3/package.json b/dev-packages/e2e-tests/test-applications/nuxt-3/package.json index 9da11f778a6b..7173aeaa4ce5 100644 --- a/dev-packages/e2e-tests/test-applications/nuxt-3/package.json +++ b/dev-packages/e2e-tests/test-applications/nuxt-3/package.json @@ -6,7 +6,7 @@ "build": "nuxt build", "dev": "nuxt dev", "generate": "nuxt generate", - "preview": "NODE_OPTIONS='--import ./server/instrument-sentry.mjs' nuxt preview", + "preview": "NODE_OPTIONS='--import ./public/instrument.server.mjs' nuxt preview", "clean": "npx nuxi cleanup", "test": "playwright test", "test:build": "pnpm install && npx playwright install && pnpm build", @@ -14,10 +14,10 @@ }, "dependencies": { "@sentry/nuxt": "latest || *", - "nuxt": "3.12.4" + "nuxt": "3.13.1" }, "devDependencies": { - "@nuxt/test-utils": "^3.13.1", + "@nuxt/test-utils": "^3.14.1", "@playwright/test": "^1.44.1", "@sentry-internal/test-utils": "link:../../../test-utils" } diff --git a/dev-packages/e2e-tests/test-applications/nuxt-3/sentry.server.config.ts b/dev-packages/e2e-tests/test-applications/nuxt-3/public/instrument.server.mjs similarity index 100% rename from dev-packages/e2e-tests/test-applications/nuxt-3/sentry.server.config.ts rename to dev-packages/e2e-tests/test-applications/nuxt-3/public/instrument.server.mjs diff --git a/packages/nuxt/src/module.ts b/packages/nuxt/src/module.ts index 9ded800c3c54..9d0e5c1ace59 100644 --- a/packages/nuxt/src/module.ts +++ b/packages/nuxt/src/module.ts @@ -1,9 +1,8 @@ -import * as fs from 'fs'; -import * as path from 'path'; import { addPlugin, addPluginTemplate, addServerPlugin, createResolver, defineNuxtModule } from '@nuxt/kit'; import type { SentryNuxtModuleOptions } from './common/types'; -import { addServerConfig } from './vite/addServerConfig'; +import { addServerConfigToBuild } from './vite/addServerConfig'; import { setupSourceMaps } from './vite/sourceMaps'; +import { findDefaultSdkInitFile } from './vite/utils'; export type ModuleOptions = SentryNuxtModuleOptions; @@ -63,25 +62,8 @@ export default defineNuxtModule({ if (clientConfigFile || serverConfigFile) { setupSourceMaps(moduleOptions, nuxt); } - if (serverConfigFile) { - addServerConfig(moduleOptions, nuxt, serverConfigFile); + if (serverConfigFile && serverConfigFile.includes('.server.config')) { + addServerConfigToBuild(moduleOptions, nuxt, serverConfigFile); } }, }); - -function findDefaultSdkInitFile(type: 'server' | 'client'): string | undefined { - const possibleFileExtensions = ['ts', 'js', 'mjs', 'cjs', 'mts', 'cts']; - - const cwd = process.cwd(); - const filePath = possibleFileExtensions - .map(e => - path.resolve( - type === 'server' - ? path.join(cwd, 'public', `instrument.${type}.${e}`) - : path.join(cwd, `sentry.${type}.config.${e}`), - ), - ) - .find(filename => fs.existsSync(filename)); - - return filePath ? path.basename(filePath) : undefined; -} diff --git a/packages/nuxt/src/vite/addServerConfig.ts b/packages/nuxt/src/vite/addServerConfig.ts index 6e670ada3fd1..d1f6d20a5c8d 100644 --- a/packages/nuxt/src/vite/addServerConfig.ts +++ b/packages/nuxt/src/vite/addServerConfig.ts @@ -5,17 +5,29 @@ import type { Nuxt } from '@nuxt/schema'; import type { SentryNuxtModuleOptions } from '../common/types'; /** - * Adds the `server.config.ts` file to the `.output` directory to be able to reference this file in the node --import option. + * Adds the `server.config.ts` file as `instrument-sentry.mjs` to the `.output` directory to be able to reference this file in the node --import option. + * * 1. Adding the file as a rollup import, so it is included in the build (automatically transpiles the file). * 2. Copying the file to the `.output` directory after the build process is finished. */ -export function addServerConfig(moduleOptions: SentryNuxtModuleOptions, nuxt: Nuxt, serverConfigFile: string): void { +export function addServerConfigToBuild( + moduleOptions: SentryNuxtModuleOptions, + nuxt: Nuxt, + serverConfigFile: string, +): void { + if (moduleOptions.debug) { + // eslint-disable-next-line no-console + console.log( + '[Sentry] Using your `sentry.server.config` file for the server-side Sentry configuration. In case you have a `public/instrument.server` file, it will be ignored.', + ); + } + nuxt.hook('vite:extendConfig', async (viteInlineConfig, _env) => { if ( typeof viteInlineConfig?.build?.rollupOptions?.input === 'object' && 'server' in viteInlineConfig.build.rollupOptions.input ) { - // Create a rollup entry for the server config to add it to the build + // Create a rollup entry for the server config to add it as `instrument-sentry.mjs` to the build (viteInlineConfig.build.rollupOptions.input as { [entryName: string]: string })['instrument-sentry'] = createResolver(nuxt.options.srcDir).resolve(`/${serverConfigFile}`); } @@ -34,7 +46,9 @@ export function addServerConfig(moduleOptions: SentryNuxtModuleOptions, nuxt: Nu if (moduleOptions.debug) { // eslint-disable-next-line no-console - console.log('[Sentry] Successfully added the `sentry.server.config` file to the `.output` directory'); + console.log( + '[Sentry] Successfully added the content of the `sentry.server.config` file as `instrument-sentry.mjs` to the `.output/server` directory', + ); } } catch (error) { if (moduleOptions.debug) { diff --git a/packages/nuxt/src/vite/utils.ts b/packages/nuxt/src/vite/utils.ts new file mode 100644 index 000000000000..7d794e807fd7 --- /dev/null +++ b/packages/nuxt/src/vite/utils.ts @@ -0,0 +1,28 @@ +import * as fs from 'fs'; +import * as path from 'path'; + +/** + * Find the default SDK init file for the given type (client or server). + * The sentry.server.config file is prioritized over the instrument.server file. + */ +export function findDefaultSdkInitFile(type: 'server' | 'client'): string | undefined { + const possibleFileExtensions = ['ts', 'js', 'mjs', 'cjs', 'mts', 'cts']; + const cwd = process.cwd(); + + const filePaths: string[] = []; + if (type === 'server') { + for (const ext of possibleFileExtensions) { + // order is important here - we want to prioritize the server.config file + filePaths.push(path.join(cwd, `sentry.${type}.config.${ext}`)); + filePaths.push(path.join(cwd, 'public', `instrument.${type}.${ext}`)); + } + } else { + for (const ext of possibleFileExtensions) { + filePaths.push(path.join(cwd, `sentry.${type}.config.${ext}`)); + } + } + + const filePath = filePaths.find(filename => fs.existsSync(filename)); + + return filePath ? path.basename(filePath) : undefined; +} diff --git a/packages/nuxt/test/vite/utils.test.ts b/packages/nuxt/test/vite/utils.test.ts new file mode 100644 index 000000000000..5961d8adff04 --- /dev/null +++ b/packages/nuxt/test/vite/utils.test.ts @@ -0,0 +1,73 @@ +import * as fs from 'fs'; +import { afterEach, describe, expect, it, vi } from 'vitest'; +import { findDefaultSdkInitFile } from '../../src/vite/utils'; + +vi.mock('fs'); + +describe('findDefaultSdkInitFile', () => { + afterEach(() => { + vi.clearAllMocks(); + }); + + it('should return the server file if it exists', () => { + vi.spyOn(fs, 'existsSync').mockImplementation(filePath => { + return !(filePath instanceof URL) && filePath.includes('sentry.server.config.js'); + }); + + const result = findDefaultSdkInitFile('server'); + expect(result).toBe('sentry.server.config.js'); + }); + + it('should return the client file if it exists', () => { + vi.spyOn(fs, 'existsSync').mockImplementation(filePath => { + return !(filePath instanceof URL) && filePath.includes('sentry.client.config.js'); + }); + + const result = findDefaultSdkInitFile('client'); + expect(result).toBe('sentry.client.config.js'); + }); + + it('should return undefined if no file exists', () => { + vi.spyOn(fs, 'existsSync').mockReturnValue(false); + + const result = findDefaultSdkInitFile('server'); + expect(result).toBeUndefined(); + }); + + it('should return the server config file if server.config and instrument exist', () => { + vi.spyOn(fs, 'existsSync').mockImplementation(filePath => { + return ( + !(filePath instanceof URL) && + (filePath.includes('sentry.server.config.js') || filePath.includes('instrument.server.js')) + ); + }); + + const result = findDefaultSdkInitFile('server'); + expect(result).toBe('sentry.server.config.js'); + }); + + it('should return the server file with .ts extension if it exists', () => { + vi.spyOn(fs, 'existsSync').mockImplementation(filePath => { + return !(filePath instanceof URL) && filePath.includes('sentry.server.config.ts'); + }); + + const result = findDefaultSdkInitFile('server'); + expect(result).toBe('sentry.server.config.ts'); + }); + + it('should return the client file with .mjs extension if it exists', () => { + vi.spyOn(fs, 'existsSync').mockImplementation(filePath => { + return !(filePath instanceof URL) && filePath.includes('sentry.client.config.mjs'); + }); + + const result = findDefaultSdkInitFile('client'); + expect(result).toBe('sentry.client.config.mjs'); + }); + + it('should return undefined if no file with specified extensions exists', () => { + vi.spyOn(fs, 'existsSync').mockReturnValue(false); + + const result = findDefaultSdkInitFile('server'); + expect(result).toBeUndefined(); + }); +}); From 16ef5ac513880ea81e456083c8b5ad858752a30b Mon Sep 17 00:00:00 2001 From: s1gr1d Date: Mon, 9 Sep 2024 09:41:51 +0200 Subject: [PATCH 3/5] review comments: refine console logs --- packages/nuxt/src/module.ts | 7 +++++++ packages/nuxt/src/vite/addServerConfig.ts | 13 ++----------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/packages/nuxt/src/module.ts b/packages/nuxt/src/module.ts index 9d0e5c1ace59..2de157506036 100644 --- a/packages/nuxt/src/module.ts +++ b/packages/nuxt/src/module.ts @@ -63,6 +63,13 @@ export default defineNuxtModule({ setupSourceMaps(moduleOptions, nuxt); } if (serverConfigFile && serverConfigFile.includes('.server.config')) { + if (moduleOptions.debug) { + // eslint-disable-next-line no-console + console.log( + `[Sentry] Using your ${serverConfigFile} file for the server-side Sentry configuration. In case you have a \`public/instrument.server\` file, the \`public/instrument.server\` file will be ignored. Make sure the file path in your node \`--import\` option matches your Sentry server config file.`, + ); + } + addServerConfigToBuild(moduleOptions, nuxt, serverConfigFile); } }, diff --git a/packages/nuxt/src/vite/addServerConfig.ts b/packages/nuxt/src/vite/addServerConfig.ts index d1f6d20a5c8d..c48c69014ccc 100644 --- a/packages/nuxt/src/vite/addServerConfig.ts +++ b/packages/nuxt/src/vite/addServerConfig.ts @@ -15,13 +15,6 @@ export function addServerConfigToBuild( nuxt: Nuxt, serverConfigFile: string, ): void { - if (moduleOptions.debug) { - // eslint-disable-next-line no-console - console.log( - '[Sentry] Using your `sentry.server.config` file for the server-side Sentry configuration. In case you have a `public/instrument.server` file, it will be ignored.', - ); - } - nuxt.hook('vite:extendConfig', async (viteInlineConfig, _env) => { if ( typeof viteInlineConfig?.build?.rollupOptions?.input === 'object' && @@ -46,15 +39,13 @@ export function addServerConfigToBuild( if (moduleOptions.debug) { // eslint-disable-next-line no-console - console.log( - '[Sentry] Successfully added the content of the `sentry.server.config` file as `instrument-sentry.mjs` to the `.output/server` directory', - ); + console.log(`[Sentry] Successfully added the content of the ${serverConfigFile} file to \`${destination}\``); } } catch (error) { if (moduleOptions.debug) { // eslint-disable-next-line no-console console.warn( - '[Sentry] An error occurred when trying to add the `sentry.server.config` file to the `.output` directory', + `[Sentry] An error occurred when trying to add the ${serverConfigFile} file to the \`.output\` directory`, error, ); } From 5a72b0bb186285f1e1779069b840a08475adb4cf Mon Sep 17 00:00:00 2001 From: s1gr1d Date: Mon, 9 Sep 2024 10:10:40 +0200 Subject: [PATCH 4/5] change filename to sentry.server.config.mjs --- packages/nuxt/src/module.ts | 2 +- packages/nuxt/src/vite/addServerConfig.ts | 16 +++++++++------- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/packages/nuxt/src/module.ts b/packages/nuxt/src/module.ts index 2de157506036..faa48e5c3c26 100644 --- a/packages/nuxt/src/module.ts +++ b/packages/nuxt/src/module.ts @@ -66,7 +66,7 @@ export default defineNuxtModule({ if (moduleOptions.debug) { // eslint-disable-next-line no-console console.log( - `[Sentry] Using your ${serverConfigFile} file for the server-side Sentry configuration. In case you have a \`public/instrument.server\` file, the \`public/instrument.server\` file will be ignored. Make sure the file path in your node \`--import\` option matches your Sentry server config file.`, + `[Sentry] Using your \`${serverConfigFile}\` file for the server-side Sentry configuration. In case you have a \`public/instrument.server\` file, the \`public/instrument.server\` file will be ignored. Make sure the file path in your node \`--import\` option matches the Sentry server config file in your \`.output\` folder and has a \`.mjs\` extension.`, ); } diff --git a/packages/nuxt/src/vite/addServerConfig.ts b/packages/nuxt/src/vite/addServerConfig.ts index c48c69014ccc..dc1fc21dd6bd 100644 --- a/packages/nuxt/src/vite/addServerConfig.ts +++ b/packages/nuxt/src/vite/addServerConfig.ts @@ -5,7 +5,7 @@ import type { Nuxt } from '@nuxt/schema'; import type { SentryNuxtModuleOptions } from '../common/types'; /** - * Adds the `server.config.ts` file as `instrument-sentry.mjs` to the `.output` directory to be able to reference this file in the node --import option. + * Adds the `sentry.server.config.ts` file as `sentry.server.config.mjs` to the `.output` directory to be able to reference this file in the node --import option. * * 1. Adding the file as a rollup import, so it is included in the build (automatically transpiles the file). * 2. Copying the file to the `.output` directory after the build process is finished. @@ -20,8 +20,8 @@ export function addServerConfigToBuild( typeof viteInlineConfig?.build?.rollupOptions?.input === 'object' && 'server' in viteInlineConfig.build.rollupOptions.input ) { - // Create a rollup entry for the server config to add it as `instrument-sentry.mjs` to the build - (viteInlineConfig.build.rollupOptions.input as { [entryName: string]: string })['instrument-sentry'] = + // Create a rollup entry for the server config to add it as `sentry.server.config.mjs` to the build + (viteInlineConfig.build.rollupOptions.input as { [entryName: string]: string })['sentry.server.config'] = createResolver(nuxt.options.srcDir).resolve(`/${serverConfigFile}`); } @@ -30,8 +30,8 @@ export function addServerConfigToBuild( * This is necessary because we need to reference this file path in the node --import option. */ nuxt.hook('close', async () => { - const source = path.resolve('.nuxt/dist/server/instrument-sentry.mjs'); - const destination = path.resolve('.output/server/instrument-sentry.mjs'); + const source = path.resolve('.nuxt/dist/server/sentry.server.config.mjs'); + const destination = path.resolve('.output/server/sentry.server.config.mjs'); try { await fs.promises.access(source, fs.constants.F_OK); @@ -39,13 +39,15 @@ export function addServerConfigToBuild( if (moduleOptions.debug) { // eslint-disable-next-line no-console - console.log(`[Sentry] Successfully added the content of the ${serverConfigFile} file to \`${destination}\``); + console.log( + `[Sentry] Successfully added the content of the \`${serverConfigFile}\` file to \`${destination}\``, + ); } } catch (error) { if (moduleOptions.debug) { // eslint-disable-next-line no-console console.warn( - `[Sentry] An error occurred when trying to add the ${serverConfigFile} file to the \`.output\` directory`, + `[Sentry] An error occurred when trying to add the \`${serverConfigFile}\` file to the \`.output\` directory`, error, ); } From 2be97af5fa4cc7dacec57185e957426f34be854b Mon Sep 17 00:00:00 2001 From: s1gr1d Date: Mon, 9 Sep 2024 10:30:48 +0200 Subject: [PATCH 5/5] create it.each test --- packages/nuxt/test/vite/utils.test.ts | 66 +++++++++++---------------- 1 file changed, 27 insertions(+), 39 deletions(-) diff --git a/packages/nuxt/test/vite/utils.test.ts b/packages/nuxt/test/vite/utils.test.ts index 5961d8adff04..0ca81b3e2986 100644 --- a/packages/nuxt/test/vite/utils.test.ts +++ b/packages/nuxt/test/vite/utils.test.ts @@ -9,22 +9,35 @@ describe('findDefaultSdkInitFile', () => { vi.clearAllMocks(); }); - it('should return the server file if it exists', () => { - vi.spyOn(fs, 'existsSync').mockImplementation(filePath => { - return !(filePath instanceof URL) && filePath.includes('sentry.server.config.js'); - }); - - const result = findDefaultSdkInitFile('server'); - expect(result).toBe('sentry.server.config.js'); - }); + it.each(['ts', 'js', 'mjs', 'cjs', 'mts', 'cts'])( + 'should return the server file with .%s extension if it exists', + ext => { + vi.spyOn(fs, 'existsSync').mockImplementation(filePath => { + return !(filePath instanceof URL) && filePath.includes(`sentry.server.config.${ext}`); + }); + + const result = findDefaultSdkInitFile('server'); + expect(result).toBe(`sentry.server.config.${ext}`); + }, + ); + + it.each(['ts', 'js', 'mjs', 'cjs', 'mts', 'cts'])( + 'should return the client file with .%s extension if it exists', + ext => { + vi.spyOn(fs, 'existsSync').mockImplementation(filePath => { + return !(filePath instanceof URL) && filePath.includes(`sentry.client.config.${ext}`); + }); + + const result = findDefaultSdkInitFile('client'); + expect(result).toBe(`sentry.client.config.${ext}`); + }, + ); - it('should return the client file if it exists', () => { - vi.spyOn(fs, 'existsSync').mockImplementation(filePath => { - return !(filePath instanceof URL) && filePath.includes('sentry.client.config.js'); - }); + it('should return undefined if no file with specified extensions exists', () => { + vi.spyOn(fs, 'existsSync').mockReturnValue(false); - const result = findDefaultSdkInitFile('client'); - expect(result).toBe('sentry.client.config.js'); + const result = findDefaultSdkInitFile('server'); + expect(result).toBeUndefined(); }); it('should return undefined if no file exists', () => { @@ -45,29 +58,4 @@ describe('findDefaultSdkInitFile', () => { const result = findDefaultSdkInitFile('server'); expect(result).toBe('sentry.server.config.js'); }); - - it('should return the server file with .ts extension if it exists', () => { - vi.spyOn(fs, 'existsSync').mockImplementation(filePath => { - return !(filePath instanceof URL) && filePath.includes('sentry.server.config.ts'); - }); - - const result = findDefaultSdkInitFile('server'); - expect(result).toBe('sentry.server.config.ts'); - }); - - it('should return the client file with .mjs extension if it exists', () => { - vi.spyOn(fs, 'existsSync').mockImplementation(filePath => { - return !(filePath instanceof URL) && filePath.includes('sentry.client.config.mjs'); - }); - - const result = findDefaultSdkInitFile('client'); - expect(result).toBe('sentry.client.config.mjs'); - }); - - it('should return undefined if no file with specified extensions exists', () => { - vi.spyOn(fs, 'existsSync').mockReturnValue(false); - - const result = findDefaultSdkInitFile('server'); - expect(result).toBeUndefined(); - }); });