@@ -13,10 +13,11 @@ import type {
1313} from '@sentry/core' ;
1414import { normalize } from '@sentry/core' ;
1515import { createBasicSentryServer } from '@sentry-internal/test-utils' ;
16- import { execSync , spawn , spawnSync } from 'child_process' ;
17- import { cpSync , existsSync , mkdirSync , readFileSync , rmSync , writeFileSync } from 'fs' ;
16+ import { exec , execSync , spawn , spawnSync } from 'child_process' ;
17+ import { existsSync } from 'fs' ;
18+ import { cp , mkdir , readFile , rm , writeFile } from 'fs/promises' ;
1819import { basename , join } from 'path' ;
19- import { inspect } from 'util' ;
20+ import { inspect , promisify } from 'util' ;
2021import { afterAll , beforeAll , describe , test } from 'vitest' ;
2122import {
2223 assertEnvelopeHeader ,
@@ -29,6 +30,8 @@ import {
2930 assertSentryTransaction ,
3031} from './assertions' ;
3132
33+ const execPromise = promisify ( exec ) ;
34+
3235const CLEANUP_STEPS = new Set < VoidFunction > ( ) ;
3336
3437export function cleanupChildProcesses ( ) : void {
@@ -215,21 +218,21 @@ export function createEsmAndCjsTests(
215218 const cjsScenarioPath = join ( tmpDirPath , esmScenarioBasename . replace ( '.mjs' , '.cjs' ) ) ;
216219 const cjsInstrumentPath = join ( tmpDirPath , esmInstrumentBasename . replace ( '.mjs' , '.cjs' ) ) ;
217220
218- function createTmpDir ( ) : void {
219- mkdirSync ( tmpDirPath ) ;
221+ async function createTmpDir ( ) : Promise < void > {
222+ await mkdir ( tmpDirPath ) ;
220223
221224 // Copy ESM files as-is into tmp dir
222- writeFileSync ( esmScenarioPathForRun , readFileSync ( mjsScenarioPath , 'utf8' ) ) ;
223- writeFileSync ( esmInstrumentPathForRun , readFileSync ( mjsInstrumentPath , 'utf8' ) ) ;
225+ await writeFile ( esmScenarioPathForRun , await readFile ( mjsScenarioPath , 'utf8' ) ) ;
226+ await writeFile ( esmInstrumentPathForRun , await readFile ( mjsInstrumentPath , 'utf8' ) ) ;
224227
225228 // Pre-create CJS converted files inside tmp dir
226- convertEsmFileToCjs ( esmScenarioPathForRun , cjsScenarioPath ) ;
227- convertEsmFileToCjs ( esmInstrumentPathForRun , cjsInstrumentPath ) ;
229+ await convertEsmFileToCjs ( esmScenarioPathForRun , cjsScenarioPath ) ;
230+ await convertEsmFileToCjs ( esmInstrumentPathForRun , cjsInstrumentPath ) ;
228231
229232 // Copy any additional files/dirs into tmp dir
230233 if ( options ?. copyPaths ) {
231234 for ( const path of options . copyPaths ) {
232- cpSync ( join ( cwd , path ) , join ( tmpDirPath , path ) , { recursive : true } ) ;
235+ await cp ( join ( cwd , path ) , join ( tmpDirPath , path ) , { recursive : true } ) ;
233236 }
234237 }
235238
@@ -243,7 +246,7 @@ export function createEsmAndCjsTests(
243246 dependencies : additionalDependencies ,
244247 } as const ;
245248
246- writeFileSync ( join ( tmpDirPath , 'package.json' ) , JSON . stringify ( packageJson , null , 2 ) ) ;
249+ await writeFile ( join ( tmpDirPath , 'package.json' ) , JSON . stringify ( packageJson , null , 2 ) ) ;
247250
248251 try {
249252 const deps = Object . entries ( additionalDependencies ) . map ( ( [ name , range ] ) => {
@@ -254,33 +257,24 @@ export function createEsmAndCjsTests(
254257 } ) ;
255258
256259 if ( deps . length > 0 ) {
257- // Prefer npm for temp installs to avoid Yarn engine strictness; see https://github.com/vercel/ai/issues/7777
258- // We rely on the generated package.json dependencies and run a plain install.
259- const result = spawnSync ( 'npm' , [ 'install' , '--silent' , '--no-audit' , '--no-fund' ] , {
260- cwd : tmpDirPath ,
261- encoding : 'utf8' ,
262- } ) ;
263-
264- if ( process . env . DEBUG ) {
265- // eslint-disable-next-line no-console
266- console . log ( '[additionalDependencies via npm]' , deps . join ( ' ' ) ) ;
267- // eslint-disable-next-line no-console
268- console . log ( '[npm stdout]' , result . stdout ) ;
269- // eslint-disable-next-line no-console
270- console . log ( '[npm stderr]' , result . stderr ) ;
271- }
260+ try {
261+ // Prefer npm for temp installs to avoid Yarn engine strictness; see https://github.com/vercel/ai/issues/7777
262+ // We rely on the generated package.json dependencies and run a plain install.
263+ const { stdout, stderr } = await execPromise ( 'npm install --silent --no-audit --no-fund' , {
264+ cwd : tmpDirPath ,
265+ encoding : 'utf8' ,
266+ } ) ;
272267
273- if ( result . error ) {
274- throw new Error (
275- `Failed to install additionalDependencies in tmp dir ${ tmpDirPath } : ${ result . error . message } ` ,
276- ) ;
277- }
278- if ( typeof result . status === 'number' && result . status !== 0 ) {
279- throw new Error (
280- `Failed to install additionalDependencies in tmp dir ${ tmpDirPath } (exit ${ result . status } ):\n${
281- result . stderr || result . stdout || '(no output)'
282- } `,
283- ) ;
268+ if ( process . env . DEBUG ) {
269+ // eslint-disable-next-line no-console
270+ console . log ( '[additionalDependencies via npm]' , deps . join ( ' ' ) ) ;
271+ // eslint-disable-next-line no-console
272+ console . log ( '[npm stdout]' , stdout ) ;
273+ // eslint-disable-next-line no-console
274+ console . log ( '[npm stderr]' , stderr ) ;
275+ }
276+ } catch ( error ) {
277+ throw new Error ( `Failed to install additionalDependencies in tmp dir ${ tmpDirPath } : ${ error } ` ) ;
284278 }
285279 }
286280 } catch ( e ) {
@@ -313,31 +307,31 @@ export function createEsmAndCjsTests(
313307 } ) ;
314308
315309 // Create tmp directory
316- beforeAll ( ( ) => {
317- createTmpDir ( ) ;
318- } ) ;
310+ beforeAll ( async ( ) => {
311+ await createTmpDir ( ) ;
312+ } , 60_000 ) ;
319313
320314 // Clean up the tmp directory after both esm and cjs suites have run
321- afterAll ( ( ) => {
315+ afterAll ( async ( ) => {
322316 // First do cleanup!
323317 cleanupChildProcesses ( ) ;
324318
325319 try {
326- rmSync ( tmpDirPath , { recursive : true , force : true } ) ;
320+ await rm ( tmpDirPath , { recursive : true , force : true } ) ;
327321 } catch {
328322 if ( process . env . DEBUG ) {
329323 // eslint-disable-next-line no-console
330324 console . error ( `Failed to remove tmp dir: ${ tmpDirPath } ` ) ;
331325 }
332326 }
333- } ) ;
327+ } , 30_000 ) ;
334328 } ) ;
335329}
336330
337- function convertEsmFileToCjs ( inputPath : string , outputPath : string ) : void {
338- const cjsFileContent = readFileSync ( inputPath , 'utf8' ) ;
331+ async function convertEsmFileToCjs ( inputPath : string , outputPath : string ) : Promise < void > {
332+ const cjsFileContent = await readFile ( inputPath , 'utf8' ) ;
339333 const cjsFileContentConverted = convertEsmToCjs ( cjsFileContent ) ;
340- writeFileSync ( outputPath , cjsFileContentConverted ) ;
334+ return writeFile ( outputPath , cjsFileContentConverted ) ;
341335}
342336
343337/** Creates a test runner */
0 commit comments