Skip to content

Commit

Permalink
chore: use fs.rm instead of fs.unlink in test (#14296)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB authored Jul 4, 2023
1 parent 4ecf91c commit 02ce2a7
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 26 deletions.
3 changes: 2 additions & 1 deletion packages/jest-circus/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
32 changes: 7 additions & 25 deletions packages/jest-circus/src/__mocks__/testUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, '\\\\');
Expand All @@ -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"]});
Expand Down Expand Up @@ -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(
Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 02ce2a7

Please sign in to comment.