@@ -61,7 +61,7 @@ type WorkerResult = {
61
61
stderr : string
62
62
uhr : Array < UHRReport >
63
63
count : Array < CountReport >
64
- errorLog : Array < ErrorLogReport >
64
+ errorLog : Array < string >
65
65
data : Record < string , unknown >
66
66
messages : Array < ReportableResult >
67
67
}
@@ -104,7 +104,7 @@ export function runWorkerCode(fn: Function): Promise<WorkerResult> {
104
104
const messages : Array < ReportableResult > = [ ]
105
105
const uhr : Array < UHRReport > = [ ]
106
106
const count : Array < CountReport > = [ ]
107
- const errorLog : Array < ErrorLogReport > = [ ]
107
+ const errorLog : Array < string > = [ ]
108
108
const data = { } as Record < string , unknown >
109
109
let stderr = ''
110
110
@@ -166,7 +166,7 @@ describe('unhandled-rejection filter', () => {
166
166
167
167
it ( 'should not install filter when disabled' , async ( ) => {
168
168
async function testForWorker ( ) {
169
- process . env . NEXT_USE_UNHANDLED_REJECTION_FILTER = 'disabled'
169
+ process . env . NEXT_UNHANDLED_REJECTION_FILTER = 'disabled'
170
170
require ( 'next/dist/server/node-environment-extensions/unhandled-rejection' )
171
171
172
172
reportResult ( {
@@ -185,7 +185,7 @@ describe('unhandled-rejection filter', () => {
185
185
186
186
it ( 'should install filter rejections when environment variable is enabled' , async ( ) => {
187
187
async function testForWorker ( ) {
188
- process . env . NEXT_USE_UNHANDLED_REJECTION_FILTER = 'enabled'
188
+ process . env . NEXT_UNHANDLED_REJECTION_FILTER = 'enabled'
189
189
require ( 'next/dist/server/node-environment-extensions/unhandled-rejection' )
190
190
191
191
reportResult ( {
@@ -204,7 +204,7 @@ describe('unhandled-rejection filter', () => {
204
204
205
205
it ( 'should install filter rejections when environment variable is enabled in debug mode' , async ( ) => {
206
206
async function testForWorker ( ) {
207
- process . env . NEXT_USE_UNHANDLED_REJECTION_FILTER = 'debug'
207
+ process . env . NEXT_UNHANDLED_REJECTION_FILTER = 'debug'
208
208
require ( 'next/dist/server/node-environment-extensions/unhandled-rejection' )
209
209
210
210
reportResult ( {
@@ -220,12 +220,121 @@ describe('unhandled-rejection filter', () => {
220
220
expect . arrayContaining ( [ expect . objectContaining ( { count : 1 } ) ] )
221
221
)
222
222
} )
223
+
224
+ it ( 'should warn once when you uninstall the filter with removeListener' , async ( ) => {
225
+ async function testForWorker ( ) {
226
+ const originalWarn = console . warn
227
+ console . warn = ( ...args : Array < any > ) => {
228
+ reportResult ( { type : 'error-log' , message : args . join ( ' ' ) } )
229
+ originalWarn ( ...args )
230
+ }
231
+
232
+ require ( 'next/dist/server/node-environment-extensions/unhandled-rejection' )
233
+
234
+ const filterListener = process . listeners ( 'unhandledRejection' ) [ 0 ]
235
+ process . removeListener ( 'unhandledRejection' , filterListener )
236
+ process . removeListener ( 'unhandledRejection' , filterListener )
237
+ process . removeAllListeners ( )
238
+ }
239
+
240
+ const { errorLog, exitCode } = await runWorkerCode ( testForWorker )
241
+ expect ( exitCode ) . toBe ( 0 )
242
+ expect ( errorLog ) . toMatchInlineSnapshot ( `
243
+ [
244
+ "[Next.js Unhandled Rejection Filter]: Uninstalling filter because \`process.removeListener('unhandledRejection', listener)\` was called with the filter listener. Uninstalling this filter is not recommended and will cause you to observe 'unhandledRejection' events related to intentionally aborted prerenders.
245
+
246
+ You can silence warnings related to this behavior by running Next.js with \`NEXT_UNHANDLED_REJECTION_FILTER=silent\` environment variable.
247
+
248
+ You can debug event listener operations by running Next.js with \`NEXT_UNHANDLED_REJECTION_FILTER=debug\` environment variable.",
249
+ ]
250
+ ` )
251
+ } )
252
+
253
+ it ( 'should warn once when you uninstall the filter with off' , async ( ) => {
254
+ async function testForWorker ( ) {
255
+ const originalWarn = console . warn
256
+ console . warn = ( ...args : Array < any > ) => {
257
+ reportResult ( { type : 'error-log' , message : args . join ( ' ' ) } )
258
+ originalWarn ( ...args )
259
+ }
260
+
261
+ require ( 'next/dist/server/node-environment-extensions/unhandled-rejection' )
262
+
263
+ const filterListener = process . listeners ( 'unhandledRejection' ) [ 0 ]
264
+ process . off ( 'unhandledRejection' , filterListener )
265
+ process . off ( 'unhandledRejection' , filterListener )
266
+ process . removeAllListeners ( )
267
+ }
268
+
269
+ const { errorLog, exitCode } = await runWorkerCode ( testForWorker )
270
+ expect ( exitCode ) . toBe ( 0 )
271
+ expect ( errorLog ) . toMatchInlineSnapshot ( `
272
+ [
273
+ "[Next.js Unhandled Rejection Filter]: Uninstalling filter because \`process.removeListener('unhandledRejection', listener)\` was called with the filter listener. Uninstalling this filter is not recommended and will cause you to observe 'unhandledRejection' events related to intentionally aborted prerenders.
274
+
275
+ You can silence warnings related to this behavior by running Next.js with \`NEXT_UNHANDLED_REJECTION_FILTER=silent\` environment variable.
276
+
277
+ You can debug event listener operations by running Next.js with \`NEXT_UNHANDLED_REJECTION_FILTER=debug\` environment variable.",
278
+ ]
279
+ ` )
280
+ } )
281
+
282
+ it ( 'should warn once when you uninstall the filter with removeAllListeners' , async ( ) => {
283
+ async function testForWorker ( ) {
284
+ const originalWarn = console . warn
285
+ console . warn = ( ...args : Array < any > ) => {
286
+ reportResult ( { type : 'error-log' , message : args . join ( ' ' ) } )
287
+ originalWarn ( ...args )
288
+ }
289
+
290
+ require ( 'next/dist/server/node-environment-extensions/unhandled-rejection' )
291
+
292
+ const filterListener = process . listeners ( 'unhandledRejection' ) [ 0 ]
293
+ process . removeAllListeners ( )
294
+ process . off ( 'unhandledRejection' , filterListener )
295
+ process . removeListener ( 'unhandledRejection' , filterListener )
296
+ }
297
+
298
+ const { errorLog, exitCode } = await runWorkerCode ( testForWorker )
299
+ expect ( exitCode ) . toBe ( 0 )
300
+ expect ( errorLog ) . toMatchInlineSnapshot ( `
301
+ [
302
+ "[Next.js Unhandled Rejection Filter]: Uninstalling filter because \`process.removeAllListeners()\` was called. Uninstalling this filter is not recommended and will cause you to observe 'unhandledRejection' events related to intentionally aborted prerenders.
303
+
304
+ You can silence warnings related to this behavior by running Next.js with \`NEXT_UNHANDLED_REJECTION_FILTER=silent\` environment variable.
305
+
306
+ You can debug event listener operations by running Next.js with \`NEXT_UNHANDLED_REJECTION_FILTER=debug\` environment variable.",
307
+ ]
308
+ ` )
309
+ } )
310
+
311
+ it ( 'does not warn when environment variable is set to silent mode' , async ( ) => {
312
+ async function testForWorker ( ) {
313
+ process . env . NEXT_UNHANDLED_REJECTION_FILTER = 'silent'
314
+ const originalWarn = console . warn
315
+ console . warn = ( ...args : Array < any > ) => {
316
+ reportResult ( { type : 'error-log' , message : args . join ( ' ' ) } )
317
+ originalWarn ( ...args )
318
+ }
319
+
320
+ require ( 'next/dist/server/node-environment-extensions/unhandled-rejection' )
321
+
322
+ const filterListener = process . listeners ( 'unhandledRejection' ) [ 0 ]
323
+ process . removeAllListeners ( )
324
+ process . off ( 'unhandledRejection' , filterListener )
325
+ process . removeListener ( 'unhandledRejection' , filterListener )
326
+ }
327
+
328
+ const { errorLog, exitCode } = await runWorkerCode ( testForWorker )
329
+ expect ( exitCode ) . toBe ( 0 )
330
+ expect ( errorLog ) . toMatchInlineSnapshot ( `[]` )
331
+ } )
223
332
} )
224
333
225
334
describe ( 'filtering functionality' , ( ) => {
226
335
it ( 'should suppress rejections from aborted prerender contexts' , async ( ) => {
227
336
async function testForWorker ( ) {
228
- process . env . NEXT_USE_UNHANDLED_REJECTION_FILTER = '1'
337
+ process . env . NEXT_UNHANDLED_REJECTION_FILTER = '1'
229
338
require ( 'next/dist/server/node-environment-extensions/unhandled-rejection' )
230
339
231
340
const {
@@ -293,7 +402,7 @@ describe('unhandled-rejection filter', () => {
293
402
294
403
it ( 'should suppress rejections from aborted prerender-client contexts' , async ( ) => {
295
404
async function testForWorker ( ) {
296
- process . env . NEXT_USE_UNHANDLED_REJECTION_FILTER = '1'
405
+ process . env . NEXT_UNHANDLED_REJECTION_FILTER = '1'
297
406
require ( 'next/dist/server/node-environment-extensions/unhandled-rejection' )
298
407
299
408
const {
@@ -361,7 +470,7 @@ describe('unhandled-rejection filter', () => {
361
470
362
471
it ( 'should suppress rejections from aborted prerender-runtime contexts' , async ( ) => {
363
472
async function testForWorker ( ) {
364
- process . env . NEXT_USE_UNHANDLED_REJECTION_FILTER = '1'
473
+ process . env . NEXT_UNHANDLED_REJECTION_FILTER = '1'
365
474
require ( 'next/dist/server/node-environment-extensions/unhandled-rejection' )
366
475
367
476
const {
@@ -429,7 +538,7 @@ describe('unhandled-rejection filter', () => {
429
538
430
539
it ( 'should pass through rejections from non-aborted prerender contexts' , async ( ) => {
431
540
async function testForWorker ( ) {
432
- process . env . NEXT_USE_UNHANDLED_REJECTION_FILTER = '1'
541
+ process . env . NEXT_UNHANDLED_REJECTION_FILTER = '1'
433
542
require ( 'next/dist/server/node-environment-extensions/unhandled-rejection' )
434
543
435
544
const {
@@ -472,7 +581,7 @@ describe('unhandled-rejection filter', () => {
472
581
473
582
it ( 'should call console.error when no handlers are present' , async ( ) => {
474
583
async function testForWorker ( ) {
475
- process . env . NEXT_USE_UNHANDLED_REJECTION_FILTER = '1'
584
+ process . env . NEXT_UNHANDLED_REJECTION_FILTER = '1'
476
585
require ( 'next/dist/server/node-environment-extensions/unhandled-rejection' )
477
586
478
587
console . error = ( ...args : Array < any > ) => {
@@ -493,7 +602,7 @@ describe('unhandled-rejection filter', () => {
493
602
describe ( 'process method interception' , ( ) => {
494
603
it ( 'should handle process.once listeners correctly' , async ( ) => {
495
604
async function testForWorker ( ) {
496
- process . env . NEXT_USE_UNHANDLED_REJECTION_FILTER = 'enabled'
605
+ process . env . NEXT_UNHANDLED_REJECTION_FILTER = 'enabled'
497
606
require ( 'next/dist/server/node-environment-extensions/unhandled-rejection' )
498
607
499
608
let callCount = 0
@@ -528,7 +637,7 @@ describe('unhandled-rejection filter', () => {
528
637
529
638
it ( 'should handle process.removeListener correctly' , async ( ) => {
530
639
async function testForWorker ( ) {
531
- process . env . NEXT_USE_UNHANDLED_REJECTION_FILTER = 'enabled'
640
+ process . env . NEXT_UNHANDLED_REJECTION_FILTER = 'enabled'
532
641
require ( 'next/dist/server/node-environment-extensions/unhandled-rejection' )
533
642
534
643
const handler1 = ( reason : unknown ) => {
@@ -598,7 +707,7 @@ describe('unhandled-rejection filter', () => {
598
707
599
708
it ( 'should uninstall filter when removeAllListeners() is called without arguments' , async ( ) => {
600
709
async function testForWorker ( ) {
601
- process . env . NEXT_USE_UNHANDLED_REJECTION_FILTER = 'enabled'
710
+ process . env . NEXT_UNHANDLED_REJECTION_FILTER = 'enabled'
602
711
require ( 'next/dist/server/node-environment-extensions/unhandled-rejection' )
603
712
604
713
const {
@@ -660,7 +769,7 @@ describe('unhandled-rejection filter', () => {
660
769
661
770
it ( 'should not uninstall filter when removeAllListeners("unhandledRejection") is called' , async ( ) => {
662
771
async function testForWorker ( ) {
663
- process . env . NEXT_USE_UNHANDLED_REJECTION_FILTER = 'enabled'
772
+ process . env . NEXT_UNHANDLED_REJECTION_FILTER = 'enabled'
664
773
require ( 'next/dist/server/node-environment-extensions/unhandled-rejection' )
665
774
666
775
const {
@@ -719,7 +828,7 @@ describe('unhandled-rejection filter', () => {
719
828
// an event is emitted will be invoked regardless of whether there are mutations to the listeners
720
829
// during event handling.
721
830
async function testForWorker ( ) {
722
- process . env . NEXT_USE_UNHANDLED_REJECTION_FILTER = 'enabled'
831
+ process . env . NEXT_UNHANDLED_REJECTION_FILTER = 'enabled'
723
832
require ( 'next/dist/server/node-environment-extensions/unhandled-rejection' )
724
833
725
834
const onceHandler = ( reason : unknown ) => {
@@ -825,7 +934,7 @@ describe('unhandled-rejection filter', () => {
825
934
const originalToStrings = originalMethods . map ( ( m ) => m . toString ( ) )
826
935
const originalNames = originalMethods . map ( ( m ) => m . name )
827
936
828
- process . env . NEXT_USE_UNHANDLED_REJECTION_FILTER = 'enabled'
937
+ process . env . NEXT_UNHANDLED_REJECTION_FILTER = 'enabled'
829
938
require ( 'next/dist/server/node-environment-extensions/unhandled-rejection' )
830
939
831
940
const patchedMethods = [
@@ -885,7 +994,7 @@ describe('unhandled-rejection filter', () => {
885
994
describe ( 'error handling in handlers' , ( ) => {
886
995
it ( 'should handle errors thrown by user handlers gracefully' , async ( ) => {
887
996
async function testForWorker ( ) {
888
- process . env . NEXT_USE_UNHANDLED_REJECTION_FILTER = 'enabled'
997
+ process . env . NEXT_UNHANDLED_REJECTION_FILTER = 'enabled'
889
998
require ( 'next/dist/server/node-environment-extensions/unhandled-rejection' )
890
999
891
1000
const {
@@ -929,7 +1038,7 @@ describe('unhandled-rejection filter', () => {
929
1038
reportResult ( { type : 'uhr' , reason : `existing: ${ String ( reason ) } ` } )
930
1039
} )
931
1040
932
- process . env . NEXT_USE_UNHANDLED_REJECTION_FILTER = 'enabled'
1041
+ process . env . NEXT_UNHANDLED_REJECTION_FILTER = 'enabled'
933
1042
require ( 'next/dist/server/node-environment-extensions/unhandled-rejection' )
934
1043
935
1044
const {
@@ -971,7 +1080,7 @@ describe('unhandled-rejection filter', () => {
971
1080
reportResult ( { type : 'uhr' , reason : `existing: ${ String ( reason ) } ` } )
972
1081
} )
973
1082
974
- process . env . NEXT_USE_UNHANDLED_REJECTION_FILTER = 'enabled'
1083
+ process . env . NEXT_UNHANDLED_REJECTION_FILTER = 'enabled'
975
1084
require ( 'next/dist/server/node-environment-extensions/unhandled-rejection' )
976
1085
977
1086
process . removeAllListeners ( 'unhandledRejection' )
0 commit comments