|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +const devServer = { |
| 4 | + url: "http://localhost:8081/", |
| 5 | + fullBundleUrl: "http://localhost:8081/index.bundle", |
| 6 | + bundleLoadedFromServer: true, |
| 7 | +}; |
| 8 | + |
| 9 | +vi.mock("../dev-server", () => ({ |
| 10 | + getDevServer: () => devServer, |
| 11 | +})); |
| 12 | + |
| 13 | +import { symbolicateStackTrace } from "../symbolicate"; |
| 14 | + |
| 15 | +const frame = { |
| 16 | + methodName: "Counter", |
| 17 | + file: "http://localhost:8081/index.bundle", |
| 18 | + lineNumber: 12, |
| 19 | + column: 3, |
| 20 | +}; |
| 21 | + |
| 22 | +beforeEach(() => { |
| 23 | + devServer.bundleLoadedFromServer = true; |
| 24 | +}); |
| 25 | + |
| 26 | +afterEach(() => { |
| 27 | + vi.unstubAllGlobals(); |
| 28 | +}); |
| 29 | + |
| 30 | +describe("symbolicateStackTrace", () => { |
| 31 | + it("posts the stack to Metro and returns the symbolicated result", async () => { |
| 32 | + const symbolicated = { stack: [{ ...frame, file: "src/Counter.tsx" }], codeFrame: null }; |
| 33 | + const fetchMock = vi.fn(async () => ({ |
| 34 | + ok: true, |
| 35 | + status: 200, |
| 36 | + json: async () => symbolicated, |
| 37 | + })); |
| 38 | + vi.stubGlobal("fetch", fetchMock); |
| 39 | + |
| 40 | + await expect(symbolicateStackTrace([frame], { extra: true })).resolves.toEqual(symbolicated); |
| 41 | + |
| 42 | + const [url, init] = fetchMock.mock.calls[0] as unknown as [string, RequestInit]; |
| 43 | + expect(url).toBe("http://localhost:8081/symbolicate"); |
| 44 | + expect(init.method).toBe("POST"); |
| 45 | + expect(JSON.parse(init.body as string)).toEqual({ |
| 46 | + stack: [frame], |
| 47 | + extraData: { extra: true }, |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + it("throws when the bundle was not loaded from Metro", async () => { |
| 52 | + devServer.bundleLoadedFromServer = false; |
| 53 | + const fetchMock = vi.fn(); |
| 54 | + vi.stubGlobal("fetch", fetchMock); |
| 55 | + |
| 56 | + await expect(symbolicateStackTrace([frame])).rejects.toThrow( |
| 57 | + "Bundle was not loaded from Metro", |
| 58 | + ); |
| 59 | + expect(fetchMock).not.toHaveBeenCalled(); |
| 60 | + }); |
| 61 | + |
| 62 | + it("throws when Metro responds with an error status", async () => { |
| 63 | + vi.stubGlobal( |
| 64 | + "fetch", |
| 65 | + vi.fn(async () => ({ ok: false, status: 500, json: async () => ({}) })), |
| 66 | + ); |
| 67 | + |
| 68 | + await expect(symbolicateStackTrace([frame])).rejects.toThrow( |
| 69 | + "Symbolicate request failed with status 500", |
| 70 | + ); |
| 71 | + }); |
| 72 | +}); |
0 commit comments