/
sentryWebpackPlugin.test.ts
489 lines (421 loc) · 18.3 KB
/
sentryWebpackPlugin.test.ts
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import { BuildContext, ExportedNextConfig } from '../../../src/config/types';
import { getUserConfigFile, getWebpackPluginOptions, SentryWebpackPlugin } from '../../../src/config/webpack';
import {
clientBuildContext,
clientWebpackConfig,
exportedNextConfig,
getBuildContext,
serverBuildContext,
serverWebpackConfig,
userSentryWebpackPluginConfig,
} from '../fixtures';
import { exitsSync, mkdtempSyncSpy, mockExistsSync, realExistsSync } from '../mocks';
import { findWebpackPlugin, materializeFinalWebpackConfig } from '../testUtils';
describe('Sentry webpack plugin config', () => {
it('includes expected properties', async () => {
// also, can pull from either env or user config (see notes on specific properties below)
const finalWebpackConfig = await materializeFinalWebpackConfig({
exportedNextConfig,
userSentryWebpackPluginConfig,
incomingWebpackConfig: serverWebpackConfig,
incomingWebpackBuildContext: serverBuildContext,
});
const sentryWebpackPluginInstance = findWebpackPlugin(finalWebpackConfig, 'SentryCliPlugin') as SentryWebpackPlugin;
expect(sentryWebpackPluginInstance.options).toEqual(
expect.objectContaining({
include: expect.any(Array), // default, tested separately elsewhere
ignore: [], // default
org: 'squirrelChasers', // from user webpack plugin config
project: 'simulator', // from user webpack plugin config
authToken: 'dogsarebadatkeepingsecrets', // picked up from env
stripPrefix: ['webpack://_N_E/'], // default
urlPrefix: '~/_next', // default
entries: expect.any(Function), // default, tested separately elsewhere
release: 'doGsaREgReaT', // picked up from env
dryRun: false, // based on buildContext.dev being false
}),
);
});
it('preserves unrelated plugin config options', async () => {
const finalWebpackConfig = await materializeFinalWebpackConfig({
exportedNextConfig,
userSentryWebpackPluginConfig: { ...userSentryWebpackPluginConfig, debug: true },
incomingWebpackConfig: serverWebpackConfig,
incomingWebpackBuildContext: serverBuildContext,
});
const sentryWebpackPluginInstance = findWebpackPlugin(finalWebpackConfig, 'SentryCliPlugin') as SentryWebpackPlugin;
expect(sentryWebpackPluginInstance.options.debug).toEqual(true);
});
it('warns when overriding certain default values', () => {
// TODO
});
it("merges default include and ignore/ignoreFile options with user's values", () => {
// do we even want to do this?
});
describe('Sentry webpack plugin `include` option', () => {
it('has the correct value when building client bundles', async () => {
const finalWebpackConfig = await materializeFinalWebpackConfig({
exportedNextConfig,
incomingWebpackConfig: clientWebpackConfig,
incomingWebpackBuildContext: clientBuildContext,
});
const sentryWebpackPluginInstance = findWebpackPlugin(
finalWebpackConfig,
'SentryCliPlugin',
) as SentryWebpackPlugin;
expect(sentryWebpackPluginInstance.options.include).toEqual([
{ paths: [`${clientBuildContext.dir}/.next/static/chunks/pages`], urlPrefix: '~/_next/static/chunks/pages' },
]);
});
it('has the correct value when building client bundles using `widenClientFileUpload` option', async () => {
const exportedNextConfigWithWidening = { ...exportedNextConfig, sentry: { widenClientFileUpload: true } };
const buildContext = getBuildContext('client', exportedNextConfigWithWidening);
const finalWebpackConfig = await materializeFinalWebpackConfig({
exportedNextConfig: exportedNextConfigWithWidening,
incomingWebpackConfig: clientWebpackConfig,
incomingWebpackBuildContext: buildContext,
});
const sentryWebpackPluginInstance = findWebpackPlugin(
finalWebpackConfig,
'SentryCliPlugin',
) as SentryWebpackPlugin;
expect(sentryWebpackPluginInstance.options.include).toEqual([
{ paths: [`${buildContext.dir}/.next/static/chunks`], urlPrefix: '~/_next/static/chunks' },
]);
});
it('has the correct value when building serverless server bundles', async () => {
const exportedNextConfigServerless = {
...exportedNextConfig,
target: 'experimental-serverless-trace' as const,
};
const buildContext = getBuildContext('server', exportedNextConfigServerless);
const finalWebpackConfig = await materializeFinalWebpackConfig({
exportedNextConfig: exportedNextConfigServerless,
incomingWebpackConfig: serverWebpackConfig,
incomingWebpackBuildContext: buildContext,
});
const sentryWebpackPluginInstance = findWebpackPlugin(
finalWebpackConfig,
'SentryCliPlugin',
) as SentryWebpackPlugin;
expect(sentryWebpackPluginInstance.options.include).toEqual([
{ paths: [`${buildContext.dir}/.next/serverless/`], urlPrefix: '~/_next/serverless' },
]);
});
it('has the correct value when building serverful server bundles using webpack 4', async () => {
const serverBuildContextWebpack4 = getBuildContext('server', exportedNextConfig);
serverBuildContextWebpack4.webpack.version = '4.15.13';
const finalWebpackConfig = await materializeFinalWebpackConfig({
exportedNextConfig,
incomingWebpackConfig: serverWebpackConfig,
incomingWebpackBuildContext: serverBuildContextWebpack4,
});
const sentryWebpackPluginInstance = findWebpackPlugin(
finalWebpackConfig,
'SentryCliPlugin',
) as SentryWebpackPlugin;
expect(sentryWebpackPluginInstance.options.include).toEqual([
{ paths: [`${serverBuildContextWebpack4.dir}/.next/server/pages/`], urlPrefix: '~/_next/server/pages' },
]);
});
it('has the correct value when building serverful server bundles using webpack 5', async () => {
const finalWebpackConfig = await materializeFinalWebpackConfig({
exportedNextConfig,
incomingWebpackConfig: serverWebpackConfig,
incomingWebpackBuildContext: serverBuildContext,
});
const sentryWebpackPluginInstance = findWebpackPlugin(
finalWebpackConfig,
'SentryCliPlugin',
) as SentryWebpackPlugin;
expect(sentryWebpackPluginInstance.options.include).toEqual([
{ paths: [`${serverBuildContext.dir}/.next/server/pages/`], urlPrefix: '~/_next/server/pages' },
{ paths: [`${serverBuildContext.dir}/.next/server/chunks/`], urlPrefix: '~/_next/server/chunks' },
]);
});
});
describe('Sentry webpack plugin `ignore` option', () => {
it('has the correct value when building client bundles', async () => {
const finalWebpackConfig = await materializeFinalWebpackConfig({
exportedNextConfig,
incomingWebpackConfig: clientWebpackConfig,
incomingWebpackBuildContext: clientBuildContext,
});
const sentryWebpackPluginInstance = findWebpackPlugin(
finalWebpackConfig,
'SentryCliPlugin',
) as SentryWebpackPlugin;
expect(sentryWebpackPluginInstance.options.ignore).toEqual([]);
});
it('has the correct value when building client bundles using `widenClientFileUpload` option', async () => {
const exportedNextConfigWithWidening = { ...exportedNextConfig, sentry: { widenClientFileUpload: true } };
const finalWebpackConfig = await materializeFinalWebpackConfig({
exportedNextConfig: exportedNextConfigWithWidening,
incomingWebpackConfig: clientWebpackConfig,
incomingWebpackBuildContext: getBuildContext('client', exportedNextConfigWithWidening),
});
const sentryWebpackPluginInstance = findWebpackPlugin(
finalWebpackConfig,
'SentryCliPlugin',
) as SentryWebpackPlugin;
expect(sentryWebpackPluginInstance.options.ignore).toEqual([
'framework-*',
'framework.*',
'main-*',
'polyfills-*',
'webpack-*',
]);
});
});
describe("Sentry webpack plugin `include` option with basePath filled on next's config", () => {
const exportedNextConfigWithBasePath = {
...exportedNextConfig,
basePath: '/city-park',
};
it('has the correct value when building client bundles', async () => {
const buildContext = getBuildContext('client', exportedNextConfigWithBasePath);
const finalWebpackConfig = await materializeFinalWebpackConfig({
exportedNextConfig: exportedNextConfigWithBasePath,
incomingWebpackConfig: clientWebpackConfig,
incomingWebpackBuildContext: buildContext,
});
const sentryWebpackPluginInstance = findWebpackPlugin(
finalWebpackConfig,
'SentryCliPlugin',
) as SentryWebpackPlugin;
expect(sentryWebpackPluginInstance.options.include).toEqual([
{
paths: [`${buildContext.dir}/.next/static/chunks/pages`],
urlPrefix: '~/city-park/_next/static/chunks/pages',
},
]);
});
it('has the correct value when building serverless server bundles', async () => {
const exportedNextConfigServerless = {
...exportedNextConfigWithBasePath,
target: 'experimental-serverless-trace' as const,
};
const buildContext = getBuildContext('server', exportedNextConfigServerless);
const finalWebpackConfig = await materializeFinalWebpackConfig({
exportedNextConfig: exportedNextConfigServerless,
incomingWebpackConfig: serverWebpackConfig,
incomingWebpackBuildContext: buildContext,
});
const sentryWebpackPluginInstance = findWebpackPlugin(
finalWebpackConfig,
'SentryCliPlugin',
) as SentryWebpackPlugin;
expect(sentryWebpackPluginInstance.options.include).toEqual([
{ paths: [`${buildContext.dir}/.next/serverless/`], urlPrefix: '~/city-park/_next/serverless' },
]);
});
it('has the correct value when building serverful server bundles using webpack 4', async () => {
const serverBuildContextWebpack4 = getBuildContext('server', exportedNextConfigWithBasePath);
serverBuildContextWebpack4.webpack.version = '4.15.13';
const finalWebpackConfig = await materializeFinalWebpackConfig({
exportedNextConfig: exportedNextConfigWithBasePath,
incomingWebpackConfig: serverWebpackConfig,
incomingWebpackBuildContext: serverBuildContextWebpack4,
});
const sentryWebpackPluginInstance = findWebpackPlugin(
finalWebpackConfig,
'SentryCliPlugin',
) as SentryWebpackPlugin;
expect(sentryWebpackPluginInstance.options.include).toEqual([
{
paths: [`${serverBuildContextWebpack4.dir}/.next/server/pages/`],
urlPrefix: '~/city-park/_next/server/pages',
},
]);
});
it('has the correct value when building serverful server bundles using webpack 5', async () => {
const buildContext = getBuildContext('server', exportedNextConfigWithBasePath);
const finalWebpackConfig = await materializeFinalWebpackConfig({
exportedNextConfig: exportedNextConfigWithBasePath,
incomingWebpackConfig: serverWebpackConfig,
incomingWebpackBuildContext: buildContext,
});
const sentryWebpackPluginInstance = findWebpackPlugin(
finalWebpackConfig,
'SentryCliPlugin',
) as SentryWebpackPlugin;
expect(sentryWebpackPluginInstance.options.include).toEqual([
{ paths: [`${buildContext.dir}/.next/server/pages/`], urlPrefix: '~/city-park/_next/server/pages' },
{ paths: [`${buildContext.dir}/.next/server/chunks/`], urlPrefix: '~/city-park/_next/server/chunks' },
]);
});
});
describe('SentryWebpackPlugin enablement', () => {
let processEnvBackup: typeof process.env;
beforeEach(() => {
processEnvBackup = { ...process.env };
});
afterEach(() => {
process.env = processEnvBackup;
});
it.each([
// [testName, exportedNextConfig, extraEnvValues, shouldFindServerPlugin, shouldFindClientPlugin]
[
'obeys `disableClientWebpackPlugin = true`',
{
...exportedNextConfig,
sentry: { disableClientWebpackPlugin: true },
},
{},
true,
false,
],
[
'obeys `disableServerWebpackPlugin = true`',
{
...exportedNextConfig,
sentry: { disableServerWebpackPlugin: true },
},
{},
false,
true,
],
[
'disables the plugin in Vercel `preview` environment',
exportedNextConfig,
{ VERCEL_ENV: 'preview' },
false,
false,
],
[
'disables the plugin in Vercel `development` environment',
exportedNextConfig,
{ VERCEL_ENV: 'development' },
false,
false,
],
[
'allows `disableClientWebpackPlugin = false` to override env vars`',
{
...exportedNextConfig,
sentry: { disableClientWebpackPlugin: false },
},
{ VERCEL_ENV: 'preview' },
false,
true,
],
[
'allows `disableServerWebpackPlugin = false` to override env vars`',
{
...exportedNextConfig,
sentry: { disableServerWebpackPlugin: false },
},
{ VERCEL_ENV: 'preview' },
true,
false,
],
])(
'%s',
async (
_testName: string,
exportedNextConfig: ExportedNextConfig,
extraEnvValues: Record<string, string>,
shouldFindServerPlugin: boolean,
shouldFindClientPlugin: boolean,
) => {
process.env = { ...process.env, ...extraEnvValues };
// We create a copy of the next config for each `materializeFinalWebpackConfig` call because the `sentry`
// property gets deleted along the way, and its value matters for some of our test cases
const serverFinalWebpackConfig = await materializeFinalWebpackConfig({
exportedNextConfig: { ...exportedNextConfig },
userSentryWebpackPluginConfig,
incomingWebpackConfig: serverWebpackConfig,
incomingWebpackBuildContext: serverBuildContext,
});
const clientFinalWebpackConfig = await materializeFinalWebpackConfig({
exportedNextConfig: { ...exportedNextConfig },
userSentryWebpackPluginConfig,
incomingWebpackConfig: clientWebpackConfig,
incomingWebpackBuildContext: clientBuildContext,
});
const genericSentryWebpackPluginInstance = expect.any(SentryWebpackPlugin);
expect(findWebpackPlugin(serverFinalWebpackConfig, 'SentryCliPlugin')).toEqual(
shouldFindServerPlugin ? genericSentryWebpackPluginInstance : undefined,
);
expect(findWebpackPlugin(clientFinalWebpackConfig, 'SentryCliPlugin')).toEqual(
shouldFindClientPlugin ? genericSentryWebpackPluginInstance : undefined,
);
},
);
});
describe('getUserConfigFile', () => {
let tempDir: string;
beforeAll(() => {
exitsSync.mockImplementation(realExistsSync);
});
beforeEach(() => {
// these will get cleaned up by the file's overall `afterAll` function, and the `mkdtempSync` mock above ensures
// that the location of the created folder is stored in `tempDir`
const tempDirPathPrefix = path.join(os.tmpdir(), 'sentry-nextjs-test-');
fs.mkdtempSync(tempDirPathPrefix);
tempDir = mkdtempSyncSpy.mock.results[0].value;
});
afterAll(() => {
exitsSync.mockImplementation(mockExistsSync);
});
it('successfully finds js files', () => {
fs.writeFileSync(path.resolve(tempDir, 'sentry.server.config.js'), 'Dogs are great!');
fs.writeFileSync(path.resolve(tempDir, 'sentry.client.config.js'), 'Squirrel!');
expect(getUserConfigFile(tempDir, 'server')).toEqual('sentry.server.config.js');
expect(getUserConfigFile(tempDir, 'client')).toEqual('sentry.client.config.js');
});
it('successfully finds ts files', () => {
fs.writeFileSync(path.resolve(tempDir, 'sentry.server.config.ts'), 'Sit. Stay. Lie Down.');
fs.writeFileSync(path.resolve(tempDir, 'sentry.client.config.ts'), 'Good dog!');
expect(getUserConfigFile(tempDir, 'server')).toEqual('sentry.server.config.ts');
expect(getUserConfigFile(tempDir, 'client')).toEqual('sentry.client.config.ts');
});
it('errors when files are missing', () => {
expect(() => getUserConfigFile(tempDir, 'server')).toThrowError(
`Cannot find 'sentry.server.config.ts' or 'sentry.server.config.js' in '${tempDir}'`,
);
expect(() => getUserConfigFile(tempDir, 'client')).toThrowError(
`Cannot find 'sentry.client.config.ts' or 'sentry.client.config.js' in '${tempDir}'`,
);
});
});
describe('correct paths from `distDir` in WebpackPluginOptions', () => {
it.each([
[getBuildContext('client', {}), '.next'],
[getBuildContext('server', { target: 'experimental-serverless-trace' }), '.next'], // serverless
[getBuildContext('server', {}, '4'), '.next'],
[getBuildContext('server', {}, '5'), '.next'],
])('`distDir` is not defined', (buildContext: BuildContext, expectedDistDir) => {
const includePaths = getWebpackPluginOptions(
buildContext,
{}, // userPluginOptions
{}, // userSentryOptions
).include as { paths: [] }[];
for (const pathDescriptor of includePaths) {
for (const path of pathDescriptor.paths) {
expect(path).toMatch(new RegExp(`${buildContext.dir}/${expectedDistDir}.*`));
}
}
});
it.each([
[getBuildContext('client', { distDir: 'tmpDir' }), 'tmpDir'],
[getBuildContext('server', { distDir: 'tmpDir', target: 'experimental-serverless-trace' }), 'tmpDir'], // serverless
[getBuildContext('server', { distDir: 'tmpDir' }, '4'), 'tmpDir'],
[getBuildContext('server', { distDir: 'tmpDir' }, '5'), 'tmpDir'],
])('`distDir` is defined', (buildContext: BuildContext, expectedDistDir) => {
const includePaths = getWebpackPluginOptions(
buildContext,
{}, // userPluginOptions
{}, // userSentryOptions
).include as { paths: [] }[];
for (const pathDescriptor of includePaths) {
for (const path of pathDescriptor.paths) {
expect(path).toMatch(new RegExp(`${buildContext.dir}/${expectedDistDir}.*`));
}
}
});
});
});