Skip to content

Commit 4607c62

Browse files
committed
feat: Chainable sh mock commands
It's now possible to include several commands in one chain: ```ts sh.mock() .command('a').returns({ stdout: 'output from a' }) .command('b').returns({ stdout: 'output from b' }) // ... ```
1 parent 9313f9c commit 4607c62

3 files changed

Lines changed: 32 additions & 16 deletions

File tree

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,15 @@ When you're done, use `sh.restore()` to stop mocking.
115115
// activate mock mode
116116
const mock = sh.mock();
117117

118-
// add some default mocks
119-
mock.returns({ stdout: 'first call' });
120-
mock.returns({ stdout: 'second call' });
118+
// add some default mocks (you can chain these methods)
119+
mock
120+
.returns({ stdout: 'first call' })
121+
.returns({ stdout: 'second call' });
121122

122123
// add some command-specific mocks
123-
mock.command('cat file.txt').returns({ stdout: 'file contents' });
124-
mock.command(/echo/).returns({ stdout: 'mock echo' });
124+
mock
125+
.command('cat file.txt').returns({ stdout: 'file contents' })
126+
.command(/echo/).returns({ stdout: 'mock echo' });
125127

126128
await sh('some arbitrary command');
127129
// => 'first call'

src/shell.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ export interface MockShCommandController {
7979
* @param [mock.stderr] The mock stderr output.
8080
*/
8181
returns(mock: { exitCode?: number, stdout?: string, stderr?: string }): MockShCommandController;
82+
83+
/**
84+
* Ends this chain and starts a new command matcher.
85+
*
86+
* @param command The command to match.
87+
*/
88+
command(command: string | RegExp): MockShCommandController;
8289
}
8390

8491
export interface MockShController {
@@ -201,7 +208,12 @@ sh.mock = () => {
201208
mocks.push({ exitCode, stdout, stderr });
202209
return cmdController;
203210
},
211+
212+
command(newCommand: string | RegExp) {
213+
return mockController.command(newCommand);
214+
},
204215
};
216+
205217
return cmdController;
206218
},
207219

tests/shell.spec.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,22 @@ describe('sh mock mode', () => {
4444
it('should work as demonstrated', async () => {
4545
const mock = sh.mock();
4646

47-
mock.returns({ stdout: 'First call' });
48-
mock.returns({ stdout: 'Second call' });
47+
mock
48+
.returns({ stdout: 'first call' })
49+
.returns({ stdout: 'second call' });
4950

50-
mock.command('cat file.txt').returns({ stdout: 'file contents' });
51-
mock.command(/echo/).returns({ stdout: 'mock echo!' });
51+
mock
52+
.command('cat file.txt').returns({ stdout: 'file contents' })
53+
.command(/echo/).returns({ stdout: 'mock echo' });
5254

5355
let result = await sh('some arbitrary command');
54-
expect(result).to.equal('First call');
56+
expect(result).to.equal('first call');
5557

5658
result = await sh('some arbitrary command');
57-
expect(result).to.equal('Second call');
59+
expect(result).to.equal('second call');
5860

5961
result = await sh('echo "hi there"');
60-
expect(result).to.equal('mock echo!');
62+
expect(result).to.equal('mock echo');
6163

6264
result = await sh('cat file.txt');
6365
expect(result).to.equal('file contents');
@@ -98,11 +100,11 @@ describe('sh mock mode', () => {
98100

99101
describe('matching mocks', () => {
100102
it('should return the mock results for the matching command', async () => {
101-
const mock = sh.mock();
102-
mock.command('a')
103+
sh.mock()
104+
.command('a')
103105
.returns({ stdout: 'first a' })
104-
.returns({ stdout: 'second a' });
105-
mock.command('b')
106+
.returns({ stdout: 'second a' })
107+
.command('b')
106108
.returns({ stdout: 'first b' })
107109
.returns({ stdout: 'second b' });
108110

0 commit comments

Comments
 (0)