@@ -7,15 +7,16 @@ import {
7
7
SentryWebpackPluginOptions ,
8
8
WebpackConfigObject ,
9
9
} from '../src/config/types' ;
10
- import { SENTRY_SERVER_CONFIG_FILE , SERVER_SDK_INIT_PATH , storeServerConfigFileLocation } from '../src/config/utils' ;
10
+ import { SENTRY_SERVER_CONFIG_FILE , SERVER_SDK_INIT_PATH } from '../src/config/utils' ;
11
11
import { constructWebpackConfigFunction , SentryWebpackPlugin } from '../src/config/webpack' ;
12
12
13
13
// mock `storeServerConfigFileLocation` in order to make it a no-op when necessary
14
14
jest . mock ( '../src/config/utils' , ( ) => {
15
15
const original = jest . requireActual ( '../src/config/utils' ) ;
16
16
return {
17
17
...original ,
18
- storeServerConfigFileLocation : jest . fn ( ) . mockImplementation ( original . setRuntimeEnvVars ) ,
18
+ // nuke this so it won't try to look for our dummy paths
19
+ storeServerConfigFileLocation : jest . fn ( ) ,
19
20
} ;
20
21
} ) ;
21
22
@@ -34,6 +35,10 @@ const userNextConfig = {
34
35
} ;
35
36
const userSentryWebpackPluginConfig = { org : 'squirrelChasers' , project : 'simulator' , include : './thirdPartyMaps' } ;
36
37
38
+ /** mocks of the arguments passed to the result of `withSentryConfig` (when it's a function) */
39
+ const runtimePhase = 'puppy-phase-chew-everything-in-sight' ;
40
+ const defaultNextConfig = { nappingHoursPerDay : 20 , oversizeFeet : true , shouldChaseTail : true } ;
41
+
37
42
/** mocks of the arguments passed to `nextConfig.webpack` */
38
43
const serverWebpackConfig = {
39
44
entry : ( ) => Promise . resolve ( { 'pages/api/dogs/[name]' : 'private-next-pages/api/dogs/[name].js' } ) ,
@@ -47,7 +52,8 @@ const clientWebpackConfig = {
47
52
target : 'web' ,
48
53
context : '/Users/Maisey/projects/squirrelChasingSimulator' ,
49
54
} ;
50
- const buildContext = { isServer : true , dev : false , buildId : 'doGsaREgReaT' } ;
55
+ const serverBuildContext = { isServer : true , dev : false , buildId : 'doGsaREgReaT' } ;
56
+ const clientBuildContext = { isServer : false , dev : false , buildId : 'doGsaREgReaT' } ;
51
57
52
58
/**
53
59
* Derive the final values of all next config options, by first applying `withSentryConfig` and then, if it returns a
@@ -61,16 +67,16 @@ const buildContext = { isServer: true, dev: false, buildId: 'doGsaREgReaT' };
61
67
*/
62
68
function materializeFinalNextConfig (
63
69
userNextConfig : ExportedNextConfig ,
64
- userSentryWebpackPluginConfig : SentryWebpackPluginOptions ,
70
+ userSentryWebpackPluginConfig ? : SentryWebpackPluginOptions ,
65
71
) : NextConfigObject {
66
72
const sentrifiedConfig = withSentryConfig ( userNextConfig , userSentryWebpackPluginConfig ) ;
67
73
let finalConfigValues = sentrifiedConfig ;
68
74
69
75
if ( typeof sentrifiedConfig === 'function' ) {
70
76
// for some reason TS won't recognize that `finalConfigValues` is now a NextConfigObject, which is why the cast
71
77
// below is necessary
72
- finalConfigValues = sentrifiedConfig ( 'phase-production-build' , {
73
- defaultConfig : { } ,
78
+ finalConfigValues = sentrifiedConfig ( runtimePhase , {
79
+ defaultConfig : defaultNextConfig ,
74
80
} ) ;
75
81
}
76
82
@@ -92,14 +98,25 @@ function materializeFinalNextConfig(
92
98
*/
93
99
async function materializeFinalWebpackConfig ( options : {
94
100
userNextConfig : ExportedNextConfig ;
95
- userSentryWebpackPluginConfig : SentryWebpackPluginOptions ;
101
+ userSentryWebpackPluginConfig ? : SentryWebpackPluginOptions ;
96
102
incomingWebpackConfig : WebpackConfigObject ;
97
103
incomingWebpackBuildContext : BuildContext ;
98
104
} ) : Promise < WebpackConfigObject > {
99
105
const { userNextConfig, userSentryWebpackPluginConfig, incomingWebpackConfig, incomingWebpackBuildContext } = options ;
100
106
107
+ // if the user's next config is a function, run it so we have access to the values
108
+ const materializedUserNextConfig =
109
+ typeof userNextConfig === 'function'
110
+ ? userNextConfig ( 'phase-production-build' , {
111
+ defaultConfig : { } ,
112
+ } )
113
+ : userNextConfig ;
114
+
101
115
// get the webpack config function we'd normally pass back to next
102
- const webpackConfigFunction = constructWebpackConfigFunction ( userNextConfig , userSentryWebpackPluginConfig ) ;
116
+ const webpackConfigFunction = constructWebpackConfigFunction (
117
+ materializedUserNextConfig ,
118
+ userSentryWebpackPluginConfig ,
119
+ ) ;
103
120
104
121
// call it to get concrete values for comparison
105
122
const finalWebpackConfigValue = webpackConfigFunction ( incomingWebpackConfig , incomingWebpackBuildContext ) ;
@@ -111,7 +128,7 @@ async function materializeFinalWebpackConfig(options: {
111
128
112
129
describe ( 'withSentryConfig' , ( ) => {
113
130
it ( 'includes expected properties' , ( ) => {
114
- const finalConfig = materializeFinalNextConfig ( userNextConfig , userSentryWebpackPluginConfig ) ;
131
+ const finalConfig = materializeFinalNextConfig ( userNextConfig ) ;
115
132
116
133
expect ( finalConfig ) . toEqual (
117
134
expect . objectContaining ( {
@@ -121,13 +138,13 @@ describe('withSentryConfig', () => {
121
138
} ) ;
122
139
123
140
it ( 'preserves unrelated next config options' , ( ) => {
124
- const finalConfig = materializeFinalNextConfig ( userNextConfig , userSentryWebpackPluginConfig ) ;
141
+ const finalConfig = materializeFinalNextConfig ( userNextConfig ) ;
125
142
126
143
expect ( finalConfig . publicRuntimeConfig ) . toEqual ( userNextConfig . publicRuntimeConfig ) ;
127
144
} ) ;
128
145
129
146
it ( "works when user's overall config is an object" , ( ) => {
130
- const finalConfig = materializeFinalNextConfig ( userNextConfig , userSentryWebpackPluginConfig ) ;
147
+ const finalConfig = materializeFinalNextConfig ( userNextConfig ) ;
131
148
132
149
expect ( finalConfig ) . toEqual (
133
150
expect . objectContaining ( {
@@ -140,7 +157,7 @@ describe('withSentryConfig', () => {
140
157
it ( "works when user's overall config is a function" , ( ) => {
141
158
const userNextConfigFunction = ( ) => userNextConfig ;
142
159
143
- const finalConfig = materializeFinalNextConfig ( userNextConfigFunction , userSentryWebpackPluginConfig ) ;
160
+ const finalConfig = materializeFinalNextConfig ( userNextConfigFunction ) ;
144
161
145
162
expect ( finalConfig ) . toEqual (
146
163
expect . objectContaining ( {
@@ -149,20 +166,24 @@ describe('withSentryConfig', () => {
149
166
} ) ,
150
167
) ;
151
168
} ) ;
152
- } ) ;
153
169
154
- describe ( 'webpack config' , ( ) => {
155
- beforeEach ( ( ) => {
156
- // nuke this so it won't try to look for our dummy paths
157
- ( storeServerConfigFileLocation as jest . Mock ) . mockImplementationOnce ( ( ) => undefined ) ;
170
+ it ( 'correctly passes `phase` and `defaultConfig` through to functional `userNextConfig`' , ( ) => {
171
+ const userNextConfigFunction = jest . fn ( ) . mockReturnValue ( userNextConfig ) ;
172
+
173
+ materializeFinalNextConfig ( userNextConfigFunction ) ;
174
+
175
+ expect ( userNextConfigFunction ) . toHaveBeenCalledWith ( runtimePhase , {
176
+ defaultConfig : defaultNextConfig ,
177
+ } ) ;
158
178
} ) ;
179
+ } ) ;
159
180
181
+ describe ( 'webpack config' , ( ) => {
160
182
it ( 'includes expected properties' , async ( ) => {
161
183
const finalWebpackConfig = await materializeFinalWebpackConfig ( {
162
184
userNextConfig,
163
- userSentryWebpackPluginConfig,
164
185
incomingWebpackConfig : serverWebpackConfig ,
165
- incomingWebpackBuildContext : buildContext ,
186
+ incomingWebpackBuildContext : serverBuildContext ,
166
187
} ) ;
167
188
168
189
expect ( finalWebpackConfig ) . toEqual (
@@ -177,14 +198,13 @@ describe('webpack config', () => {
177
198
it ( 'preserves unrelated webpack config options' , async ( ) => {
178
199
const finalWebpackConfig = await materializeFinalWebpackConfig ( {
179
200
userNextConfig,
180
- userSentryWebpackPluginConfig,
181
201
incomingWebpackConfig : serverWebpackConfig ,
182
- incomingWebpackBuildContext : buildContext ,
202
+ incomingWebpackBuildContext : serverBuildContext ,
183
203
} ) ;
184
204
185
205
// Run the user's webpack config function, so we can check the results against ours. Delete `entry` because we'll
186
206
// test it separately, and besides, it's one that we *should* be overwriting.
187
- const materializedUserWebpackConfig = userNextConfig . webpack ( serverWebpackConfig , buildContext ) ;
207
+ const materializedUserWebpackConfig = userNextConfig . webpack ( serverWebpackConfig , serverBuildContext ) ;
188
208
// @ts -ignore `entry` may be required in real life, but we don't need it for our tests
189
209
delete materializedUserWebpackConfig . entry ;
190
210
@@ -195,9 +215,8 @@ describe('webpack config', () => {
195
215
it ( 'injects correct code when building server bundle' , async ( ) => {
196
216
const finalWebpackConfig = await materializeFinalWebpackConfig ( {
197
217
userNextConfig,
198
- userSentryWebpackPluginConfig,
199
218
incomingWebpackConfig : serverWebpackConfig ,
200
- incomingWebpackBuildContext : buildContext ,
219
+ incomingWebpackBuildContext : serverBuildContext ,
201
220
} ) ;
202
221
203
222
expect ( finalWebpackConfig . entry ) . toEqual (
@@ -210,9 +229,8 @@ describe('webpack config', () => {
210
229
it ( 'injects correct code when building client bundle' , async ( ) => {
211
230
const finalWebpackConfig = await materializeFinalWebpackConfig ( {
212
231
userNextConfig,
213
- userSentryWebpackPluginConfig,
214
232
incomingWebpackConfig : clientWebpackConfig ,
215
- incomingWebpackBuildContext : { ... buildContext , isServer : false } ,
233
+ incomingWebpackBuildContext : clientBuildContext ,
216
234
} ) ;
217
235
218
236
expect ( finalWebpackConfig . entry ) . toEqual (
@@ -224,12 +242,11 @@ describe('webpack config', () => {
224
242
it ( 'handles non-empty `main.js` entry point' , async ( ) => {
225
243
const finalWebpackConfig = await materializeFinalWebpackConfig ( {
226
244
userNextConfig,
227
- userSentryWebpackPluginConfig,
228
245
incomingWebpackConfig : {
229
246
...clientWebpackConfig ,
230
247
entry : ( ) => Promise . resolve ( { main : './src/index.ts' , 'main.js' : [ 'sitLieDownRollOver.config.js' ] } ) ,
231
248
} ,
232
- incomingWebpackBuildContext : { ... buildContext , isServer : false } ,
249
+ incomingWebpackBuildContext : clientBuildContext ,
233
250
} ) ;
234
251
235
252
expect ( finalWebpackConfig . entry ) . toEqual (
@@ -244,15 +261,15 @@ describe('webpack config', () => {
244
261
245
262
describe ( 'Sentry webpack plugin config' , ( ) => {
246
263
it ( 'includes expected properties' , ( ) => {
247
- // pass
264
+ // TODO
248
265
} ) ;
249
266
250
267
it ( 'preserves unrelated plugin config options' , ( ) => {
251
- // pass
268
+ // TODO
252
269
} ) ;
253
270
254
271
it ( 'warns when overriding certain default values' , ( ) => {
255
- // pass
272
+ // TODO
256
273
} ) ;
257
274
258
275
it ( "merges default include and ignore/ignoreFile options with user's values" , ( ) => {
0 commit comments