@@ -31,7 +31,12 @@ delete process.env.DD_INJECT_FORCE
3131
3232function testInjectionScenarios ( arg , filename , esmWorks = false ) {
3333 if ( ! currentVersionIsSupported ) return
34- const doTest = ( file , ...args ) => testFile ( file , ...args )
34+
35+ // For `--loader`, we generally want ESM fixtures to ensure the loader hook actually applies.
36+ // However, Node 18.0.0 is a known outlier where ESM via custom loaders is not supportable.
37+ const isNode1800 = process . versions . node === '18.0.0'
38+ const tracerFile = arg === 'loader' && ! isNode1800 ? 'init/trace.mjs' : 'init/trace.js'
39+ const instrFile = arg === 'loader' && ! isNode1800 ? 'init/instrument.mjs' : 'init/instrument.js'
3540
3641 context ( 'preferring app-dir dd-trace' , ( ) => {
3742 context ( 'when dd-trace is not in the app dir' , ( ) => {
@@ -40,23 +45,23 @@ function testInjectionScenarios (arg, filename, esmWorks = false) {
4045
4146 if ( currentVersionIsSupported ) {
4247 context ( 'without DD_INJECTION_ENABLED' , ( ) => {
43- it ( 'should initialize the tracer' , ( ) => doTest ( 'init/trace.js' , 'true\n' , [ ] , 'manual' ) )
48+ it ( 'should initialize the tracer' , ( ) => testFile ( tracerFile , 'true\n' , [ ] , 'manual' ) )
4449
45- it ( 'should initialize instrumentation' , ( ) => doTest ( 'init/instrument.js' , 'true\n' , [ ] , 'manual' ) )
50+ it ( 'should initialize instrumentation' , ( ) => testFile ( instrFile , 'true\n' , [ ] , 'manual' ) )
4651
4752 it ( `should ${ esmWorks ? '' : 'not ' } initialize ESM instrumentation` , ( ) =>
48- doTest ( 'init/instrument.mjs' , `${ esmWorks } \n` , [ ] , 'manual' ) )
53+ testFile ( 'init/instrument.mjs' , `${ esmWorks } \n` , [ ] , 'manual' ) )
4954 } )
5055 }
5156
5257 context ( 'with DD_INJECTION_ENABLED' , ( ) => {
5358 useEnv ( { DD_INJECTION_ENABLED } )
5459
55- it ( 'should not initialize the tracer' , ( ) => doTest ( 'init/trace.js' , 'false\n' , [ ] ) )
60+ it ( 'should not initialize the tracer' , ( ) => testFile ( tracerFile , 'false\n' , [ ] , '' ) )
5661
57- it ( 'should not initialize instrumentation' , ( ) => doTest ( 'init/instrument.js' , 'false\n' , [ ] ) )
62+ it ( 'should not initialize instrumentation' , ( ) => testFile ( instrFile , 'false\n' , [ ] , '' ) )
5863
59- it ( 'should not initialize ESM instrumentation' , ( ) => doTest ( 'init/instrument.mjs' , 'false\n' , [ ] ) )
64+ it ( 'should not initialize ESM instrumentation' , ( ) => testFile ( 'init/instrument.mjs' , 'false\n' , [ ] , '' ) )
6065 } )
6166 } )
6267
@@ -65,23 +70,23 @@ function testInjectionScenarios (arg, filename, esmWorks = false) {
6570 useEnv ( { NODE_OPTIONS } )
6671
6772 context ( 'without DD_INJECTION_ENABLED' , ( ) => {
68- it ( 'should initialize the tracer' , ( ) => doTest ( 'init/trace.js' , 'true\n' , [ ] , 'manual' ) )
73+ it ( 'should initialize the tracer' , ( ) => testFile ( tracerFile , 'true\n' , [ ] , 'manual' ) )
6974
70- it ( 'should initialize instrumentation' , ( ) => doTest ( 'init/instrument.js' , 'true\n' , [ ] , 'manual' ) )
75+ it ( 'should initialize instrumentation' , ( ) => testFile ( instrFile , 'true\n' , [ ] , 'manual' ) )
7176
7277 it ( `should ${ esmWorks ? '' : 'not ' } initialize ESM instrumentation` , ( ) =>
73- doTest ( 'init/instrument.mjs' , `${ esmWorks } \n` , [ ] , 'manual' ) )
78+ testFile ( 'init/instrument.mjs' , `${ esmWorks } \n` , [ ] , 'manual' ) )
7479 } )
7580
7681 context ( 'with DD_INJECTION_ENABLED' , ( ) => {
7782 useEnv ( { DD_INJECTION_ENABLED , DD_TRACE_DEBUG } )
7883
79- it ( 'should initialize the tracer' , ( ) => doTest ( 'init/trace.js' , 'true\n' , telemetryGood , 'ssi' ) )
84+ it ( 'should initialize the tracer' , ( ) => testFile ( tracerFile , 'true\n' , telemetryGood , 'ssi' ) )
8085
81- it ( 'should initialize instrumentation' , ( ) => doTest ( 'init/instrument.js' , 'true\n' , telemetryGood , 'ssi' ) )
86+ it ( 'should initialize instrumentation' , ( ) => testFile ( instrFile , 'true\n' , telemetryGood , 'ssi' ) )
8287
8388 it ( `should ${ esmWorks ? '' : 'not ' } initialize ESM instrumentation` , ( ) =>
84- doTest ( 'init/instrument.mjs' , `${ esmWorks } \n` , telemetryGood , 'ssi' ) )
89+ testFile ( 'init/instrument.mjs' , `${ esmWorks } \n` , telemetryGood , 'ssi' ) )
8590 } )
8691 } )
8792 } )
@@ -90,11 +95,13 @@ function testInjectionScenarios (arg, filename, esmWorks = false) {
9095function testRuntimeVersionChecks ( arg , filename ) {
9196 context ( 'runtime version check' , ( ) => {
9297 const NODE_OPTIONS = `--${ arg } dd-trace/${ filename } `
93- const doTest = ( ...args ) => testFile ( 'init/trace.js' , ...args )
94- const doTestForced = async ( ...args ) => {
98+ const entryFile = arg === 'loader' ? 'init/trace.mjs' : 'init/trace.js'
99+ const doTest = ( expectedOut , expectedTelemetryPoints , expectedSource ) =>
100+ testFile ( entryFile , expectedOut , expectedTelemetryPoints , expectedSource )
101+ const doTestForced = async ( expectedOut , expectedTelemetryPoints , expectedSource ) => {
95102 Object . assign ( process . env , { DD_INJECT_FORCE } )
96103 try {
97- await testFile ( 'init/trace.js' , ... args )
104+ await testFile ( entryFile , expectedOut , expectedTelemetryPoints , expectedSource )
98105 } finally {
99106 delete process . env . DD_INJECT_FORCE
100107 }
@@ -279,7 +286,7 @@ if (semver.satisfies(process.versions.node, '>=14.13.1')) {
279286 if ( semver . satisfies ( process . versions . node , '>=20.6.0' ) ) {
280287 context ( 'as --import' , ( ) => {
281288 testInjectionScenarios ( 'import' , 'initialize.mjs' , true )
282- testRuntimeVersionChecks ( 'loader ' , 'initialize.mjs' )
289+ testRuntimeVersionChecks ( 'import ' , 'initialize.mjs' )
283290 } )
284291 }
285292 } )
0 commit comments