Skip to content

Commit

Permalink
feat: Allow returns() with no argument
Browse files Browse the repository at this point in the history
Some commands simply have no output, and `returns({})` looks ugly.
  • Loading branch information
seb-cr committed May 22, 2023
1 parent 4607c62 commit 0ce0607
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
28 changes: 14 additions & 14 deletions src/shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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;
},
Expand All @@ -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;
},
Expand Down
14 changes: 14 additions & 0 deletions tests/shell.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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', () => {
Expand Down

0 comments on commit 0ce0607

Please sign in to comment.