|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | +import type { Nitro } from '../../build/types/config/types'; |
| 3 | +import { |
| 4 | + addInstrumentationFileToBuild, |
| 5 | + experimental_addInstrumentationFileTopLevelImportToServerEntry, |
| 6 | + serverFilePresets, |
| 7 | + staticHostPresets, |
| 8 | +} from '../../src/config/addInstrumentation'; |
| 9 | + |
| 10 | +const consoleLogSpy = vi.spyOn(console, 'log'); |
| 11 | +const consoleWarnSpy = vi.spyOn(console, 'warn'); |
| 12 | +const fsAccessMock = vi.fn(); |
| 13 | +const fsCopyFileMock = vi.fn(); |
| 14 | +const fsReadFile = vi.fn(); |
| 15 | +const fsWriteFileMock = vi.fn(); |
| 16 | + |
| 17 | +vi.mock('fs', async () => { |
| 18 | + const actual = await vi.importActual('fs'); |
| 19 | + return { |
| 20 | + ...actual, |
| 21 | + promises: { |
| 22 | + // @ts-expect-error this exists |
| 23 | + ...actual.promises, |
| 24 | + access: (...args: unknown[]) => fsAccessMock(...args), |
| 25 | + copyFile: (...args: unknown[]) => fsCopyFileMock(...args), |
| 26 | + readFile: (...args: unknown[]) => fsReadFile(...args), |
| 27 | + writeFile: (...args: unknown[]) => fsWriteFileMock(...args), |
| 28 | + }, |
| 29 | + }; |
| 30 | +}); |
| 31 | + |
| 32 | +beforeEach(() => { |
| 33 | + vi.clearAllMocks(); |
| 34 | +}); |
| 35 | + |
| 36 | +describe('addInstrumentationFileToBuild()', () => { |
| 37 | + const nitroOptions: Nitro = { |
| 38 | + options: { |
| 39 | + buildDir: '/path/to/buildDir', |
| 40 | + output: { |
| 41 | + serverDir: '/path/to/serverDir', |
| 42 | + }, |
| 43 | + preset: 'vercel', |
| 44 | + }, |
| 45 | + }; |
| 46 | + |
| 47 | + it('adds `instrument.server.mjs` to the server output directory', async () => { |
| 48 | + fsCopyFileMock.mockResolvedValueOnce(true); |
| 49 | + await addInstrumentationFileToBuild(nitroOptions); |
| 50 | + expect(fsCopyFileMock).toHaveBeenCalledWith( |
| 51 | + '/path/to/buildDir/build/ssr/instrument.server.js', |
| 52 | + '/path/to/serverDir/instrument.server.mjs', |
| 53 | + ); |
| 54 | + expect(consoleLogSpy).toHaveBeenCalledWith( |
| 55 | + '[Sentry SolidStart withSentry] Successfully created /path/to/serverDir/instrument.server.mjs.', |
| 56 | + ); |
| 57 | + }); |
| 58 | + |
| 59 | + it('warns when `instrument.server.js` can not be copied to the server output directory', async () => { |
| 60 | + const error = new Error('Failed to copy file.'); |
| 61 | + fsCopyFileMock.mockRejectedValueOnce(error); |
| 62 | + await addInstrumentationFileToBuild(nitroOptions); |
| 63 | + expect(fsCopyFileMock).toHaveBeenCalledWith( |
| 64 | + '/path/to/buildDir/build/ssr/instrument.server.js', |
| 65 | + '/path/to/serverDir/instrument.server.mjs', |
| 66 | + ); |
| 67 | + expect(consoleWarnSpy).toHaveBeenCalledWith( |
| 68 | + '[Sentry SolidStart withSentry] Failed to create /path/to/serverDir/instrument.server.mjs.', |
| 69 | + error, |
| 70 | + ); |
| 71 | + }); |
| 72 | + |
| 73 | + it.each([staticHostPresets])("doesn't add `instrument.server.mjs` for static host `%s`", async preset => { |
| 74 | + await addInstrumentationFileToBuild({ |
| 75 | + ...nitroOptions, |
| 76 | + options: { |
| 77 | + ...nitroOptions.options, |
| 78 | + preset, |
| 79 | + }, |
| 80 | + }); |
| 81 | + expect(fsCopyFileMock).not.toHaveBeenCalled(); |
| 82 | + }); |
| 83 | +}); |
| 84 | + |
| 85 | +describe('experimental_addInstrumentationFileTopLevelImportToServerEntry()', () => { |
| 86 | + it('adds a top level import of `instrument.server.mjs` to the index.mjs entry file', async () => { |
| 87 | + fsAccessMock.mockResolvedValueOnce(true); |
| 88 | + fsReadFile.mockResolvedValueOnce("import process from 'node:process';"); |
| 89 | + fsWriteFileMock.mockResolvedValueOnce(true); |
| 90 | + await experimental_addInstrumentationFileTopLevelImportToServerEntry('/path/to/serverDir', 'node_server'); |
| 91 | + expect(fsWriteFileMock).toHaveBeenCalledWith( |
| 92 | + '/path/to/serverDir/index.mjs', |
| 93 | + "import './instrument.server.mjs';\nimport process from 'node:process';", |
| 94 | + ); |
| 95 | + expect(consoleLogSpy).toHaveBeenCalledWith( |
| 96 | + '[Sentry SolidStart withSentry] Added `/path/to/serverDir/instrument.server.mjs` as top level import to `/path/to/serverDir/index.mjs`.', |
| 97 | + ); |
| 98 | + }); |
| 99 | + |
| 100 | + it.each([serverFilePresets])( |
| 101 | + 'adds a top level import of `instrument.server.mjs` to the server.mjs entry file for preset `%s`', |
| 102 | + async preset => { |
| 103 | + fsAccessMock.mockResolvedValueOnce(true); |
| 104 | + fsReadFile.mockResolvedValueOnce("import process from 'node:process';"); |
| 105 | + fsWriteFileMock.mockResolvedValueOnce(true); |
| 106 | + await experimental_addInstrumentationFileTopLevelImportToServerEntry('/path/to/serverDir', preset); |
| 107 | + expect(fsWriteFileMock).toHaveBeenCalledWith( |
| 108 | + '/path/to/serverDir/server.mjs', |
| 109 | + "import './instrument.server.mjs';\nimport process from 'node:process';", |
| 110 | + ); |
| 111 | + expect(consoleLogSpy).toHaveBeenCalledWith( |
| 112 | + '[Sentry SolidStart withSentry] Added `/path/to/serverDir/instrument.server.mjs` as top level import to `/path/to/serverDir/server.mjs`.', |
| 113 | + ); |
| 114 | + }, |
| 115 | + ); |
| 116 | + |
| 117 | + it("doesn't modify the sever entry file if `instrumentation.server.mjs` is not found", async () => { |
| 118 | + const error = new Error('File not found.'); |
| 119 | + fsAccessMock.mockRejectedValueOnce(error); |
| 120 | + await experimental_addInstrumentationFileTopLevelImportToServerEntry('/path/to/serverDir', 'node_server'); |
| 121 | + expect(consoleWarnSpy).toHaveBeenCalledWith( |
| 122 | + '[Sentry SolidStart withSentry] Failed to add `/path/to/serverDir/instrument.server.mjs` as top level import to `/path/to/serverDir/index.mjs`.', |
| 123 | + error, |
| 124 | + ); |
| 125 | + }); |
| 126 | + |
| 127 | + it.each([staticHostPresets])( |
| 128 | + "doesn't import `instrument.server.mjs` as top level import for host `%s`", |
| 129 | + async preset => { |
| 130 | + fsAccessMock.mockResolvedValueOnce(true); |
| 131 | + await experimental_addInstrumentationFileTopLevelImportToServerEntry('/path/to/serverDir', preset); |
| 132 | + expect(fsWriteFileMock).not.toHaveBeenCalled(); |
| 133 | + }, |
| 134 | + ); |
| 135 | +}); |
0 commit comments