/
config.test.ts
513 lines (433 loc) · 21.3 KB
/
config.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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import * as rimraf from 'rimraf';
import { withSentryConfig } from '../src/config';
import {
BuildContext,
EntryPropertyFunction,
ExportedNextConfig,
NextConfigObject,
SentryWebpackPlugin as SentryWebpackPluginType,
SentryWebpackPluginOptions,
WebpackConfigObject,
} from '../src/config/types';
import { constructWebpackConfigFunction, getUserConfigFile, SentryWebpackPlugin } from '../src/config/webpack';
const SERVER_SDK_CONFIG_FILE = 'sentry.server.config.js';
const CLIENT_SDK_CONFIG_FILE = 'sentry.client.config.js';
// We use `fs.existsSync()` in `getUserConfigFile()`. When we're not testing `getUserConfigFile()` specifically, all we
// need is for it to give us any valid answer, so make it always find what it's looking for. Since this is a core node
// built-in, though, which jest itself uses, otherwise let it do the normal thing. Storing the real version of the
// function also lets us restore the original when we do want to test `getUserConfigFile()`.
const realExistsSync = jest.requireActual('fs').existsSync;
const mockExistsSync = (path: fs.PathLike) => {
if ((path as string).endsWith(SERVER_SDK_CONFIG_FILE) || (path as string).endsWith(CLIENT_SDK_CONFIG_FILE)) {
return true;
}
return realExistsSync(path);
};
const exitsSync = jest.spyOn(fs, 'existsSync').mockImplementation(mockExistsSync);
/** Mocks of the arguments passed to `withSentryConfig` */
const userNextConfig: Partial<NextConfigObject> = {
publicRuntimeConfig: { location: 'dogpark', activities: ['fetch', 'chasing', 'digging'] },
webpack: (config: WebpackConfigObject, _options: BuildContext) => ({
...config,
mode: 'universal-sniffing',
entry: async () =>
Promise.resolve({
...(await (config.entry as EntryPropertyFunction)()),
simulatorBundle: './src/simulator/index.ts',
}),
}),
};
const userSentryWebpackPluginConfig = { org: 'squirrelChasers', project: 'simulator' };
process.env.SENTRY_AUTH_TOKEN = 'dogsarebadatkeepingsecrets';
process.env.SENTRY_RELEASE = 'doGsaREgReaT';
/** Mocks of the arguments passed to the result of `withSentryConfig` (when it's a function). */
const runtimePhase = 'ball-fetching';
// `defaultConfig` is the defaults for all nextjs options (we don't use these at all in the tests, so for our purposes
// here the values don't matter)
const defaultsObject = { defaultConfig: {} };
/** mocks of the arguments passed to `nextConfig.webpack` */
const serverWebpackConfig = {
entry: () =>
Promise.resolve({
'pages/api/dogs/[name]': 'private-next-pages/api/dogs/[name].js',
'pages/_app': ['./node_modules/smellOVision/index.js', 'private-next-pages/_app.js'],
'pages/api/simulator/dogStats/[name]': { import: 'private-next-pages/api/simulator/dogStats/[name].js' },
'pages/api/simulator/leaderboard': {
import: ['./node_modules/dogPoints/converter.js', 'private-next-pages/api/simulator/leaderboard.js'],
},
'pages/api/tricks/[trickName]': {
import: 'private-next-pages/api/tricks/[trickName].js',
dependOn: 'treats',
},
treats: './node_modules/dogTreats/treatProvider.js',
}),
output: { filename: '[name].js', path: '/Users/Maisey/projects/squirrelChasingSimulator/.next' },
target: 'node',
context: '/Users/Maisey/projects/squirrelChasingSimulator',
};
const clientWebpackConfig = {
entry: () =>
Promise.resolve({
main: './src/index.ts',
'pages/_app': 'next-client-pages-loader?page=%2F_app',
}),
output: { filename: 'static/chunks/[name].js', path: '/Users/Maisey/projects/squirrelChasingSimulator/.next' },
target: 'web',
context: '/Users/Maisey/projects/squirrelChasingSimulator',
};
// In real life, next will copy the `userNextConfig` into the `buildContext`. Since we're providing mocks for both of
// those, we need to mimic that behavior, and since `userNextConfig` can vary per test, we need to have the option do it
// dynamically.
function getBuildContext(buildTarget: 'server' | 'client', userNextConfig: Partial<NextConfigObject>): BuildContext {
return {
dev: false,
buildId: 'sItStAyLiEdOwN',
dir: '/Users/Maisey/projects/squirrelChasingSimulator',
config: { target: 'server', ...userNextConfig },
webpack: { version: '5.4.15' },
isServer: buildTarget === 'server',
};
}
const serverBuildContext = getBuildContext('server', userNextConfig);
const clientBuildContext = getBuildContext('client', userNextConfig);
/**
* Derive the final values of all next config options, by first applying `withSentryConfig` and then, if it returns a
* function, running that function.
*
* @param userNextConfig Next config options provided by the user
* @param userSentryWebpackPluginConfig SentryWebpackPlugin options provided by the user
*
* @returns The config values next will receive directly from `withSentryConfig` or when it calls the function returned
* by `withSentryConfig`
*/
function materializeFinalNextConfig(
userNextConfig: ExportedNextConfig,
userSentryWebpackPluginConfig?: Partial<SentryWebpackPluginOptions>,
): NextConfigObject {
const sentrifiedConfig = withSentryConfig(userNextConfig, userSentryWebpackPluginConfig);
let finalConfigValues = sentrifiedConfig;
if (typeof sentrifiedConfig === 'function') {
// for some reason TS won't recognize that `finalConfigValues` is now a NextConfigObject, which is why the cast
// below is necessary
finalConfigValues = sentrifiedConfig(runtimePhase, defaultsObject);
}
return finalConfigValues as NextConfigObject;
}
/**
* Derive the final values of all webpack config options, by first applying `constructWebpackConfigFunction` and then
* running the resulting function. Since the `entry` property of the resulting object is itself a function, also call
* that.
*
* @param options An object including the following:
* - `userNextConfig` Next config options provided by the user
* - `userSentryWebpackPluginConfig` SentryWebpackPlugin options provided by the user
* - `incomingWebpackConfig` The existing webpack config, passed to the function as `config`
* - `incomingWebpackBuildContext` The existing webpack build context, passed to the function as `options`
*
* @returns The webpack config values next will use when it calls the function that `createFinalWebpackConfig` returns
*/
async function materializeFinalWebpackConfig(options: {
userNextConfig: ExportedNextConfig;
userSentryWebpackPluginConfig?: Partial<SentryWebpackPluginOptions>;
incomingWebpackConfig: WebpackConfigObject;
incomingWebpackBuildContext: BuildContext;
}): Promise<WebpackConfigObject> {
const { userNextConfig, userSentryWebpackPluginConfig, incomingWebpackConfig, incomingWebpackBuildContext } = options;
// if the user's next config is a function, run it so we have access to the values
const materializedUserNextConfig =
typeof userNextConfig === 'function' ? userNextConfig('phase-production-build', defaultsObject) : userNextConfig;
// get the webpack config function we'd normally pass back to next
const webpackConfigFunction = constructWebpackConfigFunction(
materializedUserNextConfig,
userSentryWebpackPluginConfig,
);
// call it to get concrete values for comparison
const finalWebpackConfigValue = webpackConfigFunction(incomingWebpackConfig, incomingWebpackBuildContext);
const webpackEntryProperty = finalWebpackConfigValue.entry as EntryPropertyFunction;
finalWebpackConfigValue.entry = await webpackEntryProperty();
return finalWebpackConfigValue;
}
describe('withSentryConfig', () => {
it('includes expected properties', () => {
const finalConfig = materializeFinalNextConfig(userNextConfig);
expect(finalConfig).toEqual(
expect.objectContaining({
webpack: expect.any(Function), // `webpack` is tested specifically elsewhere
}),
);
});
it('preserves unrelated next config options', () => {
const finalConfig = materializeFinalNextConfig(userNextConfig);
expect(finalConfig.publicRuntimeConfig).toEqual(userNextConfig.publicRuntimeConfig);
});
it("works when user's overall config is an object", () => {
const finalConfig = materializeFinalNextConfig(userNextConfig);
expect(finalConfig).toEqual(
expect.objectContaining({
...userNextConfig,
webpack: expect.any(Function), // `webpack` is tested specifically elsewhere
}),
);
});
it("works when user's overall config is a function", () => {
const userNextConfigFunction = () => userNextConfig;
const finalConfig = materializeFinalNextConfig(userNextConfigFunction);
expect(finalConfig).toEqual(
expect.objectContaining({
...userNextConfigFunction(),
webpack: expect.any(Function), // `webpack` is tested specifically elsewhere
}),
);
});
it('correctly passes `phase` and `defaultConfig` through to functional `userNextConfig`', () => {
const userNextConfigFunction = jest.fn().mockReturnValue(userNextConfig);
materializeFinalNextConfig(userNextConfigFunction);
expect(userNextConfigFunction).toHaveBeenCalledWith(runtimePhase, defaultsObject);
});
});
describe('webpack config', () => {
it('includes expected properties', async () => {
const finalWebpackConfig = await materializeFinalWebpackConfig({
userNextConfig,
incomingWebpackConfig: serverWebpackConfig,
incomingWebpackBuildContext: serverBuildContext,
});
expect(finalWebpackConfig).toEqual(
expect.objectContaining({
devtool: 'source-map',
entry: expect.any(Object), // `entry` is tested specifically elsewhere
plugins: expect.arrayContaining([expect.any(SentryWebpackPlugin)]),
}),
);
});
it('preserves unrelated webpack config options', async () => {
const finalWebpackConfig = await materializeFinalWebpackConfig({
userNextConfig,
incomingWebpackConfig: serverWebpackConfig,
incomingWebpackBuildContext: serverBuildContext,
});
// Run the user's webpack config function, so we can check the results against ours. Delete `entry` because we'll
// test it separately, and besides, it's one that we *should* be overwriting.
const materializedUserWebpackConfig = userNextConfig.webpack!(serverWebpackConfig, serverBuildContext);
// @ts-ignore `entry` may be required in real life, but we don't need it for our tests
delete materializedUserWebpackConfig.entry;
expect(finalWebpackConfig).toEqual(expect.objectContaining(materializedUserWebpackConfig));
});
describe('webpack `entry` property config', () => {
const serverConfigFilePath = `./${SERVER_SDK_CONFIG_FILE}`;
const clientConfigFilePath = `./${CLIENT_SDK_CONFIG_FILE}`;
it('handles various entrypoint shapes', async () => {
const finalWebpackConfig = await materializeFinalWebpackConfig({
userNextConfig,
incomingWebpackConfig: serverWebpackConfig,
incomingWebpackBuildContext: serverBuildContext,
});
expect(finalWebpackConfig.entry).toEqual(
expect.objectContaining({
// original entry point value is a string
// (was 'private-next-pages/api/dogs/[name].js')
'pages/api/dogs/[name]': [serverConfigFilePath, 'private-next-pages/api/dogs/[name].js'],
// original entry point value is a string array
// (was ['./node_modules/smellOVision/index.js', 'private-next-pages/_app.js'])
'pages/_app': [serverConfigFilePath, './node_modules/smellOVision/index.js', 'private-next-pages/_app.js'],
// original entry point value is an object containing a string `import` value
// (`import` was 'private-next-pages/api/simulator/dogStats/[name].js')
'pages/api/simulator/dogStats/[name]': {
import: [serverConfigFilePath, 'private-next-pages/api/simulator/dogStats/[name].js'],
},
// original entry point value is an object containing a string array `import` value
// (`import` was ['./node_modules/dogPoints/converter.js', 'private-next-pages/api/simulator/leaderboard.js'])
'pages/api/simulator/leaderboard': {
import: [
serverConfigFilePath,
'./node_modules/dogPoints/converter.js',
'private-next-pages/api/simulator/leaderboard.js',
],
},
// original entry point value is an object containg properties besides `import`
// (`dependOn` remains untouched)
'pages/api/tricks/[trickName]': {
import: [serverConfigFilePath, 'private-next-pages/api/tricks/[trickName].js'],
dependOn: 'treats',
},
}),
);
});
it('does not inject into non-_app, non-API routes', async () => {
const finalWebpackConfig = await materializeFinalWebpackConfig({
userNextConfig,
incomingWebpackConfig: clientWebpackConfig,
incomingWebpackBuildContext: clientBuildContext,
});
expect(finalWebpackConfig.entry).toEqual(
expect.objectContaining({
// no injected file
main: './src/index.ts',
// was 'next-client-pages-loader?page=%2F_app'
'pages/_app': [clientConfigFilePath, 'next-client-pages-loader?page=%2F_app'],
}),
);
});
});
});
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({
userNextConfig,
userSentryWebpackPluginConfig,
incomingWebpackConfig: serverWebpackConfig,
incomingWebpackBuildContext: serverBuildContext,
});
expect(finalWebpackConfig.plugins?.[0].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({
userNextConfig,
userSentryWebpackPluginConfig: { ...userSentryWebpackPluginConfig, debug: true },
incomingWebpackConfig: serverWebpackConfig,
incomingWebpackBuildContext: serverBuildContext,
});
expect((finalWebpackConfig.plugins?.[0].options as SentryWebpackPluginOptions).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({
userNextConfig,
incomingWebpackConfig: clientWebpackConfig,
incomingWebpackBuildContext: clientBuildContext,
});
const sentryWebpackPlugin = finalWebpackConfig.plugins?.[0] as SentryWebpackPluginType;
expect(sentryWebpackPlugin.options?.include).toEqual([
{ paths: ['.next/static/chunks/pages'], urlPrefix: '~/_next/static/chunks/pages' },
]);
});
it('has the correct value when building serverless server bundles', async () => {
const userNextConfigServerless = { ...userNextConfig };
userNextConfigServerless.target = 'experimental-serverless-trace';
const finalWebpackConfig = await materializeFinalWebpackConfig({
userNextConfig: userNextConfigServerless,
incomingWebpackConfig: serverWebpackConfig,
incomingWebpackBuildContext: getBuildContext('server', userNextConfigServerless),
});
const sentryWebpackPlugin = finalWebpackConfig.plugins?.[0] as SentryWebpackPluginType;
expect(sentryWebpackPlugin.options?.include).toEqual([
{ paths: ['.next/serverless/'], urlPrefix: '~/_next/serverless' },
]);
});
it('has the correct value when building serverful server bundles using webpack 4', async () => {
const serverBuildContextWebpack4 = getBuildContext('server', userNextConfig);
serverBuildContextWebpack4.webpack.version = '4.15.13';
const finalWebpackConfig = await materializeFinalWebpackConfig({
userNextConfig,
incomingWebpackConfig: serverWebpackConfig,
incomingWebpackBuildContext: serverBuildContextWebpack4,
});
const sentryWebpackPlugin = finalWebpackConfig.plugins?.[0] as SentryWebpackPluginType;
expect(sentryWebpackPlugin.options?.include).toEqual([
{ paths: ['.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({
userNextConfig,
incomingWebpackConfig: serverWebpackConfig,
incomingWebpackBuildContext: serverBuildContext,
});
const sentryWebpackPlugin = finalWebpackConfig.plugins?.[0] as SentryWebpackPluginType;
expect(sentryWebpackPlugin.options?.include).toEqual([
{ paths: ['.next/server/pages/'], urlPrefix: '~/_next/server/pages' },
{ paths: ['.next/server/chunks/'], urlPrefix: '~/_next/server/chunks' },
]);
});
});
it('allows SentryWebpackPlugin to be turned off for client code (independent of server code)', () => {
const clientFinalNextConfig = materializeFinalNextConfig({
...userNextConfig,
sentry: { disableClientWebpackPlugin: true },
});
const clientFinalWebpackConfig = clientFinalNextConfig.webpack?.(clientWebpackConfig, clientBuildContext);
const serverFinalNextConfig = materializeFinalNextConfig(userNextConfig, userSentryWebpackPluginConfig);
const serverFinalWebpackConfig = serverFinalNextConfig.webpack?.(serverWebpackConfig, serverBuildContext);
expect(clientFinalWebpackConfig?.plugins).not.toEqual(expect.arrayContaining([expect.any(SentryWebpackPlugin)]));
expect(serverFinalWebpackConfig?.plugins).toEqual(expect.arrayContaining([expect.any(SentryWebpackPlugin)]));
});
it('allows SentryWebpackPlugin to be turned off for server code (independent of client code)', () => {
const serverFinalNextConfig = materializeFinalNextConfig({
...userNextConfig,
sentry: { disableServerWebpackPlugin: true },
});
const serverFinalWebpackConfig = serverFinalNextConfig.webpack?.(serverWebpackConfig, serverBuildContext);
const clientFinalNextConfig = materializeFinalNextConfig(userNextConfig, userSentryWebpackPluginConfig);
const clientFinalWebpackConfig = clientFinalNextConfig.webpack?.(clientWebpackConfig, clientBuildContext);
expect(serverFinalWebpackConfig?.plugins).not.toEqual(expect.arrayContaining([expect.any(SentryWebpackPlugin)]));
expect(clientFinalWebpackConfig?.plugins).toEqual(expect.arrayContaining([expect.any(SentryWebpackPlugin)]));
});
it("doesn't set devtool if webpack plugin is disabled", () => {
const finalNextConfig = materializeFinalNextConfig({
...userNextConfig,
webpack: () => ({ devtool: 'something-besides-source-map' } as any),
sentry: { disableServerWebpackPlugin: true },
});
const finalWebpackConfig = finalNextConfig.webpack?.(serverWebpackConfig, serverBuildContext);
expect(finalWebpackConfig?.devtool).not.toEqual('source-map');
});
describe('getUserConfigFile', () => {
let tempDir: string;
beforeAll(() => {
exitsSync.mockImplementation(realExistsSync);
});
beforeEach(() => {
const tempDirPathPrefix = path.join(os.tmpdir(), 'sentry-nextjs-test-');
tempDir = fs.mkdtempSync(tempDirPathPrefix);
});
afterEach(() => {
rimraf.sync(tempDir);
});
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}'`,
);
});
});
});