|
| 1 | +import * as fs from 'fs'; |
| 2 | +import type { MixedOutput , Module } from 'metro'; |
| 3 | +import CountingSet from 'metro/src/lib/CountingSet'; |
| 4 | +import * as countLines from 'metro/src/lib/countLines'; |
| 5 | + |
| 6 | +import type { MetroSerializer, VirtualJSOutput } from '../../src/js/tools/sentryMetroSerializer'; |
| 7 | +import { createSentryMetroSerializer } from '../../src/js/tools/sentryMetroSerializer'; |
| 8 | + |
| 9 | +describe('Sentry Metro Serializer', () => { |
| 10 | + |
| 11 | + test('generates bundle and source map with deterministic uuidv5 debug id', async () => { |
| 12 | + const serializer = createSentryMetroSerializer(); |
| 13 | + |
| 14 | + const bundle = await serializer(...mockMinSerializerArgs()); |
| 15 | + if (typeof bundle === 'string') { |
| 16 | + fail('Expected bundle to be an object with a "code" property'); |
| 17 | + } |
| 18 | + |
| 19 | + expect(bundle.code) |
| 20 | + .toEqual('var _sentryDebugIds={},_sentryDebugIdIdentifier="";try{var e=Error().stack;e&&(_sentryDebugIds[e]="4a945039-d159-4021-938a-83830ce8127c",_sentryDebugIdIdentifier="sentry-dbid-4a945039-d159-4021-938a-83830ce8127c")}catch(r){}\n//# debugId=4a945039-d159-4021-938a-83830ce8127c'); |
| 21 | + expect(bundle.map) |
| 22 | + .toEqual('{"debug_id":"4a945039-d159-4021-938a-83830ce8127c","debugId":"4a945039-d159-4021-938a-83830ce8127c"}'); |
| 23 | + }); |
| 24 | + |
| 25 | + test('generated debug id is uuid v4 format', async () => { |
| 26 | + const serializer = createSentryMetroSerializer(); |
| 27 | + const bundle = await serializer(...mockMinSerializerArgs()); |
| 28 | + const debugId = determineDebugIdFromBundleSource(typeof bundle === 'string' ? bundle : bundle.code); |
| 29 | + expect(debugId).toEqual('4a945039-d159-4021-938a-83830ce8127c'); |
| 30 | + }); |
| 31 | + |
| 32 | + test('adds debug id snipped after prelude module and before ', async () => { |
| 33 | + const serializer = createSentryMetroSerializer(); |
| 34 | + |
| 35 | + const bundle = await serializer(...mockWithPreludAndDepsSerializerArgs()); |
| 36 | + if (typeof bundle === 'string') { |
| 37 | + fail('Expected bundle to be an object with a "code" property'); |
| 38 | + } |
| 39 | + |
| 40 | + expect(bundle.code).toEqual(fs.readFileSync(`${__dirname}/fixtures/bundleWithPrelude.js.fixture`, 'utf8')); |
| 41 | + expect(bundle.map).toEqual(fs.readFileSync(`${__dirname}/fixtures/bundleWithPrelude.js.fixture.map`, 'utf8')); |
| 42 | + }); |
| 43 | +}); |
| 44 | + |
| 45 | +function mockMinSerializerArgs(): Parameters<MetroSerializer> { |
| 46 | + let modulesCounter = 0; |
| 47 | + return [ |
| 48 | + 'index.js', |
| 49 | + [], |
| 50 | + { |
| 51 | + entryPoints: new Set(), |
| 52 | + dependencies: new Map(), |
| 53 | + transformOptions: { |
| 54 | + hot: false, |
| 55 | + dev: false, |
| 56 | + minify: false, |
| 57 | + type: 'script', |
| 58 | + unstable_transformProfile: 'hermes-stable', |
| 59 | + } |
| 60 | + }, |
| 61 | + { |
| 62 | + asyncRequireModulePath: 'asyncRequire', |
| 63 | + createModuleId: (_filePath: string): number => modulesCounter++, |
| 64 | + dev: false, |
| 65 | + getRunModuleStatement: (_moduleId: string | number): string => '', |
| 66 | + includeAsyncPaths: false, |
| 67 | + modulesOnly: false, |
| 68 | + processModuleFilter: (_module: Module<MixedOutput>) => true, |
| 69 | + projectRoot: '/project/root', |
| 70 | + runBeforeMainModule: [], |
| 71 | + runModule: false, |
| 72 | + serverRoot: '/server/root', |
| 73 | + shouldAddToIgnoreList: (_module: Module<MixedOutput>) => false, |
| 74 | + }, |
| 75 | + ]; |
| 76 | +} |
| 77 | + |
| 78 | +function mockWithPreludAndDepsSerializerArgs(): Parameters<MetroSerializer> { |
| 79 | + const mockPreludeCode = '__mock_prelude__'; |
| 80 | + const indexJsCode = '__mock_index_js__'; |
| 81 | + const args = mockMinSerializerArgs(); |
| 82 | + args[1] = [ |
| 83 | + { |
| 84 | + dependencies: new Map(), |
| 85 | + getSource: () => Buffer.from(mockPreludeCode), |
| 86 | + inverseDependencies: new CountingSet(), |
| 87 | + path: '__prelude__', |
| 88 | + output: [ |
| 89 | + <VirtualJSOutput> { |
| 90 | + type: 'js/script/virtual', |
| 91 | + data: { |
| 92 | + code: mockPreludeCode, |
| 93 | + lineCount: countLines(indexJsCode), |
| 94 | + map: [], |
| 95 | + }, |
| 96 | + }, |
| 97 | + ], |
| 98 | + }, |
| 99 | + ]; |
| 100 | + |
| 101 | + // @ts-expect-error - This is a mock |
| 102 | + args[2].dependencies = <Parameters<MetroSerializer>[2]['dependencies']>new Map([ |
| 103 | + [ |
| 104 | + 'index.js', |
| 105 | + <Module<VirtualJSOutput>> { |
| 106 | + dependencies: new Map(), |
| 107 | + getSource: () => Buffer.from(indexJsCode), |
| 108 | + inverseDependencies: new CountingSet(), |
| 109 | + path: 'index.js', |
| 110 | + output: [ |
| 111 | + { |
| 112 | + type: 'js/script/virtual', |
| 113 | + data: { |
| 114 | + code: indexJsCode, |
| 115 | + lineCount: countLines(indexJsCode), |
| 116 | + map: [], |
| 117 | + }, |
| 118 | + }, |
| 119 | + ], |
| 120 | + }, |
| 121 | + ] |
| 122 | + ]); |
| 123 | + |
| 124 | + return args; |
| 125 | +} |
| 126 | + |
| 127 | +/** |
| 128 | + * This function is on purpose not shared with the actual implementation. |
| 129 | + */ |
| 130 | +function determineDebugIdFromBundleSource(code: string): string | undefined { |
| 131 | + const match = code.match( |
| 132 | + /sentry-dbid-([0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12})/, |
| 133 | + ); |
| 134 | + |
| 135 | + if (match) { |
| 136 | + return match[1]; |
| 137 | + } else { |
| 138 | + return undefined; |
| 139 | + } |
| 140 | +} |
0 commit comments