|
| 1 | +import * as fs from 'fs'; |
| 2 | +import { afterEach, describe, expect, it, vi } from 'vitest'; |
| 3 | +import { findDefaultSdkInitFile } from '../../src/vite/utils'; |
| 4 | + |
| 5 | +vi.mock('fs'); |
| 6 | + |
| 7 | +describe('findDefaultSdkInitFile', () => { |
| 8 | + afterEach(() => { |
| 9 | + vi.clearAllMocks(); |
| 10 | + }); |
| 11 | + |
| 12 | + it.each(['ts', 'js', 'mjs', 'cjs', 'mts', 'cts'])( |
| 13 | + 'should return the server file with .%s extension if it exists', |
| 14 | + ext => { |
| 15 | + vi.spyOn(fs, 'existsSync').mockImplementation(filePath => { |
| 16 | + return !(filePath instanceof URL) && filePath.includes(`sentry.server.config.${ext}`); |
| 17 | + }); |
| 18 | + |
| 19 | + const result = findDefaultSdkInitFile('server'); |
| 20 | + expect(result).toBe(`sentry.server.config.${ext}`); |
| 21 | + }, |
| 22 | + ); |
| 23 | + |
| 24 | + it.each(['ts', 'js', 'mjs', 'cjs', 'mts', 'cts'])( |
| 25 | + 'should return the client file with .%s extension if it exists', |
| 26 | + ext => { |
| 27 | + vi.spyOn(fs, 'existsSync').mockImplementation(filePath => { |
| 28 | + return !(filePath instanceof URL) && filePath.includes(`sentry.client.config.${ext}`); |
| 29 | + }); |
| 30 | + |
| 31 | + const result = findDefaultSdkInitFile('client'); |
| 32 | + expect(result).toBe(`sentry.client.config.${ext}`); |
| 33 | + }, |
| 34 | + ); |
| 35 | + |
| 36 | + it('should return undefined if no file with specified extensions exists', () => { |
| 37 | + vi.spyOn(fs, 'existsSync').mockReturnValue(false); |
| 38 | + |
| 39 | + const result = findDefaultSdkInitFile('server'); |
| 40 | + expect(result).toBeUndefined(); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should return undefined if no file exists', () => { |
| 44 | + vi.spyOn(fs, 'existsSync').mockReturnValue(false); |
| 45 | + |
| 46 | + const result = findDefaultSdkInitFile('server'); |
| 47 | + expect(result).toBeUndefined(); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should return the server config file if server.config and instrument exist', () => { |
| 51 | + vi.spyOn(fs, 'existsSync').mockImplementation(filePath => { |
| 52 | + return ( |
| 53 | + !(filePath instanceof URL) && |
| 54 | + (filePath.includes('sentry.server.config.js') || filePath.includes('instrument.server.js')) |
| 55 | + ); |
| 56 | + }); |
| 57 | + |
| 58 | + const result = findDefaultSdkInitFile('server'); |
| 59 | + expect(result).toBe('sentry.server.config.js'); |
| 60 | + }); |
| 61 | +}); |
0 commit comments