使用 Node exec 时开玩笑打开句柄?

Ole*_*Ole 5 javascript node.js jestjs

我有一个用于调用指挥官脚本的函数:

\n\n
    function cli(args, cwd) {\n    return new Promise(resolve => {\n        exec(\n        `node ${path.resolve("./index")} ${args.join(" ")}`,\n        { cwd },\n        (error, stdout, stderr) => {\n            resolve({\n            code: error && error.code ? error.code : 0,\n            error,\n            stdout,\n            stderr\n            });\n        }\n        );\n    });\n    }\n
Run Code Online (Sandbox Code Playgroud)\n\n

在某些调用中,Jest 会记录以下内容:

\n\n
    Jest has detected the following 1 open handle potentially keeping Jest from exiting:\n\n    \xe2\x97\x8f  PROCESSWRAP\n\n        255 | function cli(args, cwd) {\n        256 |   return new Promise(resolve => {\n        > 257 |     exec(\n            |     ^\n        258 |       `node ${path.resolve("./index")} ${args.join(" ")}`,\n        259 |       { cwd },\n        260 |       (error, stdout, stderr) => {\n\n        at exec (index.spec.js:257:5)\n        at cli (index.spec.js:256:10)\n        at Object.cli (index.spec.js:89:24)\n
Run Code Online (Sandbox Code Playgroud)\n\n

使用时是否需要执行某种类型的终结exec以使句柄关闭?

\n\n

对于完整的上下文,这是包含该函数的笑话测试脚本cli

\n

小智 6

我遇到了同样的问题,看来你应该使用unref()ChildProcess 。看起来像是发送了一个终止信号,但子进程仍然与父进程通信,因此通信被保留下来,jest 正在检测到这一点。kill()

不太确定我是否正确理解了一切是如何工作的,但至少这为我解决了问题:

// FIXES PIPEWRAP open handle
child_process.stdout.destroy()
child_process.stderr.destroy()
child_process.stdin.destroy()

// FIXES PROCESSWRAP open handle
child_process.kill() // (Optional: Just if you want to kill the process)
child_process.unref() // Necessary: fixes PROCESSWRAP
Run Code Online (Sandbox Code Playgroud)