@@ -2,14 +2,14 @@ import * as fs from 'fs';
22import * as os from 'os' ;
33import * as path from 'path' ;
44import * as rimraf from 'rimraf' ;
5+ import { WebpackPluginInstance } from 'webpack' ;
56
67import { withSentryConfig } from '../src/config' ;
78import {
89 BuildContext ,
910 EntryPropertyFunction ,
1011 ExportedNextConfig ,
1112 NextConfigObject ,
12- SentryWebpackPlugin as SentryWebpackPluginType ,
1313 SentryWebpackPluginOptions ,
1414 WebpackConfigObject ,
1515} from '../src/config/types' ;
@@ -58,7 +58,7 @@ process.env.SENTRY_RELEASE = 'doGsaREgReaT';
5858const runtimePhase = 'ball-fetching' ;
5959// `defaultConfig` is the defaults for all nextjs options (we don't use these at all in the tests, so for our purposes
6060// here the values don't matter)
61- const defaultsObject = { defaultConfig : { } } ;
61+ const defaultsObject = { defaultConfig : { } as NextConfigObject } ;
6262
6363/** mocks of the arguments passed to `nextConfig.webpack` */
6464const serverWebpackConfig = {
@@ -103,7 +103,7 @@ function getBuildContext(
103103 dev : false ,
104104 buildId : 'sItStAyLiEdOwN' ,
105105 dir : '/Users/Maisey/projects/squirrelChasingSimulator' ,
106- config : { target : 'server' , ...userNextConfig } ,
106+ config : { target : 'server' , ...userNextConfig } as NextConfigObject ,
107107 webpack : { version : webpackVersion } ,
108108 isServer : buildTarget === 'server' ,
109109 } ;
@@ -177,6 +177,14 @@ async function materializeFinalWebpackConfig(options: {
177177 return finalWebpackConfigValue ;
178178}
179179
180+ // helper function to make sure we're checking the correct plugin's data
181+ export function findWebpackPlugin (
182+ webpackConfig : WebpackConfigObject ,
183+ pluginName : string ,
184+ ) : WebpackPluginInstance | SentryWebpackPlugin | undefined {
185+ return webpackConfig . plugins ?. find ( plugin => plugin . constructor . name === pluginName ) ;
186+ }
187+
180188describe ( 'withSentryConfig' , ( ) => {
181189 it ( 'includes expected properties' , ( ) => {
182190 const finalConfig = materializeFinalNextConfig ( userNextConfig ) ;
@@ -335,8 +343,9 @@ describe('Sentry webpack plugin config', () => {
335343 incomingWebpackConfig : serverWebpackConfig ,
336344 incomingWebpackBuildContext : serverBuildContext ,
337345 } ) ;
346+ const sentryWebpackPluginInstance = findWebpackPlugin ( finalWebpackConfig , 'SentryCliPlugin' ) as SentryWebpackPlugin ;
338347
339- expect ( finalWebpackConfig . plugins ?. [ 0 ] . options ) . toEqual (
348+ expect ( sentryWebpackPluginInstance . options ) . toEqual (
340349 expect . objectContaining ( {
341350 include : expect . any ( Array ) , // default, tested separately elsewhere
342351 ignore : [ ] , // default
@@ -359,8 +368,9 @@ describe('Sentry webpack plugin config', () => {
359368 incomingWebpackConfig : serverWebpackConfig ,
360369 incomingWebpackBuildContext : serverBuildContext ,
361370 } ) ;
371+ const sentryWebpackPluginInstance = findWebpackPlugin ( finalWebpackConfig , 'SentryCliPlugin' ) as SentryWebpackPlugin ;
362372
363- expect ( ( finalWebpackConfig . plugins ?. [ 0 ] . options as SentryWebpackPluginOptions ) . debug ) . toEqual ( true ) ;
373+ expect ( sentryWebpackPluginInstance . options . debug ) . toEqual ( true ) ;
364374 } ) ;
365375
366376 it ( 'warns when overriding certain default values' , ( ) => {
@@ -379,9 +389,12 @@ describe('Sentry webpack plugin config', () => {
379389 incomingWebpackBuildContext : clientBuildContext ,
380390 } ) ;
381391
382- const sentryWebpackPlugin = finalWebpackConfig . plugins ?. [ 0 ] as SentryWebpackPluginType ;
392+ const sentryWebpackPluginInstance = findWebpackPlugin (
393+ finalWebpackConfig ,
394+ 'SentryCliPlugin' ,
395+ ) as SentryWebpackPlugin ;
383396
384- expect ( sentryWebpackPlugin . options ? .include ) . toEqual ( [
397+ expect ( sentryWebpackPluginInstance . options . include ) . toEqual ( [
385398 { paths : [ '.next/static/chunks/pages' ] , urlPrefix : '~/_next/static/chunks/pages' } ,
386399 ] ) ;
387400 } ) ;
@@ -396,9 +409,12 @@ describe('Sentry webpack plugin config', () => {
396409 incomingWebpackBuildContext : getBuildContext ( 'server' , userNextConfigServerless ) ,
397410 } ) ;
398411
399- const sentryWebpackPlugin = finalWebpackConfig . plugins ?. [ 0 ] as SentryWebpackPluginType ;
412+ const sentryWebpackPluginInstance = findWebpackPlugin (
413+ finalWebpackConfig ,
414+ 'SentryCliPlugin' ,
415+ ) as SentryWebpackPlugin ;
400416
401- expect ( sentryWebpackPlugin . options ? .include ) . toEqual ( [
417+ expect ( sentryWebpackPluginInstance . options . include ) . toEqual ( [
402418 { paths : [ '.next/serverless/' ] , urlPrefix : '~/_next/serverless' } ,
403419 ] ) ;
404420 } ) ;
@@ -413,9 +429,12 @@ describe('Sentry webpack plugin config', () => {
413429 incomingWebpackBuildContext : serverBuildContextWebpack4 ,
414430 } ) ;
415431
416- const sentryWebpackPlugin = finalWebpackConfig . plugins ?. [ 0 ] as SentryWebpackPluginType ;
432+ const sentryWebpackPluginInstance = findWebpackPlugin (
433+ finalWebpackConfig ,
434+ 'SentryCliPlugin' ,
435+ ) as SentryWebpackPlugin ;
417436
418- expect ( sentryWebpackPlugin . options ? .include ) . toEqual ( [
437+ expect ( sentryWebpackPluginInstance . options . include ) . toEqual ( [
419438 { paths : [ '.next/server/pages/' ] , urlPrefix : '~/_next/server/pages' } ,
420439 ] ) ;
421440 } ) ;
@@ -427,9 +446,12 @@ describe('Sentry webpack plugin config', () => {
427446 incomingWebpackBuildContext : serverBuildContext ,
428447 } ) ;
429448
430- const sentryWebpackPlugin = finalWebpackConfig . plugins ?. [ 0 ] as SentryWebpackPluginType ;
449+ const sentryWebpackPluginInstance = findWebpackPlugin (
450+ finalWebpackConfig ,
451+ 'SentryCliPlugin' ,
452+ ) as SentryWebpackPlugin ;
431453
432- expect ( sentryWebpackPlugin . options ? .include ) . toEqual ( [
454+ expect ( sentryWebpackPluginInstance . options . include ) . toEqual ( [
433455 { paths : [ '.next/server/pages/' ] , urlPrefix : '~/_next/server/pages' } ,
434456 { paths : [ '.next/server/chunks/' ] , urlPrefix : '~/_next/server/chunks' } ,
435457 ] ) ;
@@ -449,9 +471,12 @@ describe('Sentry webpack plugin config', () => {
449471 incomingWebpackBuildContext : getBuildContext ( 'client' , userNextConfigWithBasePath ) ,
450472 } ) ;
451473
452- const sentryWebpackPlugin = finalWebpackConfig . plugins ?. [ 0 ] as SentryWebpackPluginType ;
474+ const sentryWebpackPluginInstance = findWebpackPlugin (
475+ finalWebpackConfig ,
476+ 'SentryCliPlugin' ,
477+ ) as SentryWebpackPlugin ;
453478
454- expect ( sentryWebpackPlugin . options ? .include ) . toEqual ( [
479+ expect ( sentryWebpackPluginInstance . options . include ) . toEqual ( [
455480 { paths : [ '.next/static/chunks/pages' ] , urlPrefix : '~/city-park/_next/static/chunks/pages' } ,
456481 ] ) ;
457482 } ) ;
@@ -466,9 +491,12 @@ describe('Sentry webpack plugin config', () => {
466491 incomingWebpackBuildContext : getBuildContext ( 'server' , userNextConfigServerless ) ,
467492 } ) ;
468493
469- const sentryWebpackPlugin = finalWebpackConfig . plugins ?. [ 0 ] as SentryWebpackPluginType ;
494+ const sentryWebpackPluginInstance = findWebpackPlugin (
495+ finalWebpackConfig ,
496+ 'SentryCliPlugin' ,
497+ ) as SentryWebpackPlugin ;
470498
471- expect ( sentryWebpackPlugin . options ? .include ) . toEqual ( [
499+ expect ( sentryWebpackPluginInstance . options . include ) . toEqual ( [
472500 { paths : [ '.next/serverless/' ] , urlPrefix : '~/city-park/_next/serverless' } ,
473501 ] ) ;
474502 } ) ;
@@ -483,9 +511,12 @@ describe('Sentry webpack plugin config', () => {
483511 incomingWebpackBuildContext : serverBuildContextWebpack4 ,
484512 } ) ;
485513
486- const sentryWebpackPlugin = finalWebpackConfig . plugins ?. [ 0 ] as SentryWebpackPluginType ;
514+ const sentryWebpackPluginInstance = findWebpackPlugin (
515+ finalWebpackConfig ,
516+ 'SentryCliPlugin' ,
517+ ) as SentryWebpackPlugin ;
487518
488- expect ( sentryWebpackPlugin . options ? .include ) . toEqual ( [
519+ expect ( sentryWebpackPluginInstance . options . include ) . toEqual ( [
489520 { paths : [ '.next/server/pages/' ] , urlPrefix : '~/city-park/_next/server/pages' } ,
490521 ] ) ;
491522 } ) ;
@@ -497,9 +528,12 @@ describe('Sentry webpack plugin config', () => {
497528 incomingWebpackBuildContext : getBuildContext ( 'server' , userNextConfigWithBasePath ) ,
498529 } ) ;
499530
500- const sentryWebpackPlugin = finalWebpackConfig . plugins ?. [ 0 ] as SentryWebpackPluginType ;
531+ const sentryWebpackPluginInstance = findWebpackPlugin (
532+ finalWebpackConfig ,
533+ 'SentryCliPlugin' ,
534+ ) as SentryWebpackPlugin ;
501535
502- expect ( sentryWebpackPlugin . options ? .include ) . toEqual ( [
536+ expect ( sentryWebpackPluginInstance . options . include ) . toEqual ( [
503537 { paths : [ '.next/server/pages/' ] , urlPrefix : '~/city-park/_next/server/pages' } ,
504538 { paths : [ '.next/server/chunks/' ] , urlPrefix : '~/city-park/_next/server/chunks' } ,
505539 ] ) ;
0 commit comments