@@ -33,7 +33,7 @@ const mockExistsSync = (path: fs.PathLike) => {
33
33
const exitsSync = jest . spyOn ( fs , 'existsSync' ) . mockImplementation ( mockExistsSync ) ;
34
34
35
35
/** Mocks of the arguments passed to `withSentryConfig` */
36
- const userNextConfig = {
36
+ const userNextConfig : Partial < NextConfigObject > = {
37
37
publicRuntimeConfig : { location : 'dogpark' , activities : [ 'fetch' , 'chasing' , 'digging' ] } ,
38
38
webpack : ( config : WebpackConfigObject , _options : BuildContext ) => ( {
39
39
...config ,
@@ -51,7 +51,9 @@ process.env.SENTRY_RELEASE = 'doGsaREgReaT';
51
51
52
52
/** Mocks of the arguments passed to the result of `withSentryConfig` (when it's a function). */
53
53
const runtimePhase = 'ball-fetching' ;
54
- const defaultNextConfig = { nappingHoursPerDay : 20 , oversizeFeet : true , shouldChaseTail : true } ;
54
+ // `defaultConfig` is the defaults for all nextjs options (we don't use these at all in the tests, so for our purposes
55
+ // here the values don't matter)
56
+ const defaultsObject = { defaultConfig : { } } ;
55
57
56
58
/** mocks of the arguments passed to `nextConfig.webpack` */
57
59
const serverWebpackConfig = {
@@ -84,15 +86,21 @@ const clientWebpackConfig = {
84
86
context : '/Users/Maisey/projects/squirrelChasingSimulator' ,
85
87
} ;
86
88
87
- const baseBuildContext = {
88
- dev : false ,
89
- buildId : 'sItStAyLiEdOwN' ,
90
- dir : '/Users/Maisey/projects/squirrelChasingSimulator' ,
91
- config : { target : 'server' as const } ,
92
- webpack : { version : '5.4.15' } ,
93
- } ;
94
- const serverBuildContext = { isServer : true , ...baseBuildContext } ;
95
- const clientBuildContext = { isServer : false , ...baseBuildContext } ;
89
+ // In real life, next will copy the `userNextConfig` into the `buildContext`. Since we're providing mocks for both of
90
+ // those, we need to mimic that behavior, and since `userNextConfig` can vary per test, we need to have the option do it
91
+ // dynamically.
92
+ function getBuildContext ( buildTarget : 'server' | 'client' , userNextConfig : Partial < NextConfigObject > ) : BuildContext {
93
+ return {
94
+ dev : false ,
95
+ buildId : 'sItStAyLiEdOwN' ,
96
+ dir : '/Users/Maisey/projects/squirrelChasingSimulator' ,
97
+ config : { target : 'server' , ...userNextConfig } ,
98
+ webpack : { version : '5.4.15' } ,
99
+ isServer : buildTarget === 'server' ,
100
+ } ;
101
+ }
102
+ const serverBuildContext = getBuildContext ( 'server' , userNextConfig ) ;
103
+ const clientBuildContext = getBuildContext ( 'client' , userNextConfig ) ;
96
104
97
105
/**
98
106
* Derive the final values of all next config options, by first applying `withSentryConfig` and then, if it returns a
@@ -114,9 +122,7 @@ function materializeFinalNextConfig(
114
122
if ( typeof sentrifiedConfig === 'function' ) {
115
123
// for some reason TS won't recognize that `finalConfigValues` is now a NextConfigObject, which is why the cast
116
124
// below is necessary
117
- finalConfigValues = sentrifiedConfig ( runtimePhase , {
118
- defaultConfig : defaultNextConfig ,
119
- } ) ;
125
+ finalConfigValues = sentrifiedConfig ( runtimePhase , defaultsObject ) ;
120
126
}
121
127
122
128
return finalConfigValues as NextConfigObject ;
@@ -145,11 +151,7 @@ async function materializeFinalWebpackConfig(options: {
145
151
146
152
// if the user's next config is a function, run it so we have access to the values
147
153
const materializedUserNextConfig =
148
- typeof userNextConfig === 'function'
149
- ? userNextConfig ( 'phase-production-build' , {
150
- defaultConfig : { } ,
151
- } )
152
- : userNextConfig ;
154
+ typeof userNextConfig === 'function' ? userNextConfig ( 'phase-production-build' , defaultsObject ) : userNextConfig ;
153
155
154
156
// get the webpack config function we'd normally pass back to next
155
157
const webpackConfigFunction = constructWebpackConfigFunction (
@@ -211,9 +213,7 @@ describe('withSentryConfig', () => {
211
213
212
214
materializeFinalNextConfig ( userNextConfigFunction ) ;
213
215
214
- expect ( userNextConfigFunction ) . toHaveBeenCalledWith ( runtimePhase , {
215
- defaultConfig : defaultNextConfig ,
216
- } ) ;
216
+ expect ( userNextConfigFunction ) . toHaveBeenCalledWith ( runtimePhase , defaultsObject ) ;
217
217
} ) ;
218
218
} ) ;
219
219
@@ -243,7 +243,7 @@ describe('webpack config', () => {
243
243
244
244
// Run the user's webpack config function, so we can check the results against ours. Delete `entry` because we'll
245
245
// test it separately, and besides, it's one that we *should* be overwriting.
246
- const materializedUserWebpackConfig = userNextConfig. webpack ( serverWebpackConfig , serverBuildContext ) ;
246
+ const materializedUserWebpackConfig = userNextConfig . webpack ! ( serverWebpackConfig , serverBuildContext ) ;
247
247
// @ts -ignore `entry` may be required in real life, but we don't need it for our tests
248
248
delete materializedUserWebpackConfig . entry ;
249
249
@@ -377,10 +377,13 @@ describe('Sentry webpack plugin config', () => {
377
377
} ) ;
378
378
379
379
it ( 'has the correct value when building serverless server bundles' , async ( ) => {
380
+ const userNextConfigServerless = { ...userNextConfig } ;
381
+ userNextConfigServerless . target = 'experimental-serverless-trace' ;
382
+
380
383
const finalWebpackConfig = await materializeFinalWebpackConfig ( {
381
- userNextConfig,
384
+ userNextConfig : userNextConfigServerless ,
382
385
incomingWebpackConfig : serverWebpackConfig ,
383
- incomingWebpackBuildContext : { ... serverBuildContext , config : { target : 'experimental-serverless-trace' } } ,
386
+ incomingWebpackBuildContext : getBuildContext ( 'server' , userNextConfigServerless ) ,
384
387
} ) ;
385
388
386
389
const sentryWebpackPlugin = finalWebpackConfig . plugins ?. [ 0 ] as SentryWebpackPluginType ;
@@ -391,10 +394,13 @@ describe('Sentry webpack plugin config', () => {
391
394
} ) ;
392
395
393
396
it ( 'has the correct value when building serverful server bundles using webpack 4' , async ( ) => {
397
+ const serverBuildContextWebpack4 = getBuildContext ( 'server' , userNextConfig ) ;
398
+ serverBuildContextWebpack4 . webpack . version = '4.15.13' ;
399
+
394
400
const finalWebpackConfig = await materializeFinalWebpackConfig ( {
395
401
userNextConfig,
396
402
incomingWebpackConfig : serverWebpackConfig ,
397
- incomingWebpackBuildContext : { ... serverBuildContext , webpack : { version : '4.15.13' } } ,
403
+ incomingWebpackBuildContext : serverBuildContextWebpack4 ,
398
404
} ) ;
399
405
400
406
const sentryWebpackPlugin = finalWebpackConfig . plugins ?. [ 0 ] as SentryWebpackPluginType ;
0 commit comments