forked from getsentry/sentry-javascript-bundler-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresolve-source-maps.test.ts
More file actions
164 lines (147 loc) · 5.38 KB
/
resolve-source-maps.test.ts
File metadata and controls
164 lines (147 loc) · 5.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import * as path from "path";
import * as fs from "fs";
import * as url from "url";
import { determineSourceMapPathFromBundle } from "../../src/debug-id-upload";
import { createLogger } from "../../src/logger";
const logger = createLogger({ prefix: "[resolve-source-maps-test]", silent: false, debug: false });
const fixtureDir = path.resolve(__dirname, "../fixtures/resolve-source-maps");
const adjacentBundlePath = path.join(fixtureDir, "adjacent-sourcemap/index.js");
const adjacentSourceMapPath = path.join(fixtureDir, "adjacent-sourcemap/index.js.map");
const adjacentBundleContent = fs.readFileSync(adjacentBundlePath, "utf-8");
const separateBundlePath = path.join(fixtureDir, "separate-directory/bundles/index.js");
const separateSourceMapPath = path.join(fixtureDir, "separate-directory/sourcemaps/index.js.map");
const separateBundleContent = fs.readFileSync(separateBundlePath, "utf-8");
const sourceMapUrl = "https://sourcemaps.example.com/foo/index.js.map"
function srcMappingUrl(url: string): string {
return `\n//# sourceMappingURL=${url}`
}
describe("Resolve source maps", () => {
it("should resolve source maps next to bundles", async () => {
expect(
await determineSourceMapPathFromBundle(
adjacentBundlePath,
adjacentBundleContent,
logger,
undefined
)
).toEqual(adjacentSourceMapPath);
});
it("shouldn't resolve source maps in separate directories", async () => {
expect(
await determineSourceMapPathFromBundle(
separateBundlePath,
separateBundleContent,
logger,
undefined
)
).toBeUndefined();
});
describe("sourceMappingURL resolution", () => {
it("should resolve source maps when sourceMappingURL is a file URL", async () => {
expect(
await determineSourceMapPathFromBundle(
separateBundlePath,
separateBundleContent + srcMappingUrl(url.pathToFileURL(separateSourceMapPath).href),
logger,
undefined
)
).toEqual(separateSourceMapPath);
});
it("shouldn't resolve source maps when sourceMappingURL is a non-file URL", async () => {
expect(
await determineSourceMapPathFromBundle(
separateBundlePath,
separateBundleContent + srcMappingUrl(sourceMapUrl),
logger,
undefined
)
).toBeUndefined();
});
it("should resolve source maps when sourceMappingURL is an absolute path", async () => {
expect(
await determineSourceMapPathFromBundle(
separateBundlePath,
separateBundleContent + srcMappingUrl(separateSourceMapPath),
logger,
undefined
)
).toEqual(separateSourceMapPath);
});
it("should resolve source maps when sourceMappingURL is a relative path", async () => {
expect(
await determineSourceMapPathFromBundle(
separateBundlePath,
separateBundleContent + srcMappingUrl(path.relative(path.dirname(separateBundlePath), separateSourceMapPath)),
logger,
undefined
)
).toEqual(separateSourceMapPath);
});
});
describe("resolveSourceMap hook", () => {
it("should resolve source maps when a resolveSourceMap hook is provided", async () => {
expect(
await determineSourceMapPathFromBundle(
separateBundlePath,
separateBundleContent + srcMappingUrl(sourceMapUrl),
logger,
() => separateSourceMapPath
)
).toEqual(separateSourceMapPath);
});
it("should pass the correct values to the resolveSourceMap hook", async () => {
const hook = jest.fn(() => separateSourceMapPath)
expect(
await determineSourceMapPathFromBundle(
separateBundlePath,
separateBundleContent + srcMappingUrl(sourceMapUrl),
logger,
hook
)
).toEqual(separateSourceMapPath);
expect(hook.mock.calls[0]).toEqual([separateBundlePath, sourceMapUrl])
});
it("should pass the correct values to the resolveSourceMap hook when no sourceMappingURL is present", async () => {
const hook = jest.fn(() => separateSourceMapPath)
expect(
await determineSourceMapPathFromBundle(
separateBundlePath,
separateBundleContent,
logger,
hook
)
).toEqual(separateSourceMapPath);
expect(hook.mock.calls[0]).toEqual([separateBundlePath, undefined])
});
it("should prefer resolveSourceMap result over heuristic results", async () => {
expect(
await determineSourceMapPathFromBundle(
adjacentBundlePath,
adjacentBundleContent,
logger,
() => separateSourceMapPath
)
).toEqual(separateSourceMapPath);
});
it("should fall back when the resolveSourceMap hook returns undefined", async () => {
expect(
await determineSourceMapPathFromBundle(
adjacentBundlePath,
adjacentBundleContent,
logger,
() => undefined
)
).toEqual(adjacentSourceMapPath);
});
it("should fall back when the resolveSourceMap hook returns a non-existent path", async () => {
expect(
await determineSourceMapPathFromBundle(
adjacentBundlePath,
adjacentBundleContent,
logger,
() => path.join(fixtureDir, "non-existent.js.map")
)
).toEqual(adjacentSourceMapPath);
});
});
});