Skip to content

Commit f1cdb91

Browse files
committed
test: improve test error message logs
1 parent 4d2f2bd commit f1cdb91

File tree

1 file changed

+30
-19
lines changed

1 file changed

+30
-19
lines changed

tests/legacy-cli/e2e/utils/process.ts

+30-19
Original file line numberDiff line numberDiff line change
@@ -96,39 +96,47 @@ function _exec(options: ExecOptions, cmd: string, args: string[]): Promise<Proce
9696
_processes.push(childProcess);
9797

9898
// Create the error here so the stack shows who called this function.
99+
const error = new Error();
100+
try {
101+
throw error;
102+
} catch (_) {
103+
// ignore, just need to generate the stack trace
104+
}
105+
106+
// Return log info about the current process status
107+
function envDump() {
108+
return [
109+
`ENV:${JSON.stringify(spawnOptions.env, null, 2)}`,
110+
`STDOUT:\n${stdout}`,
111+
`STDERR:\n${stderr}`,
112+
].join('\n\n');
113+
}
99114

100-
return new Promise((resolve, reject) => {
115+
return new Promise<ProcessOutput>((resolve, reject) => {
101116
let matched = false;
102117

103-
childProcess.on('exit', (error: any) => {
118+
childProcess.on('exit', (err: any) => {
104119
_processes = _processes.filter((p) => p !== childProcess);
105120

106121
if (options.waitForMatch && !matched) {
107-
error = `Output didn't match '${options.waitForMatch}'.`;
122+
reject(
123+
`Process output didn't match - "${cmd} ${args.join(' ')}": '${
124+
options.waitForMatch
125+
}'.\n\n${envDump()}\n`,
126+
);
127+
return;
108128
}
109129

110-
if (!error) {
130+
if (!err) {
111131
resolve({ stdout, stderr });
112132
return;
113133
}
114134

115-
reject(
116-
new Error(
117-
`Running "${cmd} ${args.join(' ')}" returned error. ${error}...\n\nENV:${JSON.stringify(
118-
process.env,
119-
null,
120-
2,
121-
)}\n\nSTDOUT:\n${stdout}\n\nSTDERR:\n${stderr}\n`,
122-
),
123-
);
135+
reject(`Process exit error - "${cmd} ${args.join(' ')}": ${err}...\n\n${envDump()}\n`);
124136
});
137+
125138
childProcess.on('error', (err) => {
126-
err.message += `${err}...\n\nENV:${JSON.stringify(
127-
process.env,
128-
null,
129-
2,
130-
)}\n\nSTDOUT:\n${stdout}\n\nSTDERR:\n${stderr}\n`;
131-
reject(err);
139+
reject(`Process error - "${cmd} ${args.join(' ')}": ${err}...\n\n${envDump()}\n`);
132140
});
133141

134142
if (options.waitForMatch) {
@@ -154,6 +162,9 @@ function _exec(options: ExecOptions, cmd: string, args: string[]): Promise<Proce
154162
childProcess.stdin!.write(options.stdin);
155163
childProcess.stdin!.end();
156164
}
165+
}).catch((err) => {
166+
error.message = err.toString();
167+
return Promise.reject(error);
157168
});
158169
}
159170

0 commit comments

Comments
 (0)