|
| 1 | +import { stripVTControlCharacters } from 'node:util'; |
1 | 2 | import type { FastifyBaseLogger } from 'fastify'; |
2 | 3 | import { beforeEach, describe, expect, it, vi } from 'vitest'; |
3 | 4 | import { logSymbolicatedStackFrame } from '../logSymbolicatedStackFrame.js'; |
@@ -50,6 +51,20 @@ function createSourceMapWithoutContent(source: string) { |
50 | 51 | }); |
51 | 52 | } |
52 | 53 |
|
| 54 | +// Maps generated line N to the start of `sources[N - 1]`. |
| 55 | +function createSourceMapWithSources( |
| 56 | + sources: string[], |
| 57 | + sourcesContent?: string[] |
| 58 | +) { |
| 59 | + return JSON.stringify({ |
| 60 | + version: 3, |
| 61 | + sources, |
| 62 | + sourcesContent, |
| 63 | + names: [], |
| 64 | + mappings: ['AAAA', ...sources.slice(1).map(() => 'ACAA')].join(';'), |
| 65 | + }); |
| 66 | +} |
| 67 | + |
53 | 68 | function createDeferred<T>() { |
54 | 69 | let resolve!: (value: T | PromiseLike<T>) => void; |
55 | 70 | const promise = new Promise<T>((resolvePromise) => { |
@@ -188,6 +203,87 @@ describe('Symbolicator', () => { |
188 | 203 | }); |
189 | 204 | }); |
190 | 205 |
|
| 206 | + describe('source names that URLs encode', () => { |
| 207 | + const bundleUrl = 'http://localhost:8081/index.bundle?platform=ios'; |
| 208 | + const sources = [ |
| 209 | + '[projectRoot^2]/node_modules/react-native/Libraries/Core/ExceptionsManager.js', |
| 210 | + '[projectRoot]/src/screens/Home Screen.tsx', |
| 211 | + '[projectRoot]/src/Écran.tsx', |
| 212 | + '[projectRoot]/src/100%25 done.tsx', |
| 213 | + '/Users/me/My Projects/app/src/App.tsx', |
| 214 | + ]; |
| 215 | + const frameFor = (source: string) => ({ |
| 216 | + file: bundleUrl, |
| 217 | + lineNumber: sources.indexOf(source) + 1, |
| 218 | + column: 0, |
| 219 | + methodName: 'frame', |
| 220 | + }); |
| 221 | + |
| 222 | + it.each(sources)('returns %s as emitted by the bundler', async (source) => { |
| 223 | + const symbolicator = new Symbolicator( |
| 224 | + createDelegate(async () => |
| 225 | + createSourceMapWithSources( |
| 226 | + sources, |
| 227 | + sources.map((name) => `// content of ${name}`) |
| 228 | + ) |
| 229 | + ) |
| 230 | + ); |
| 231 | + |
| 232 | + const result = await symbolicator.process(logger, [frameFor(source)]); |
| 233 | + |
| 234 | + expect(result.stack[0]?.file).toBe(source); |
| 235 | + expect(result.codeFrame?.fileName).toBe(source); |
| 236 | + expect( |
| 237 | + stripVTControlCharacters(result.codeFrame?.content ?? '') |
| 238 | + ).toContain(`content of ${source}`); |
| 239 | + }); |
| 240 | + |
| 241 | + it.each(sources)( |
| 242 | + 'reads %s from the compiler when the map has no content', |
| 243 | + async (source) => { |
| 244 | + const getSource = vi.fn(async () => 'const fromDisk = true;'); |
| 245 | + const symbolicator = new Symbolicator( |
| 246 | + createDelegate( |
| 247 | + async () => createSourceMapWithSources(sources), |
| 248 | + getSource |
| 249 | + ) |
| 250 | + ); |
| 251 | + |
| 252 | + const result = await symbolicator.process(logger, [frameFor(source)]); |
| 253 | + |
| 254 | + expect(getSource).toHaveBeenCalledWith(source); |
| 255 | + expect( |
| 256 | + stripVTControlCharacters(result.codeFrame?.content ?? '') |
| 257 | + ).toContain('const fromDisk = true;'); |
| 258 | + } |
| 259 | + ); |
| 260 | + |
| 261 | + it('keeps remote source URLs encoded', async () => { |
| 262 | + // `fetchSourceMapFromBundle` rewrites remote source names to URLs. |
| 263 | + const remoteSource = |
| 264 | + 'http://localhost:8082/__repack_source__/[projectRoot%5E2]/packages/remote/src/App.tsx'; |
| 265 | + const symbolicator = new Symbolicator( |
| 266 | + createDelegate(async () => |
| 267 | + createSourceMapWithSources([remoteSource], ['const remote = 1;']) |
| 268 | + ) |
| 269 | + ); |
| 270 | + |
| 271 | + const result = await symbolicator.process(logger, [ |
| 272 | + { |
| 273 | + file: 'http://localhost:8082/remote.chunk.bundle', |
| 274 | + lineNumber: 1, |
| 275 | + column: 0, |
| 276 | + methodName: 'App', |
| 277 | + }, |
| 278 | + ]); |
| 279 | + |
| 280 | + expect(result.stack[0]?.file).toBe(remoteSource); |
| 281 | + expect( |
| 282 | + stripVTControlCharacters(result.codeFrame?.content ?? '') |
| 283 | + ).toContain('const remote = 1;'); |
| 284 | + }); |
| 285 | + }); |
| 286 | + |
191 | 287 | it('loads a source map once for repeated frames in one request', async () => { |
192 | 288 | const bundleUrl = 'http://localhost:8082/repeated.chunk.bundle'; |
193 | 289 | const getSourceMap = vi.fn(async () => |
@@ -338,6 +434,41 @@ describe('logSymbolicatedStackFrame', () => { |
338 | 434 | }); |
339 | 435 | }); |
340 | 436 |
|
| 437 | + it('strips the placeholder from a frame outside of the project root', async () => { |
| 438 | + const info = vi.fn(); |
| 439 | + const runtimeLogger = { ...logger, info } as unknown as FastifyBaseLogger; |
| 440 | + const stack = [ |
| 441 | + { |
| 442 | + file: 'http://localhost:8081/index.bundle?platform=ios', |
| 443 | + lineNumber: 1, |
| 444 | + column: 0, |
| 445 | + methodName: 'Shared', |
| 446 | + }, |
| 447 | + { |
| 448 | + file: 'http://localhost:8081/index.bundle?platform=ios', |
| 449 | + lineNumber: 200, |
| 450 | + column: 30, |
| 451 | + methodName: 'renderWithHooks', |
| 452 | + }, |
| 453 | + ]; |
| 454 | + const symbolicator = new Symbolicator( |
| 455 | + createDelegate(async () => |
| 456 | + createSourceMapWithSources( |
| 457 | + ['[projectRoot^2]/packages/shared/src/Shared.tsx'], |
| 458 | + ['export const Shared = () => null;'] |
| 459 | + ) |
| 460 | + ) |
| 461 | + ); |
| 462 | + |
| 463 | + const results = await symbolicator.process(runtimeLogger, stack); |
| 464 | + logSymbolicatedStackFrame(runtimeLogger, stack, results); |
| 465 | + |
| 466 | + expect(info).toHaveBeenCalledWith({ |
| 467 | + msg: 'Symbolicated stack frame: packages/shared/src/Shared.tsx:1:0', |
| 468 | + methodName: 'Shared', |
| 469 | + }); |
| 470 | + }); |
| 471 | + |
341 | 472 | it('does not log component-stack-only symbolication', () => { |
342 | 473 | const info = vi.fn(); |
343 | 474 | const runtimeLogger = { info } as unknown as FastifyBaseLogger; |
|
0 commit comments