Skip to content

Commit 02ce2a7

Browse files
authored
chore: use fs.rm instead of fs.unlink in test (#14296)
1 parent 4ecf91c commit 02ce2a7

File tree

3 files changed

+10
-26
lines changed

3 files changed

+10
-26
lines changed

packages/jest-circus/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747
"@types/graceful-fs": "^4.1.3",
4848
"@types/stack-utils": "^2.0.0",
4949
"execa": "^5.0.0",
50-
"graceful-fs": "^4.2.9"
50+
"graceful-fs": "^4.2.9",
51+
"tempy": "^1.0.0"
5152
},
5253
"engines": {
5354
"node": "^14.15.0 || ^16.10.0 || >=18.0.0"

packages/jest-circus/src/__mocks__/testUtils.ts

+7-25
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import {createHash} from 'crypto';
9-
import {tmpdir} from 'os';
10-
import * as path from 'path';
11-
import {ExecaSyncReturnValue, sync as spawnSync} from 'execa';
8+
import {sync as spawnSync} from 'execa';
129
import * as fs from 'graceful-fs';
10+
import tempy = require('tempy');
1311

1412
const CIRCUS_PATH = require.resolve('../').replace(/\\/g, '\\\\');
1513
const CIRCUS_RUN_PATH = require.resolve('../run').replace(/\\/g, '\\\\');
@@ -21,20 +19,11 @@ const BABEL_REGISTER_PATH = require
2119
.resolve('@babel/register')
2220
.replace(/\\/g, '\\\\');
2321

24-
interface Result extends ExecaSyncReturnValue {
25-
status: number;
26-
error: string;
27-
}
28-
2922
export const runTest = (
3023
source: string,
3124
opts?: {seed?: number; randomize?: boolean},
3225
) => {
33-
const filename = createHash('sha1')
34-
.update(source)
35-
.digest('hex')
36-
.substring(0, 32);
37-
const tmpFilename = path.join(tmpdir(), filename);
26+
const tmpFilename = tempy.file();
3827

3928
const content = `
4029
require('${BABEL_REGISTER_PATH}')({extensions: [".js", ".ts"]});
@@ -62,25 +51,18 @@ export const runTest = (
6251
fs.writeFileSync(tmpFilename, content);
6352
const result = spawnSync('node', [tmpFilename], {
6453
cwd: process.cwd(),
65-
}) as Result;
66-
67-
// For compat with cross-spawn
68-
result.status = result.exitCode;
54+
});
6955

70-
if (result.status !== 0) {
56+
if (result.exitCode !== 0) {
7157
const message = `
7258
STDOUT: ${result.stdout && result.stdout.toString()}
7359
STDERR: ${result.stderr && result.stderr.toString()}
74-
STATUS: ${result.status}
75-
ERROR: ${String(result.error)}
60+
STATUS: ${result.exitCode}
7661
`;
7762
throw new Error(message);
7863
}
7964

80-
result.stdout = String(result.stdout);
81-
result.stderr = String(result.stderr);
82-
83-
fs.unlinkSync(tmpFilename);
65+
fs.rmSync(tmpFilename, {force: true});
8466

8567
if (result.stderr) {
8668
throw new Error(

yarn.lock

+1
Original file line numberDiff line numberDiff line change
@@ -12486,6 +12486,7 @@ __metadata:
1248612486
pure-rand: ^6.0.0
1248712487
slash: ^3.0.0
1248812488
stack-utils: ^2.0.3
12489+
tempy: ^1.0.0
1248912490
languageName: unknown
1249012491
linkType: soft
1249112492

0 commit comments

Comments
 (0)