From 0ce060734979d443678df8cf99b106d1a98d7852 Mon Sep 17 00:00:00 2001 From: Seb Aebischer Date: Mon, 22 May 2023 10:28:24 +0100 Subject: [PATCH] feat: Allow `returns()` with no argument Some commands simply have no output, and `returns({})` looks ugly. --- src/shell.ts | 28 ++++++++++++++-------------- tests/shell.spec.ts | 14 ++++++++++++++ 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/src/shell.ts b/src/shell.ts index 9143c55..c263082 100644 --- a/src/shell.ts +++ b/src/shell.ts @@ -73,12 +73,12 @@ export interface MockShCommandController { /** * Adds a new command-specific mock result. * - * @param mock The mock result. - * @param [mock.exitCode] The mock process exit code. - * @param [mock.stdout] The mock stdout output. - * @param [mock.stderr] The mock stderr output. + * @param [mock] The mock result. + * @param [mock.exitCode] The mock process exit code. Default: 0 + * @param [mock.stdout] The mock stdout output. Default: none + * @param [mock.stderr] The mock stderr output. Default: none */ - returns(mock: { exitCode?: number, stdout?: string, stderr?: string }): MockShCommandController; + returns(mock?: { exitCode?: number, stdout?: string, stderr?: string }): MockShCommandController; /** * Ends this chain and starts a new command matcher. @@ -92,12 +92,12 @@ export interface MockShController { /** * Adds a new default mock result. * - * @param mock The mock result. - * @param [mock.exitCode] The mock process exit code. - * @param [mock.stdout] The mock stdout output. - * @param [mock.stderr] The mock stderr output. + * @param [mock] The mock result. + * @param [mock.exitCode] The mock process exit code. Default: 0 + * @param [mock.stdout] The mock stdout output. Default: none + * @param [mock.stderr] The mock stderr output. Default: none */ - returns(mock: { exitCode?: number, stdout?: string, stderr?: string }): MockShController; + returns(mock?: { exitCode?: number, stdout?: string, stderr?: string }): MockShController; /** * Adds a command matcher. @@ -181,12 +181,12 @@ sh.mock = () => { }; const mockController: MockShController = { - returns(mock: { exitCode?: number, stdout?: string, stderr?: string }) { + returns(mock?: { exitCode?: number, stdout?: string, stderr?: string }) { const { exitCode = 0, stdout = '', stderr = '', - } = mock; + } = mock ?? {}; defaults.push({ exitCode, stdout, stderr }); return mockController; }, @@ -199,12 +199,12 @@ sh.mock = () => { matchers.push([re, mocks]); const cmdController: MockShCommandController = { - returns(mock: { exitCode?: number, stdout?: string, stderr?: string }) { + returns(mock?: { exitCode?: number, stdout?: string, stderr?: string }) { const { exitCode = 0, stdout = '', stderr = '', - } = mock; + } = mock ?? {}; mocks.push({ exitCode, stdout, stderr }); return cmdController; }, diff --git a/tests/shell.spec.ts b/tests/shell.spec.ts index 6ff3f21..e8ae99b 100644 --- a/tests/shell.spec.ts +++ b/tests/shell.spec.ts @@ -96,6 +96,13 @@ describe('sh mock mode', () => { 'No mock found for command: blah', ); }); + + it('should give no output if mock is omitted', async () => { + sh.mock().returns(); + + const result = await sh('blah'); + expect(result).to.equal(''); + }); }); describe('matching mocks', () => { @@ -129,6 +136,13 @@ describe('sh mock mode', () => { const result = await sh('blah'); expect(result).to.equal('default'); }); + + it('should give no output if mock is omitted', async () => { + sh.mock().command('a').returns(); + + const result = await sh('a'); + expect(result).to.equal(''); + }); }); describe('assertDone', () => {