Skip to content

Commit 3f61979

Browse files
committed
Add tests
1 parent cad40b4 commit 3f61979

2 files changed

Lines changed: 46 additions & 22 deletions

File tree

src/spawn.spec.ts

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,52 @@
1-
import { getSpawnOpts } from './spawn';
1+
import { getSpawnOpts, spawn } from './spawn';
22

33
const baseProcess = {
44
platform: 'win32' as const,
55
cwd: () => '',
66
env: {},
77
};
88

9-
it('sets detached mode to false for Windows platform', () => {
10-
expect(getSpawnOpts({ process: baseProcess }).detached).toBe(false);
11-
});
9+
describe('spawn()', () => {
10+
it('spawns the given command', async () => {
11+
const fakeSpawn = jest.fn();
12+
spawn('echo banana', {}, fakeSpawn, baseProcess);
13+
expect(fakeSpawn).toHaveBeenCalled();
14+
expect(fakeSpawn.mock.calls[0][1].join(' ')).toContain('echo banana');
15+
});
1216

13-
it('sets stdio to inherit when raw', () => {
14-
expect(getSpawnOpts({ raw: true }).stdio).toBe('inherit');
17+
it('returns spawned process', async () => {
18+
const childProcess = {};
19+
const fakeSpawn = jest.fn().mockReturnValue(childProcess);
20+
const child = spawn('echo banana', {}, fakeSpawn, baseProcess);
21+
expect(child).toBe(childProcess);
22+
});
1523
});
1624

17-
it('merges FORCE_COLOR into env vars if color supported', () => {
18-
const process = { ...baseProcess, env: { foo: 'bar' } };
19-
expect(getSpawnOpts({ process, colorSupport: false }).env).toEqual(process.env);
20-
expect(getSpawnOpts({ process, colorSupport: { level: 1 } }).env).toEqual({
21-
FORCE_COLOR: '1',
22-
foo: 'bar',
25+
describe('getSpawnOpts()', () => {
26+
it('sets detached mode to false for Windows platform', () => {
27+
expect(getSpawnOpts({ process: baseProcess }).detached).toBe(false);
2328
});
24-
});
2529

26-
it('sets default cwd to process.cwd()', () => {
27-
const process = { ...baseProcess, cwd: () => 'process-cwd' };
28-
expect(getSpawnOpts({ process }).cwd).toBe('process-cwd');
29-
});
30+
it('sets stdio to inherit when raw', () => {
31+
expect(getSpawnOpts({ raw: true }).stdio).toBe('inherit');
32+
});
3033

31-
it('overrides default cwd', () => {
32-
const cwd = 'foobar';
33-
expect(getSpawnOpts({ cwd }).cwd).toBe(cwd);
34+
it('merges FORCE_COLOR into env vars if color supported', () => {
35+
const process = { ...baseProcess, env: { foo: 'bar' } };
36+
expect(getSpawnOpts({ process, colorSupport: false }).env).toEqual(process.env);
37+
expect(getSpawnOpts({ process, colorSupport: { level: 1 } }).env).toEqual({
38+
FORCE_COLOR: '1',
39+
foo: 'bar',
40+
});
41+
});
42+
43+
it('sets default cwd to process.cwd()', () => {
44+
const process = { ...baseProcess, cwd: () => 'process-cwd' };
45+
expect(getSpawnOpts({ process }).cwd).toBe('process-cwd');
46+
});
47+
48+
it('overrides default cwd', () => {
49+
const cwd = 'foobar';
50+
expect(getSpawnOpts({ cwd }).cwd).toBe(cwd);
51+
});
3452
});

src/spawn.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,21 @@ import supportsColor from 'supports-color';
55
* Spawns a command using `cmd.exe` on Windows, or `/bin/sh` elsewhere.
66
*/
77
// Implementation based off of https://github.com/mmalecki/spawn-command/blob/v0.0.2-1/lib/spawn-command.js
8-
export function spawn(command: string, options: SpawnOptions): ChildProcess {
8+
export function spawn(
9+
command: string,
10+
options: SpawnOptions,
11+
// For testing
12+
spawn: (command: string, args: string[], options: SpawnOptions) => ChildProcess = baseSpawn,
13+
process: Pick<NodeJS.Process, 'platform'> = global.process,
14+
): ChildProcess {
915
let file = '/bin/sh';
1016
let args = ['-c', command];
1117
if (process.platform === 'win32') {
1218
file = 'cmd.exe';
1319
args = ['/s', '/c', `"${command}"`];
1420
options.windowsVerbatimArguments = true;
1521
}
16-
return baseSpawn(file, args, options);
22+
return spawn(file, args, options);
1723
}
1824

1925
export const getSpawnOpts = ({

0 commit comments

Comments
 (0)