diff --git a/packages/jest-circus/package.json b/packages/jest-circus/package.json index 02c3599ab4e8..2ada179ed65d 100644 --- a/packages/jest-circus/package.json +++ b/packages/jest-circus/package.json @@ -47,7 +47,8 @@ "@types/graceful-fs": "^4.1.3", "@types/stack-utils": "^2.0.0", "execa": "^5.0.0", - "graceful-fs": "^4.2.9" + "graceful-fs": "^4.2.9", + "tempy": "^1.0.0" }, "engines": { "node": "^14.15.0 || ^16.10.0 || >=18.0.0" diff --git a/packages/jest-circus/src/__mocks__/testUtils.ts b/packages/jest-circus/src/__mocks__/testUtils.ts index 358c0bf8b685..fe903ffb3db6 100644 --- a/packages/jest-circus/src/__mocks__/testUtils.ts +++ b/packages/jest-circus/src/__mocks__/testUtils.ts @@ -5,11 +5,9 @@ * LICENSE file in the root directory of this source tree. */ -import {createHash} from 'crypto'; -import {tmpdir} from 'os'; -import * as path from 'path'; -import {ExecaSyncReturnValue, sync as spawnSync} from 'execa'; +import {sync as spawnSync} from 'execa'; import * as fs from 'graceful-fs'; +import tempy = require('tempy'); const CIRCUS_PATH = require.resolve('../').replace(/\\/g, '\\\\'); const CIRCUS_RUN_PATH = require.resolve('../run').replace(/\\/g, '\\\\'); @@ -21,20 +19,11 @@ const BABEL_REGISTER_PATH = require .resolve('@babel/register') .replace(/\\/g, '\\\\'); -interface Result extends ExecaSyncReturnValue { - status: number; - error: string; -} - export const runTest = ( source: string, opts?: {seed?: number; randomize?: boolean}, ) => { - const filename = createHash('sha1') - .update(source) - .digest('hex') - .substring(0, 32); - const tmpFilename = path.join(tmpdir(), filename); + const tmpFilename = tempy.file(); const content = ` require('${BABEL_REGISTER_PATH}')({extensions: [".js", ".ts"]}); @@ -62,25 +51,18 @@ export const runTest = ( fs.writeFileSync(tmpFilename, content); const result = spawnSync('node', [tmpFilename], { cwd: process.cwd(), - }) as Result; - - // For compat with cross-spawn - result.status = result.exitCode; + }); - if (result.status !== 0) { + if (result.exitCode !== 0) { const message = ` STDOUT: ${result.stdout && result.stdout.toString()} STDERR: ${result.stderr && result.stderr.toString()} - STATUS: ${result.status} - ERROR: ${String(result.error)} + STATUS: ${result.exitCode} `; throw new Error(message); } - result.stdout = String(result.stdout); - result.stderr = String(result.stderr); - - fs.unlinkSync(tmpFilename); + fs.rmSync(tmpFilename, {force: true}); if (result.stderr) { throw new Error( diff --git a/yarn.lock b/yarn.lock index ad64f226706d..0c08ce49ae00 100644 --- a/yarn.lock +++ b/yarn.lock @@ -12486,6 +12486,7 @@ __metadata: pure-rand: ^6.0.0 slash: ^3.0.0 stack-utils: ^2.0.3 + tempy: ^1.0.0 languageName: unknown linkType: soft