@@ -14,10 +14,10 @@ import type {
14
14
import { normalize } from '@sentry/core' ;
15
15
import { createBasicSentryServer } from '@sentry-internal/test-utils' ;
16
16
import { execSync , spawn , spawnSync } from 'child_process' ;
17
- import { existsSync , mkdirSync , readFileSync , rmSync , unlinkSync , writeFileSync } from 'fs' ;
17
+ import { existsSync , mkdirSync , readFileSync , rmSync , writeFileSync } from 'fs' ;
18
18
import { basename , join } from 'path' ;
19
19
import { inspect } from 'util' ;
20
- import { afterAll , beforeAll , describe , test } from 'vitest' ;
20
+ import { afterAll , describe , test } from 'vitest' ;
21
21
import {
22
22
assertEnvelopeHeader ,
23
23
assertSentryCheckIn ,
@@ -187,38 +187,29 @@ export function createEsmAndCjsTests(
187
187
throw new Error ( `Instrument file not found: ${ mjsInstrumentPath } ` ) ;
188
188
}
189
189
190
- // If additionalDependencies are provided, we create a dedicated tmp directory that includes
191
- // copied ESM & CJS scenario/instrument files and a nested package.json with those dependencies installed.
192
- const useTmpDir = Boolean ( options ?. additionalDependencies && Object . keys ( options . additionalDependencies ) . length > 0 ) ;
193
-
194
- let tmpDirPath : string | undefined ;
195
- let esmScenarioPathForRun = mjsScenarioPath ;
196
- let esmInstrumentPathForRun = mjsInstrumentPath ;
197
- let cjsScenarioPath = join ( cwd , `tmp_${ scenarioPath . replace ( '.mjs' , '.cjs' ) } ` ) ;
198
- let cjsInstrumentPath = join ( cwd , `tmp_${ instrumentPath . replace ( '.mjs' , '.cjs' ) } ` ) ;
199
-
200
- if ( useTmpDir ) {
201
- // Create unique tmp directory within the suite folder
202
- const uniqueId = `${ Date . now ( ) . toString ( 36 ) } _${ Math . random ( ) . toString ( 36 ) . slice ( 2 , 8 ) } ` ;
203
- tmpDirPath = join ( cwd , `tmp_${ uniqueId } ` ) ;
204
- mkdirSync ( tmpDirPath ) ;
205
-
206
- // Copy ESM files as-is into tmp dir
207
- const esmScenarioBasename = basename ( scenarioPath ) ;
208
- const esmInstrumentBasename = basename ( instrumentPath ) ;
209
- esmScenarioPathForRun = join ( tmpDirPath , esmScenarioBasename ) ;
210
- esmInstrumentPathForRun = join ( tmpDirPath , esmInstrumentBasename ) ;
211
- writeFileSync ( esmScenarioPathForRun , readFileSync ( mjsScenarioPath , 'utf8' ) ) ;
212
- writeFileSync ( esmInstrumentPathForRun , readFileSync ( mjsInstrumentPath , 'utf8' ) ) ;
213
-
214
- // Pre-create CJS converted files inside tmp dir
215
- cjsScenarioPath = join ( tmpDirPath , esmScenarioBasename . replace ( '.mjs' , '.cjs' ) ) ;
216
- cjsInstrumentPath = join ( tmpDirPath , esmInstrumentBasename . replace ( '.mjs' , '.cjs' ) ) ;
217
- convertEsmFileToCjs ( esmScenarioPathForRun , cjsScenarioPath ) ;
218
- convertEsmFileToCjs ( esmInstrumentPathForRun , cjsInstrumentPath ) ;
219
-
220
- // Create a minimal package.json with requested dependencies
221
- const additionalDependencies = options ?. additionalDependencies ?? { } ;
190
+ // Create a dedicated tmp directory that includes copied ESM & CJS scenario/instrument files.
191
+ // If additionalDependencies are provided, we also create a nested package.json and install them there.
192
+ const uniqueId = `${ Date . now ( ) . toString ( 36 ) } _${ Math . random ( ) . toString ( 36 ) . slice ( 2 , 8 ) } ` ;
193
+ const tmpDirPath = join ( cwd , `tmp_${ uniqueId } ` ) ;
194
+ mkdirSync ( tmpDirPath ) ;
195
+
196
+ // Copy ESM files as-is into tmp dir
197
+ const esmScenarioBasename = basename ( scenarioPath ) ;
198
+ const esmInstrumentBasename = basename ( instrumentPath ) ;
199
+ const esmScenarioPathForRun = join ( tmpDirPath , esmScenarioBasename ) ;
200
+ const esmInstrumentPathForRun = join ( tmpDirPath , esmInstrumentBasename ) ;
201
+ writeFileSync ( esmScenarioPathForRun , readFileSync ( mjsScenarioPath , 'utf8' ) ) ;
202
+ writeFileSync ( esmInstrumentPathForRun , readFileSync ( mjsInstrumentPath , 'utf8' ) ) ;
203
+
204
+ // Pre-create CJS converted files inside tmp dir
205
+ const cjsScenarioPath = join ( tmpDirPath , esmScenarioBasename . replace ( '.mjs' , '.cjs' ) ) ;
206
+ const cjsInstrumentPath = join ( tmpDirPath , esmInstrumentBasename . replace ( '.mjs' , '.cjs' ) ) ;
207
+ convertEsmFileToCjs ( esmScenarioPathForRun , cjsScenarioPath ) ;
208
+ convertEsmFileToCjs ( esmInstrumentPathForRun , cjsInstrumentPath ) ;
209
+
210
+ // Create a minimal package.json with requested dependencies (if any) and install them
211
+ const additionalDependencies = options ?. additionalDependencies ?? { } ;
212
+ if ( Object . keys ( additionalDependencies ) . length > 0 ) {
222
213
const packageJson = {
223
214
name : 'tmp-integration-test' ,
224
215
private : true ,
@@ -228,7 +219,6 @@ export function createEsmAndCjsTests(
228
219
229
220
writeFileSync ( join ( tmpDirPath , 'package.json' ) , JSON . stringify ( packageJson , null , 2 ) ) ;
230
221
231
- // Install the requested dependencies using yarn
232
222
try {
233
223
const deps = Object . entries ( additionalDependencies ) . map ( ( [ name , range ] ) => {
234
224
if ( ! range || typeof range !== 'string' ) {
@@ -238,11 +228,30 @@ export function createEsmAndCjsTests(
238
228
} ) ;
239
229
240
230
if ( deps . length > 0 ) {
241
- // Run yarn add non-interactively, keep output visible when DEBUG is set
242
- spawnSync ( 'yarn' , [ 'add' , '--non-interactive' , ...deps ] , {
231
+ const result = spawnSync ( 'yarn' , [ 'add' , '--non-interactive' , ...deps ] , {
243
232
cwd : tmpDirPath ,
244
- stdio : process . env . DEBUG ? 'inherit' : 'ignore ',
233
+ encoding : 'utf8 ',
245
234
} ) ;
235
+
236
+ if ( process . env . DEBUG ) {
237
+ // eslint-disable-next-line no-console
238
+ console . log ( '[additionalDependencies]' , deps . join ( ' ' ) ) ;
239
+ // eslint-disable-next-line no-console
240
+ console . log ( '[yarn stdout]' , result . stdout ) ;
241
+ // eslint-disable-next-line no-console
242
+ console . log ( '[yarn stderr]' , result . stderr ) ;
243
+ }
244
+
245
+ if ( result . error ) {
246
+ throw new Error ( `Failed to install additionalDependencies in tmp dir ${ tmpDirPath } : ${ result . error . message } ` ) ;
247
+ }
248
+ if ( typeof result . status === 'number' && result . status !== 0 ) {
249
+ throw new Error (
250
+ `Failed to install additionalDependencies in tmp dir ${ tmpDirPath } (exit ${ result . status } ):\n${
251
+ result . stderr || result . stdout || '(no output)'
252
+ } `,
253
+ ) ;
254
+ }
246
255
}
247
256
} catch ( e ) {
248
257
// eslint-disable-next-line no-console
@@ -257,40 +266,17 @@ export function createEsmAndCjsTests(
257
266
} ) ;
258
267
259
268
describe ( 'cjs' , ( ) => {
260
- if ( ! useTmpDir ) {
261
- beforeAll ( ( ) => {
262
- // For the CJS runner, we create some temporary files...
263
- convertEsmFileToCjs ( mjsScenarioPath , cjsScenarioPath ) ;
264
- convertEsmFileToCjs ( mjsInstrumentPath , cjsInstrumentPath ) ;
265
- } ) ;
266
-
267
- afterAll ( ( ) => {
268
- try {
269
- unlinkSync ( cjsInstrumentPath ) ;
270
- } catch {
271
- // Ignore errors here
272
- }
273
- try {
274
- unlinkSync ( cjsScenarioPath ) ;
275
- } catch {
276
- // Ignore errors here
269
+ // Clean up the tmp directory once CJS tests are finished
270
+ afterAll ( ( ) => {
271
+ try {
272
+ rmSync ( tmpDirPath , { recursive : true , force : true } ) ;
273
+ } catch {
274
+ if ( process . env . DEBUG ) {
275
+ // eslint-disable-next-line no-console
276
+ console . error ( `Failed to remove tmp dir: ${ tmpDirPath } ` ) ;
277
277
}
278
- } ) ;
279
- } else {
280
- // When using a tmp dir, clean up the entire directory once CJS tests are finished
281
- afterAll ( ( ) => {
282
- if ( tmpDirPath ) {
283
- try {
284
- rmSync ( tmpDirPath , { recursive : true , force : true } ) ;
285
- } catch {
286
- if ( process . env . DEBUG ) {
287
- // eslint-disable-next-line no-console
288
- console . error ( `Failed to remove tmp dir: ${ tmpDirPath } ` ) ;
289
- }
290
- }
291
- }
292
- } ) ;
293
- }
278
+ }
279
+ } ) ;
294
280
295
281
const testFn = options ?. failsOnCjs ? test . fails : test ;
296
282
callback ( ( ) => createRunner ( cjsScenarioPath ) . withFlags ( '--require' , cjsInstrumentPath ) , testFn , 'cjs' ) ;
0 commit comments