|
| 1 | +import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; |
| 2 | +import * as fs from "fs"; |
| 3 | +import * as path from "path"; |
| 4 | +import * as os from "os"; |
| 5 | +import { prepareBundleForDebugIdUpload } from "../src/debug-id-upload"; |
| 6 | +import type { RewriteSourcesHook } from "../src/types"; |
| 7 | +import { Logger } from "../src"; |
| 8 | + |
| 9 | +describe("prepareBundleForDebugIdUpload", () => { |
| 10 | + let tmpDir: string; |
| 11 | + |
| 12 | + beforeEach(() => { |
| 13 | + tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "sentry-test-")); |
| 14 | + }); |
| 15 | + |
| 16 | + afterEach(() => { |
| 17 | + fs.rmSync(tmpDir, { recursive: true, force: true }); |
| 18 | + }); |
| 19 | + |
| 20 | + it("passes mapDir context to rewriteSources hook", async () => { |
| 21 | + const bundleDir = path.join(tmpDir, "src"); |
| 22 | + const uploadDir = path.join(tmpDir, "upload"); |
| 23 | + fs.mkdirSync(bundleDir, { recursive: true }); |
| 24 | + fs.mkdirSync(uploadDir, { recursive: true }); |
| 25 | + |
| 26 | + const debugId = "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"; |
| 27 | + const bundlePath = path.join(bundleDir, "bundle.js"); |
| 28 | + const mapPath = path.join(bundleDir, "bundle.js.map"); |
| 29 | + |
| 30 | + // Bundle with debug ID snippet and sourceMappingURL |
| 31 | + fs.writeFileSync( |
| 32 | + bundlePath, |
| 33 | + `"use strict";\n// some code\n;!function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="${debugId}",e._sentryDebugIdIdentifier="sentry-dbid-${debugId}")}catch(e){}}();\n//# sourceMappingURL=bundle.js.map` |
| 34 | + ); |
| 35 | + |
| 36 | + // Source map file |
| 37 | + fs.writeFileSync( |
| 38 | + mapPath, |
| 39 | + JSON.stringify({ |
| 40 | + version: 3, |
| 41 | + sources: ["../original/file.ts"], |
| 42 | + mappings: "AAAA", |
| 43 | + }) |
| 44 | + ); |
| 45 | + |
| 46 | + const capturedContexts: Array<{ mapDir?: string } | undefined> = []; |
| 47 | + const rewriteHook: RewriteSourcesHook = (source, _map, context) => { |
| 48 | + capturedContexts.push(context); |
| 49 | + return source; |
| 50 | + }; |
| 51 | + |
| 52 | + const logger = { |
| 53 | + info: vi.fn(), |
| 54 | + warn: vi.fn(), |
| 55 | + error: vi.fn(), |
| 56 | + debug: vi.fn(), |
| 57 | + }; |
| 58 | + |
| 59 | + await prepareBundleForDebugIdUpload( |
| 60 | + bundlePath, |
| 61 | + uploadDir, |
| 62 | + 0, |
| 63 | + logger as Logger, |
| 64 | + rewriteHook, |
| 65 | + undefined |
| 66 | + ); |
| 67 | + |
| 68 | + expect(capturedContexts).toHaveLength(1); |
| 69 | + expect(capturedContexts[0]!.mapDir).toBe(bundleDir); |
| 70 | + }); |
| 71 | +}); |
0 commit comments