-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathlegacy-sourcemaps.test.ts
More file actions
266 lines (221 loc) · 8.86 KB
/
legacy-sourcemaps.test.ts
File metadata and controls
266 lines (221 loc) · 8.86 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import { createSentryBuildPluginManager } from "../src/build-plugin-manager";
import SentryCli from "@sentry/cli";
import * as fs from "fs";
import * as path from "path";
import * as os from "os";
jest.mock("@sentry/cli");
const MockedSentryCli = SentryCli as jest.MockedClass<typeof SentryCli>;
interface MockSentryCliInstance {
releases: {
new: jest.MockedFunction<(release: string) => Promise<string>>;
uploadSourceMaps: jest.MockedFunction<
(
release: string,
options: {
include: Array<{
paths: string[];
ext: string[];
validate: boolean;
ignore?: string[];
}>;
dist?: string;
}
) => Promise<string>
>;
setCommits: jest.MockedFunction<(release: string, options: unknown) => Promise<string>>;
finalize: jest.MockedFunction<(release: string, options?: unknown) => Promise<string>>;
newDeploy: jest.MockedFunction<(release: string, options: unknown) => Promise<string>>;
proposeVersion: jest.MockedFunction<() => Promise<string>>;
listDeploys: jest.MockedFunction<(release: string, options?: unknown) => Promise<string>>;
execute: jest.MockedFunction<(args: string[], live?: boolean) => Promise<string>>;
};
}
describe("uploadLegacySourcemaps glob expansion", () => {
let tempDir: string;
let mockCliInstance: MockSentryCliInstance;
beforeEach(async () => {
jest.clearAllMocks();
tempDir = await fs.promises.mkdtemp(path.join(os.tmpdir(), "sentry-test-"));
mockCliInstance = {
releases: {
new: jest.fn().mockResolvedValue(""),
uploadSourceMaps: jest.fn().mockResolvedValue(""),
setCommits: jest.fn().mockResolvedValue(""),
finalize: jest.fn().mockResolvedValue(""),
newDeploy: jest.fn().mockResolvedValue(""),
proposeVersion: jest.fn().mockResolvedValue(""),
listDeploys: jest.fn().mockResolvedValue(""),
execute: jest.fn().mockResolvedValue(""),
},
};
MockedSentryCli.mockImplementation(() => mockCliInstance as unknown as SentryCli);
const testFiles = ["dist/beep.js", "dist/boop.js"];
for (const file of testFiles) {
const fullPath = path.join(tempDir, file);
await fs.promises.mkdir(path.dirname(fullPath), { recursive: true });
await fs.promises.writeFile(fullPath, "test content");
}
});
afterEach(async () => {
await fs.promises.rm(tempDir, { recursive: true, force: true });
});
it("should expand a basic glob pattern", async () => {
const buildPluginManager = createSentryBuildPluginManager(
{
org: "test-org",
project: "test-project",
authToken: "test-token",
release: {
name: "test-release",
uploadLegacySourcemaps: path.join(tempDir, "dist/*.js"),
},
},
{
buildTool: "webpack",
loggerPrefix: "[sentry-webpack-plugin]",
}
);
await buildPluginManager.createRelease();
expect(mockCliInstance.releases.uploadSourceMaps).toHaveBeenCalledTimes(1);
const uploadCalls = mockCliInstance.releases.uploadSourceMaps.mock.calls;
expect(uploadCalls).toHaveLength(1);
const uploadCall = uploadCalls[0];
expect(uploadCall).toHaveLength(2);
// TS yelling at me
if (!uploadCall) {
throw new Error("uploadCall should be defined");
}
const uploadOptions = uploadCall[1];
const firstInclude = uploadOptions.include[0];
if (!firstInclude) {
throw new Error("firstInclude should be defined");
}
const includePaths = firstInclude.paths;
expect(includePaths).toHaveLength(2);
expect(includePaths.some((p: string) => p.includes("beep.js"))).toBe(true);
expect(includePaths.some((p: string) => p.includes("boop.js"))).toBe(true);
});
it("should expand multiple glob patterns and mixed path types", async () => {
const additionalFiles = [
"src/app.js",
"src/app.js.map",
"dist/vendor.bundle",
"dist/vendor.bundle.map",
"assets/styles.css",
"static/image.png",
];
for (const file of additionalFiles) {
const fullPath = path.join(tempDir, file);
await fs.promises.mkdir(path.dirname(fullPath), { recursive: true });
await fs.promises.writeFile(fullPath, "additional content");
}
const buildPluginManager = createSentryBuildPluginManager(
{
org: "test-org",
project: "test-project",
authToken: "test-token",
release: {
name: "test-release",
uploadLegacySourcemaps: [
path.join(tempDir, "dist/*"),
path.join(tempDir, "src", "app.js"),
path.join(tempDir, "assets/*"),
],
},
},
{
buildTool: "webpack",
loggerPrefix: "[sentry-webpack-plugin]",
}
);
await buildPluginManager.createRelease();
expect(mockCliInstance.releases.uploadSourceMaps).toHaveBeenCalledTimes(1);
const uploadCalls = mockCliInstance.releases.uploadSourceMaps.mock.calls;
expect(uploadCalls).toHaveLength(1);
const uploadCall = uploadCalls[0];
expect(uploadCall).toHaveLength(2);
if (!uploadCall) {
throw new Error("uploadCall should be defined");
}
const uploadOptions = uploadCall[1];
expect(uploadOptions.include).toHaveLength(3);
// Should match all files in dist/
const firstInclude = uploadOptions.include[0];
if (!firstInclude) {
throw new Error("firstInclude should be defined");
}
const firstIncludePaths = firstInclude.paths;
expect(firstIncludePaths).toHaveLength(4); // beep.js, boop.js, vendor.bundle, vendor.bundle.map
expect(firstIncludePaths.some((p: string) => p.includes("beep.js"))).toBe(true);
expect(firstIncludePaths.some((p: string) => p.includes("boop.js"))).toBe(true);
expect(firstIncludePaths.some((p: string) => p.includes("vendor.bundle"))).toBe(true);
expect(firstIncludePaths.some((p: string) => p.includes("vendor.bundle.map"))).toBe(true);
// Should match the specific file
const secondInclude = uploadOptions.include[1];
if (!secondInclude) {
throw new Error("secondInclude should be defined");
}
const secondIncludePaths = secondInclude.paths;
expect(secondIncludePaths).toHaveLength(1);
expect(secondIncludePaths[0]).toContain("app.js");
// Should match files in assets/
const thirdInclude = uploadOptions.include[2];
if (!thirdInclude) {
throw new Error("thirdInclude should be defined");
}
const thirdIncludePaths = thirdInclude.paths;
expect(thirdIncludePaths).toHaveLength(1);
expect(thirdIncludePaths[0]).toContain("styles.css");
// Should have the correct default extension filter
expect(firstInclude.ext).toEqual([".js", ".map", ".jsbundle", ".bundle"]);
expect(secondInclude.ext).toEqual([".js", ".map", ".jsbundle", ".bundle"]);
expect(thirdInclude.ext).toEqual([".js", ".map", ".jsbundle", ".bundle"]);
});
it("should apply custom file extensions when specified", async () => {
const testFiles = ["dist/main.js", "dist/main.js.map", "dist/styles.css", "dist/data.json"];
for (const file of testFiles) {
const fullPath = path.join(tempDir, file);
await fs.promises.mkdir(path.dirname(fullPath), { recursive: true });
await fs.promises.writeFile(fullPath, "test content");
}
const buildPluginManager = createSentryBuildPluginManager(
{
org: "test-org",
project: "test-project",
authToken: "test-token",
release: {
name: "test-release",
uploadLegacySourcemaps: {
paths: [path.join(tempDir, "dist/*")],
ext: ["js", "css"],
},
},
},
{
buildTool: "webpack",
loggerPrefix: "[sentry-webpack-plugin]",
}
);
await buildPluginManager.createRelease();
expect(mockCliInstance.releases.uploadSourceMaps).toHaveBeenCalledTimes(1);
const uploadCalls = mockCliInstance.releases.uploadSourceMaps.mock.calls;
const uploadCall = uploadCalls[0];
if (!uploadCall) {
throw new Error("uploadCall should be defined");
}
const uploadOptions = uploadCall[1];
const firstInclude = uploadOptions.include[0];
if (!firstInclude) {
throw new Error("firstInclude should be defined");
}
const includePaths = firstInclude.paths;
expect(includePaths).toHaveLength(6);
expect(includePaths.some((p: string) => p.includes("main.js"))).toBe(true);
expect(includePaths.some((p: string) => p.includes("main.js.map"))).toBe(true);
expect(includePaths.some((p: string) => p.includes("styles.css"))).toBe(true);
expect(includePaths.some((p: string) => p.includes("data.json"))).toBe(true);
expect(includePaths.some((p: string) => p.includes("beep.js"))).toBe(true);
expect(includePaths.some((p: string) => p.includes("boop.js"))).toBe(true);
expect(firstInclude.ext).toEqual([".js", ".css"]); // Custom extensions added
});
});