Skip to content

Commit

Permalink
fix: Add missing properties to mock sh errors
Browse files Browse the repository at this point in the history
  • Loading branch information
seb-cr committed May 22, 2023
1 parent b9ffcef commit c65adca
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
12 changes: 11 additions & 1 deletion src/shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,17 @@ sh.mock = () => {
throw new Error(`No mock found for command: ${command}`);
}
if (mock.exitCode !== 0) {
throw new Error(`Command failed: ${command}\n${mock.stderr}`);
const error = new Error(`Command failed: ${command}\n${mock.stderr}`);
Object.assign(error, {
cmd: command,
code: mock.exitCode,
stdout: mock.stdout,
stderr: mock.stderr,
// not mocked but included for completeness
killed: false,
signal: null,
});
throw error;
}
return { stdout: mock.stdout, stderr: mock.stderr };
};
Expand Down
24 changes: 21 additions & 3 deletions tests/shell.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,15 @@ describe('sh', () => {
});

it('should throw if the command fails', async () => {
await expect(sh('false')).to.eventually.be.rejected;
const promise = sh('echo "output" && echo "errors" >&2 && false');
await expect(promise).to.eventually.be.rejectedWith(
'Command failed: echo "output" && echo "errors" >&2 && false\nerrors',
).and.to.include({
cmd: 'echo "output" && echo "errors" >&2 && false',
code: 1,
stdout: 'output\n',
stderr: 'errors\n',
});
});

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

await expect(sh('fail')).to.eventually.be.rejectedWith(
'Command failed: fail\nsomething went wrong',
);
).and.to.include({
cmd: 'fail',
code: 1,
stdout: 'here is some output',
stderr: 'something went wrong',
});
});
});

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

await expect(sh('fail')).to.eventually.be.rejectedWith(
'Command failed: fail\nsomething went wrong',
);
).and.to.include({
cmd: 'fail',
code: 1,
stdout: 'here is some output',
stderr: 'something went wrong',
});
});
});

Expand Down

0 comments on commit c65adca

Please sign in to comment.