|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import { describe, it } from "node:test"; |
| 3 | +import { parseStackTrace, symbolicate } from "../pkg/srcmap_symbolicate_wasm.js"; |
| 4 | + |
| 5 | +const SOURCE_MAP = JSON.stringify({ |
| 6 | + version: 3, |
| 7 | + sources: ["src/app.ts"], |
| 8 | + names: ["originalName"], |
| 9 | + mappings: "AAAAA", |
| 10 | +}); |
| 11 | + |
| 12 | +const parseResult = (stack, loader) => JSON.parse(symbolicate(stack, loader)); |
| 13 | + |
| 14 | +describe("parseStackTrace", () => { |
| 15 | + it("loads the generated Node wrapper and returns frame objects", () => { |
| 16 | + const frames = parseStackTrace( |
| 17 | + "Error: test\n at handleClick (bundle.js:10:4)\n at bundle.js:20:8", |
| 18 | + ); |
| 19 | + |
| 20 | + assert.deepEqual(frames, [ |
| 21 | + { |
| 22 | + functionName: "handleClick", |
| 23 | + file: "bundle.js", |
| 24 | + line: 10, |
| 25 | + column: 4, |
| 26 | + }, |
| 27 | + { |
| 28 | + functionName: null, |
| 29 | + file: "bundle.js", |
| 30 | + line: 20, |
| 31 | + column: 8, |
| 32 | + }, |
| 33 | + ]); |
| 34 | + }); |
| 35 | +}); |
| 36 | + |
| 37 | +describe("symbolicate", () => { |
| 38 | + it("resolves a frame and returns the documented output shape", () => { |
| 39 | + const result = parseResult("Error: test\n at compiled (bundle.js:1:1)", (file) => { |
| 40 | + assert.equal(file, "bundle.js"); |
| 41 | + return SOURCE_MAP; |
| 42 | + }); |
| 43 | + |
| 44 | + assert.deepEqual(result, { |
| 45 | + message: "Error: test", |
| 46 | + frames: [ |
| 47 | + { |
| 48 | + functionName: "originalName", |
| 49 | + file: "src/app.ts", |
| 50 | + line: 1, |
| 51 | + column: 1, |
| 52 | + symbolicated: true, |
| 53 | + }, |
| 54 | + ], |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + it("preserves frames when a map is missing", () => { |
| 59 | + const result = parseResult("Error: test\n at compiled (missing.js:2:3)", () => null); |
| 60 | + |
| 61 | + assert.deepEqual(result.frames, [ |
| 62 | + { |
| 63 | + functionName: "compiled", |
| 64 | + file: "missing.js", |
| 65 | + line: 2, |
| 66 | + column: 3, |
| 67 | + symbolicated: false, |
| 68 | + }, |
| 69 | + ]); |
| 70 | + }); |
| 71 | + |
| 72 | + it("treats malformed loader values as missing maps", () => { |
| 73 | + const stack = "Error: test\n at compiled (bundle.js:1:1)"; |
| 74 | + |
| 75 | + for (const value of [42, { version: 3 }, "not json"]) { |
| 76 | + const result = parseResult(stack, () => value); |
| 77 | + assert.equal(result.frames[0].symbolicated, false); |
| 78 | + } |
| 79 | + }); |
| 80 | + |
| 81 | + it("loads each repeated source file once", () => { |
| 82 | + let calls = 0; |
| 83 | + const stack = "Error: test\n at first (bundle.js:1:1)\n at second (bundle.js:1:1)"; |
| 84 | + |
| 85 | + const result = parseResult(stack, () => { |
| 86 | + calls += 1; |
| 87 | + return SOURCE_MAP; |
| 88 | + }); |
| 89 | + |
| 90 | + assert.equal(calls, 1); |
| 91 | + assert.equal(result.frames.length, 2); |
| 92 | + assert.ok(result.frames.every((frame) => frame.symbolicated)); |
| 93 | + }); |
| 94 | +}); |
| 95 | + |
| 96 | +it("exposes the generated web module without browser globals", async () => { |
| 97 | + const module = await import("../web/srcmap_symbolicate_wasm.js"); |
| 98 | + |
| 99 | + assert.equal(typeof module.default, "function"); |
| 100 | + assert.equal(typeof module.parseStackTrace, "function"); |
| 101 | + assert.equal(typeof module.symbolicate, "function"); |
| 102 | +}); |
0 commit comments