This is my code:
import { exec } from "child_process";
const hello = 'hello';
const goodbye = 'goodbye';
describe("nodejs exec write", () => {
it("should run child process and write back to the stream", async () => {
const execute = exec(`echo ${hello}`);
execute.stdout?.on("readable", () => {
console.log("on readable");
execute.stdin?.write(`${goodbye}n`);
// write EPIPE
// > 17 | execute.stdin?.write(`${goodbye}n`);
});
const arrData: string[] = [];
await new Promise((resolve, reject) => {
console.log("on write");
execute.stdout?.on("data", (data: any, x: any, y: any) => {
arrData.push(data);
});
execute.stdout?.on("error", (err) => {
console.log("error=", err);
reject(err);
});
execute.stdout?.on("end", () => {
resolve(true);
});
});
expect(arrData).toEqual(expect.arrayContaining([hello, goodbye]));
});
});
This returns error:
// write EPIPE
// > 11 | execute.stdin?.write(${goodbye}n
);
What is the right way to write back to the stream?