Skip to content

Commit c65adca

Browse files
committed
fix: Add missing properties to mock sh errors
1 parent b9ffcef commit c65adca

2 files changed

Lines changed: 32 additions & 4 deletions

File tree

src/shell.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,17 @@ sh.mock = () => {
175175
throw new Error(`No mock found for command: ${command}`);
176176
}
177177
if (mock.exitCode !== 0) {
178-
throw new Error(`Command failed: ${command}\n${mock.stderr}`);
178+
const error = new Error(`Command failed: ${command}\n${mock.stderr}`);
179+
Object.assign(error, {
180+
cmd: command,
181+
code: mock.exitCode,
182+
stdout: mock.stdout,
183+
stderr: mock.stderr,
184+
// not mocked but included for completeness
185+
killed: false,
186+
signal: null,
187+
});
188+
throw error;
179189
}
180190
return { stdout: mock.stdout, stderr: mock.stderr };
181191
};

tests/shell.spec.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,15 @@ describe('sh', () => {
2121
});
2222

2323
it('should throw if the command fails', async () => {
24-
await expect(sh('false')).to.eventually.be.rejected;
24+
const promise = sh('echo "output" && echo "errors" >&2 && false');
25+
await expect(promise).to.eventually.be.rejectedWith(
26+
'Command failed: echo "output" && echo "errors" >&2 && false\nerrors',
27+
).and.to.include({
28+
cmd: 'echo "output" && echo "errors" >&2 && false',
29+
code: 1,
30+
stdout: 'output\n',
31+
stderr: 'errors\n',
32+
});
2533
});
2634

2735
describe('options.trim', () => {
@@ -114,7 +122,12 @@ describe('sh mock mode', () => {
114122

115123
await expect(sh('fail')).to.eventually.be.rejectedWith(
116124
'Command failed: fail\nsomething went wrong',
117-
);
125+
).and.to.include({
126+
cmd: 'fail',
127+
code: 1,
128+
stdout: 'here is some output',
129+
stderr: 'something went wrong',
130+
});
118131
});
119132
});
120133

@@ -168,7 +181,12 @@ describe('sh mock mode', () => {
168181

169182
await expect(sh('fail')).to.eventually.be.rejectedWith(
170183
'Command failed: fail\nsomething went wrong',
171-
);
184+
).and.to.include({
185+
cmd: 'fail',
186+
code: 1,
187+
stdout: 'here is some output',
188+
stderr: 'something went wrong',
189+
});
172190
});
173191
});
174192

0 commit comments

Comments
 (0)