|
| 1 | +import * as assert from 'node:assert/strict'; |
| 2 | +import * as fs from 'node:fs'; |
| 3 | +import * as path from 'node:path'; |
| 4 | +import { |
| 5 | + getArtifactBundles, |
| 6 | + getAssembleRequests, |
| 7 | + getChunkUploadPosts, |
| 8 | + getDebugIdPairs, |
| 9 | + getSourcemaps, |
| 10 | + loadMockServerResults, |
| 11 | +} from '@sentry-internal/test-utils'; |
| 12 | + |
| 13 | +const BUILD_OUTPUT = 'build'; |
| 14 | +const CLIENT_OUTPUT = path.join(BUILD_OUTPUT, 'client'); |
| 15 | +const SERVER_OUTPUT = path.join(BUILD_OUTPUT, 'server'); |
| 16 | + |
| 17 | +/** Both markers sit in comments, so bundlers strip them from the code but keep them in `sourcesContent`. */ |
| 18 | +const CLIENT_MARKER = 'SOURCEMAP_MARKER_CLIENT'; |
| 19 | +const SERVER_MARKER = 'SOURCEMAP_MARKER_SERVER'; |
| 20 | + |
| 21 | +function listFiles(dir: string): string[] { |
| 22 | + return fs |
| 23 | + .readdirSync(dir, { recursive: true, withFileTypes: true }) |
| 24 | + .filter(entry => entry.isFile()) |
| 25 | + .map(entry => path.join(entry.parentPath, entry.name)); |
| 26 | +} |
| 27 | + |
| 28 | +const jsFiles = (dir: string): string[] => listFiles(dir).filter(file => file.endsWith('.js')); |
| 29 | + |
| 30 | +/** Written by the build, see `recordChunksWithoutSourceMap` in `vite.config.js`. */ |
| 31 | +const debugIdsWithoutSourceMap = new Set( |
| 32 | + fs.existsSync('.tmp_debug_ids_without_source_map') |
| 33 | + ? fs.readFileSync('.tmp_debug_ids_without_source_map', 'utf8').split('\n').filter(Boolean) |
| 34 | + : [], |
| 35 | +); |
| 36 | + |
| 37 | +const requests = loadMockServerResults(); |
| 38 | + |
| 39 | +console.log(`Captured ${requests.length} requests to mock Sentry server:\n`); |
| 40 | +for (const request of requests) { |
| 41 | + console.log(` ${request.method} ${request.url} (${request.bodySize} bytes)`); |
| 42 | +} |
| 43 | +console.log(''); |
| 44 | + |
| 45 | +assert.ok( |
| 46 | + requests.some(r => r.authorization.includes('fake-auth-token')), |
| 47 | + 'Expected requests with the configured auth token', |
| 48 | +); |
| 49 | + |
| 50 | +assert.ok( |
| 51 | + requests.some(r => r.url?.includes('/releases') && r.method === 'POST'), |
| 52 | + 'Expected a POST to create the release', |
| 53 | +); |
| 54 | + |
| 55 | +assert.ok( |
| 56 | + getChunkUploadPosts(requests).some(r => r.bodySize > 0), |
| 57 | + 'Expected at least one chunk upload POST with a non-empty body', |
| 58 | +); |
| 59 | + |
| 60 | +const assembleRequests = getAssembleRequests(requests); |
| 61 | +assert.ok(assembleRequests.length > 0, 'Expected at least one assemble request'); |
| 62 | +for (const request of assembleRequests) { |
| 63 | + assert.ok(request.assembleBody?.projects?.includes('test-project'), 'Expected assemble request for test-project'); |
| 64 | + assert.equal(request.assembleBody?.version, 'test-release', 'Expected assemble request to reference the release'); |
| 65 | +} |
| 66 | + |
| 67 | +const bundles = getArtifactBundles(requests); |
| 68 | +assert.ok(bundles.length > 0, 'Expected at least one artifact bundle with a manifest'); |
| 69 | + |
| 70 | +const sourcemaps = getSourcemaps(bundles); |
| 71 | + |
| 72 | +const containsMarker = (marker: string): boolean => |
| 73 | + sourcemaps.some(map => map.sourcemap.sourcesContent?.some(source => source?.includes(marker))); |
| 74 | + |
| 75 | +assert.ok(containsMarker(CLIENT_MARKER), 'Expected an uploaded sourcemap carrying the client source'); |
| 76 | +assert.ok(containsMarker(SERVER_MARKER), 'Expected an uploaded sourcemap carrying the server source'); |
| 77 | + |
| 78 | +// The auto-instrument plugin loads wrapped modules under this suffix. It must be stripped before |
| 79 | +// upload, or the uploaded sources don't match the frames Sentry receives. |
| 80 | +const wrappedSources = sourcemaps |
| 81 | + .flatMap(map => map.sourcemap.sources ?? []) |
| 82 | + .filter(source => source.includes('?sentry-auto-wrap')); |
| 83 | +assert.deepEqual(wrappedSources, [], 'Expected no uploaded source to carry the auto-wrap query suffix'); |
| 84 | + |
| 85 | +const uploadedDebugIds = new Set(getDebugIdPairs(bundles).map(pair => pair.debugId.toLowerCase())); |
| 86 | +assert.ok(uploadedDebugIds.size > 0, 'Expected at least one JS/sourcemap pair with matching debug IDs'); |
| 87 | + |
| 88 | +for (const outputDir of [CLIENT_OUTPUT, SERVER_OUTPUT]) { |
| 89 | + const injectedDebugIds = [ |
| 90 | + ...new Set( |
| 91 | + jsFiles(outputDir).flatMap(file => |
| 92 | + [...fs.readFileSync(file, 'utf8').matchAll(/sentry-dbid-([\da-f-]{36})/gi)].map(match => |
| 93 | + (match[1] as string).toLowerCase(), |
| 94 | + ), |
| 95 | + ), |
| 96 | + ), |
| 97 | + ]; |
| 98 | + assert.ok(injectedDebugIds.length > 0, `Expected debug IDs to be injected into ${outputDir}`); |
| 99 | + |
| 100 | + const unuploaded = injectedDebugIds.filter( |
| 101 | + debugId => !uploadedDebugIds.has(debugId) && !debugIdsWithoutSourceMap.has(debugId), |
| 102 | + ); |
| 103 | + assert.deepEqual(unuploaded, [], `Expected every debug ID in ${outputDir} to have an uploaded sourcemap`); |
| 104 | +} |
| 105 | + |
| 106 | +// The app does not set `build.sourcemap`, so the SDK enables hidden maps and must delete them after upload. |
| 107 | +assert.deepEqual( |
| 108 | + listFiles(BUILD_OUTPUT).filter(file => file.endsWith('.map')), |
| 109 | + [], |
| 110 | + `Expected no source maps in ${BUILD_OUTPUT}`, |
| 111 | +); |
| 112 | +assert.deepEqual( |
| 113 | + jsFiles(CLIENT_OUTPUT).filter(file => /[#@]\s*sourceMappingURL=(?!data:)/.test(fs.readFileSync(file, 'utf8'))), |
| 114 | + [], |
| 115 | + `Expected no sourceMappingURL comments in ${CLIENT_OUTPUT}`, |
| 116 | +); |
| 117 | + |
| 118 | +console.log('All sourcemap assertions passed!'); |
0 commit comments