@@ -31,6 +31,9 @@ for (const contextManagerClass of [
3131 | AsyncHooksContextManager
3232 | AsyncLocalStorageContextManager ;
3333 const key1 = createContextKey ( 'test key 1' ) ;
34+ let otherContextManager :
35+ | AsyncHooksContextManager
36+ | AsyncLocalStorageContextManager ;
3437
3538 before ( function ( ) {
3639 if (
@@ -49,6 +52,7 @@ for (const contextManagerClass of [
4952
5053 afterEach ( ( ) => {
5154 contextManager . disable ( ) ;
55+ otherContextManager ?. disable ( ) ;
5256 } ) ;
5357
5458 describe ( '.enable()' , ( ) => {
@@ -274,6 +278,22 @@ for (const contextManagerClass of [
274278 countDown ( ) ;
275279 } , time2 ) ;
276280 } ) ;
281+
282+ it ( 'should not influence other instances' , ( ) => {
283+ otherContextManager = new contextManagerClass ( ) ;
284+ otherContextManager . enable ( ) ;
285+
286+ const context = ROOT_CONTEXT . setValue ( key1 , 2 ) ;
287+ const otherContext = ROOT_CONTEXT . setValue ( key1 , 3 ) ;
288+ contextManager . with ( context , ( ) => {
289+ assert . strictEqual ( contextManager . active ( ) , context ) ;
290+ assert . strictEqual ( otherContextManager . active ( ) , ROOT_CONTEXT ) ;
291+ otherContextManager . with ( otherContext , ( ) => {
292+ assert . strictEqual ( contextManager . active ( ) , context ) ;
293+ assert . strictEqual ( otherContextManager . active ( ) , otherContext ) ;
294+ } ) ;
295+ } ) ;
296+ } ) ;
277297 } ) ;
278298
279299 describe ( '.bind(function)' , ( ) => {
@@ -335,6 +355,22 @@ for (const contextManagerClass of [
335355 } , context ) ;
336356 fn ( ) ;
337357 } ) ;
358+
359+ it ( 'should not influence other instances' , ( ) => {
360+ otherContextManager = new contextManagerClass ( ) ;
361+ otherContextManager . enable ( ) ;
362+
363+ const context = ROOT_CONTEXT . setValue ( key1 , 2 ) ;
364+ const otherContext = ROOT_CONTEXT . setValue ( key1 , 3 ) ;
365+ const fn = otherContextManager . bind (
366+ contextManager . bind ( ( ) => {
367+ assert . strictEqual ( contextManager . active ( ) , context ) ;
368+ assert . strictEqual ( otherContextManager . active ( ) , otherContext ) ;
369+ } , context ) ,
370+ otherContext
371+ ) ;
372+ fn ( ) ;
373+ } ) ;
338374 } ) ;
339375
340376 describe ( '.bind(event-emitter)' , ( ) => {
@@ -352,31 +388,31 @@ for (const contextManagerClass of [
352388 it ( 'should return current context and removeListener (when enabled)' , done => {
353389 const ee = new EventEmitter ( ) ;
354390 const context = ROOT_CONTEXT . setValue ( key1 , 1 ) ;
355- const patchedEe = contextManager . bind ( ee , context ) ;
391+ const patchedEE = contextManager . bind ( ee , context ) ;
356392 const handler = ( ) => {
357393 assert . deepStrictEqual ( contextManager . active ( ) , context ) ;
358- patchedEe . removeListener ( 'test' , handler ) ;
359- assert . strictEqual ( patchedEe . listeners ( 'test' ) . length , 0 ) ;
394+ patchedEE . removeListener ( 'test' , handler ) ;
395+ assert . strictEqual ( patchedEE . listeners ( 'test' ) . length , 0 ) ;
360396 return done ( ) ;
361397 } ;
362- patchedEe . on ( 'test' , handler ) ;
363- assert . strictEqual ( patchedEe . listeners ( 'test' ) . length , 1 ) ;
364- patchedEe . emit ( 'test' ) ;
398+ patchedEE . on ( 'test' , handler ) ;
399+ assert . strictEqual ( patchedEE . listeners ( 'test' ) . length , 1 ) ;
400+ patchedEE . emit ( 'test' ) ;
365401 } ) ;
366402
367403 it ( 'should return current context and removeAllListener (when enabled)' , done => {
368404 const ee = new EventEmitter ( ) ;
369405 const context = ROOT_CONTEXT . setValue ( key1 , 1 ) ;
370- const patchedEe = contextManager . bind ( ee , context ) ;
406+ const patchedEE = contextManager . bind ( ee , context ) ;
371407 const handler = ( ) => {
372408 assert . deepStrictEqual ( contextManager . active ( ) , context ) ;
373- patchedEe . removeAllListeners ( 'test' ) ;
374- assert . strictEqual ( patchedEe . listeners ( 'test' ) . length , 0 ) ;
409+ patchedEE . removeAllListeners ( 'test' ) ;
410+ assert . strictEqual ( patchedEE . listeners ( 'test' ) . length , 0 ) ;
375411 return done ( ) ;
376412 } ;
377- patchedEe . on ( 'test' , handler ) ;
378- assert . strictEqual ( patchedEe . listeners ( 'test' ) . length , 1 ) ;
379- patchedEe . emit ( 'test' ) ;
413+ patchedEE . on ( 'test' , handler ) ;
414+ assert . strictEqual ( patchedEE . listeners ( 'test' ) . length , 1 ) ;
415+ patchedEE . emit ( 'test' ) ;
380416 } ) ;
381417
382418 /**
@@ -387,34 +423,54 @@ for (const contextManagerClass of [
387423 contextManager . disable ( ) ;
388424 const ee = new EventEmitter ( ) ;
389425 const context = ROOT_CONTEXT . setValue ( key1 , 1 ) ;
390- const patchedEe = contextManager . bind ( ee , context ) ;
426+ const patchedEE = contextManager . bind ( ee , context ) ;
391427 const handler = ( ) => {
392428 assert . deepStrictEqual ( contextManager . active ( ) , context ) ;
393- patchedEe . removeListener ( 'test' , handler ) ;
394- assert . strictEqual ( patchedEe . listeners ( 'test' ) . length , 0 ) ;
429+ patchedEE . removeListener ( 'test' , handler ) ;
430+ assert . strictEqual ( patchedEE . listeners ( 'test' ) . length , 0 ) ;
395431 return done ( ) ;
396432 } ;
397- patchedEe . on ( 'test' , handler ) ;
398- assert . strictEqual ( patchedEe . listeners ( 'test' ) . length , 1 ) ;
399- patchedEe . emit ( 'test' ) ;
433+ patchedEE . on ( 'test' , handler ) ;
434+ assert . strictEqual ( patchedEE . listeners ( 'test' ) . length , 1 ) ;
435+ patchedEE . emit ( 'test' ) ;
400436 } ) ;
401437
402438 it ( 'should not return current context with async op' , done => {
403439 const ee = new EventEmitter ( ) ;
404440 const context = ROOT_CONTEXT . setValue ( key1 , 1 ) ;
405- const patchedEe = contextManager . bind ( ee , context ) ;
441+ const patchedEE = contextManager . bind ( ee , context ) ;
406442 const handler = ( ) => {
407443 assert . deepStrictEqual ( contextManager . active ( ) , context ) ;
408444 setImmediate ( ( ) => {
409445 assert . deepStrictEqual ( contextManager . active ( ) , context ) ;
410- patchedEe . removeAllListeners ( 'test' ) ;
411- assert . strictEqual ( patchedEe . listeners ( 'test' ) . length , 0 ) ;
446+ patchedEE . removeAllListeners ( 'test' ) ;
447+ assert . strictEqual ( patchedEE . listeners ( 'test' ) . length , 0 ) ;
412448 return done ( ) ;
413449 } ) ;
414450 } ;
415- patchedEe . on ( 'test' , handler ) ;
416- assert . strictEqual ( patchedEe . listeners ( 'test' ) . length , 1 ) ;
417- patchedEe . emit ( 'test' ) ;
451+ patchedEE . on ( 'test' , handler ) ;
452+ assert . strictEqual ( patchedEE . listeners ( 'test' ) . length , 1 ) ;
453+ patchedEE . emit ( 'test' ) ;
454+ } ) ;
455+
456+ it ( 'should not influence other instances' , ( ) => {
457+ const ee = new EventEmitter ( ) ;
458+ otherContextManager = new contextManagerClass ( ) ;
459+ otherContextManager . enable ( ) ;
460+
461+ const context = ROOT_CONTEXT . setValue ( key1 , 2 ) ;
462+ const otherContext = ROOT_CONTEXT . setValue ( key1 , 3 ) ;
463+ const patchedEE = otherContextManager . bind (
464+ contextManager . bind ( ee , context ) ,
465+ otherContext
466+ ) ;
467+ const handler = ( ) => {
468+ assert . strictEqual ( contextManager . active ( ) , context ) ;
469+ assert . strictEqual ( otherContextManager . active ( ) , otherContext ) ;
470+ } ;
471+
472+ patchedEE . on ( 'test' , handler ) ;
473+ patchedEE . emit ( 'test' ) ;
418474 } ) ;
419475 } ) ;
420476 } ) ;
0 commit comments