@@ -48,18 +48,20 @@ describe('Plugin', () => {
4848 let expectedConsumerHash
4949
5050 describe ( 'without configuration' , ( ) => {
51- beforeEach ( ( ) => {
51+ beforeEach ( async ( ) => {
5252 const msg = `[DD-PUBSUB-TEST] ======================================== Loading google-cloud-pubsub plugin at ${ new Date ( ) . toISOString ( ) } ========================================`
5353 console . log ( msg )
5454 process . stdout . write ( msg + '\n' )
55- return agent . load ( 'google-cloud-pubsub' , { dsmEnabled : false } )
56- } )
57-
58- beforeEach ( ( ) => {
59- const msg = `[DD-PUBSUB-TEST] Initializing test environment for version: ${ version } `
60- console . log ( msg )
61- process . stdout . write ( msg + '\n' )
6255
56+ // CRITICAL: Load instrumentation BEFORE requiring @google -cloud/pubsub
57+ // This ensures addHook() wrappers attach before the module is cached
58+ await agent . load ( 'google-cloud-pubsub' , { dsmEnabled : false } )
59+
60+ const initMsg = `[DD-PUBSUB-TEST] Initializing test environment for version: ${ version } `
61+ console . log ( initMsg )
62+ process . stdout . write ( initMsg + '\n' )
63+
64+ // NOW require the library - hooks will attach
6365 tracer = require ( '../../dd-trace' )
6466 gax = require ( '../../../versions/google-gax@3.5.7' ) . get ( )
6567 const lib = require ( `../../../versions/@google-cloud/pubsub@${ version } ` ) . get ( )
@@ -362,14 +364,14 @@ describe('Plugin', () => {
362364 } )
363365
364366 describe ( 'with configuration' , ( ) => {
365- beforeEach ( ( ) => {
366- return agent . load ( 'google-cloud-pubsub' , {
367+ beforeEach ( async ( ) => {
368+ // Load instrumentation BEFORE requiring the library
369+ await agent . load ( 'google-cloud-pubsub' , {
367370 service : 'a_test_service' ,
368371 dsmEnabled : false
369372 } )
370- } )
371-
372- beforeEach ( ( ) => {
373+
374+ // NOW require the library - hooks will attach
373375 tracer = require ( '../../dd-trace' )
374376 const { PubSub } = require ( `../../../versions/@google-cloud/pubsub@${ version } ` ) . get ( )
375377 project = getProjectId ( )
@@ -396,13 +398,13 @@ describe('Plugin', () => {
396398 let sub
397399 let consume
398400
399- beforeEach ( ( ) => {
400- return agent . load ( 'google-cloud-pubsub' , {
401+ before ( async ( ) => {
402+ // Load instrumentation BEFORE requiring the library
403+ await agent . load ( 'google-cloud-pubsub' , {
401404 dsmEnabled : true
402405 } )
403- } )
404-
405- before ( async ( ) => {
406+
407+ // NOW require the library - hooks will attach
406408 const { PubSub } = require ( `../../../versions/@google-cloud/pubsub@${ version } ` ) . get ( )
407409 project = getProjectId ( )
408410 resource = `projects/${ project } /topics/${ dsmTopicName } `
0 commit comments