Skip to content

Commit 0ce0607

Browse files
committed
feat: Allow returns() with no argument
Some commands simply have no output, and `returns({})` looks ugly.
1 parent 4607c62 commit 0ce0607

2 files changed

Lines changed: 28 additions & 14 deletions

File tree

src/shell.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,12 @@ export interface MockShCommandController {
7373
/**
7474
* Adds a new command-specific mock result.
7575
*
76-
* @param mock The mock result.
77-
* @param [mock.exitCode] The mock process exit code.
78-
* @param [mock.stdout] The mock stdout output.
79-
* @param [mock.stderr] The mock stderr output.
76+
* @param [mock] The mock result.
77+
* @param [mock.exitCode] The mock process exit code. Default: 0
78+
* @param [mock.stdout] The mock stdout output. Default: none
79+
* @param [mock.stderr] The mock stderr output. Default: none
8080
*/
81-
returns(mock: { exitCode?: number, stdout?: string, stderr?: string }): MockShCommandController;
81+
returns(mock?: { exitCode?: number, stdout?: string, stderr?: string }): MockShCommandController;
8282

8383
/**
8484
* Ends this chain and starts a new command matcher.
@@ -92,12 +92,12 @@ export interface MockShController {
9292
/**
9393
* Adds a new default mock result.
9494
*
95-
* @param mock The mock result.
96-
* @param [mock.exitCode] The mock process exit code.
97-
* @param [mock.stdout] The mock stdout output.
98-
* @param [mock.stderr] The mock stderr output.
95+
* @param [mock] The mock result.
96+
* @param [mock.exitCode] The mock process exit code. Default: 0
97+
* @param [mock.stdout] The mock stdout output. Default: none
98+
* @param [mock.stderr] The mock stderr output. Default: none
9999
*/
100-
returns(mock: { exitCode?: number, stdout?: string, stderr?: string }): MockShController;
100+
returns(mock?: { exitCode?: number, stdout?: string, stderr?: string }): MockShController;
101101

102102
/**
103103
* Adds a command matcher.
@@ -181,12 +181,12 @@ sh.mock = () => {
181181
};
182182

183183
const mockController: MockShController = {
184-
returns(mock: { exitCode?: number, stdout?: string, stderr?: string }) {
184+
returns(mock?: { exitCode?: number, stdout?: string, stderr?: string }) {
185185
const {
186186
exitCode = 0,
187187
stdout = '',
188188
stderr = '',
189-
} = mock;
189+
} = mock ?? {};
190190
defaults.push({ exitCode, stdout, stderr });
191191
return mockController;
192192
},
@@ -199,12 +199,12 @@ sh.mock = () => {
199199
matchers.push([re, mocks]);
200200

201201
const cmdController: MockShCommandController = {
202-
returns(mock: { exitCode?: number, stdout?: string, stderr?: string }) {
202+
returns(mock?: { exitCode?: number, stdout?: string, stderr?: string }) {
203203
const {
204204
exitCode = 0,
205205
stdout = '',
206206
stderr = '',
207-
} = mock;
207+
} = mock ?? {};
208208
mocks.push({ exitCode, stdout, stderr });
209209
return cmdController;
210210
},

tests/shell.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ describe('sh mock mode', () => {
9696
'No mock found for command: blah',
9797
);
9898
});
99+
100+
it('should give no output if mock is omitted', async () => {
101+
sh.mock().returns();
102+
103+
const result = await sh('blah');
104+
expect(result).to.equal('');
105+
});
99106
});
100107

101108
describe('matching mocks', () => {
@@ -129,6 +136,13 @@ describe('sh mock mode', () => {
129136
const result = await sh('blah');
130137
expect(result).to.equal('default');
131138
});
139+
140+
it('should give no output if mock is omitted', async () => {
141+
sh.mock().command('a').returns();
142+
143+
const result = await sh('a');
144+
expect(result).to.equal('');
145+
});
132146
});
133147

134148
describe('assertDone', () => {

0 commit comments

Comments
 (0)