[英]How to mock childprocess.exec in TS
export function executeCommandPromise(file: string, command: string) {
return new Promise((resolve, reject) => {
exec(command, { cwd: `${file}` }, (error: ExecException | null, stdout: string, stderr: string) => {
if (error) {
console.warn(error);
reject(error);
}
resolve(capRunnable);
});
});
}
我想创建一个测试,它将 exec 命令存根 exec 被调用到回调函数的问题,我想控制标准输出
我试图做类似的事情
let sandbox = sinon.sandbox.create();
let event= new events.EventEmitter();
sandbox.stub(child_process, 'exec').returns(event);
但它没有用,我不知道如何将结果返回到标准输出
这是单元测试解决方案:
index.ts
:
import cp, { ExecException } from 'child_process';
const capRunnable = 'capRunnable';
export function executeCommandPromise(file: string, command: string) {
return new Promise((resolve, reject) => {
cp.exec(command, { cwd: `${file}` }, (error: ExecException | null, stdout: string, stderr: string) => {
if (error) {
console.warn(error);
reject(error);
}
resolve(capRunnable);
});
});
}
index.spec.ts
:
import { executeCommandPromise } from './';
import cp, { ChildProcess, ExecException } from 'child_process';
describe('executeCommandPromise', () => {
afterEach(() => {
jest.restoreAllMocks();
});
test('should exec command correctly', async () => {
let execCallback: (error: ExecException | null, stdout: string, stderr: string) => void;
jest
.spyOn(cp, 'exec')
.mockImplementation(function(
this: ChildProcess,
command: string,
options: any,
callback?: (error: ExecException | null, stdout: string, stderr: string) => void
): ChildProcess {
if (callback) {
execCallback = callback;
}
return this;
});
const actualValue = executeCommandPromise('somefile', 'ls -a');
execCallback!(null, '', '');
await expect(actualValue).resolves.toBe('capRunnable');
expect(cp.exec).toBeCalledWith('ls -a', { cwd: 'somefile' }, execCallback!);
});
test('should throw error when exec command failed', async () => {
let execCallback: (error: ExecException | null, stdout: string, stderr: string) => void;
jest
.spyOn(cp, 'exec')
.mockImplementation(function(
this: ChildProcess,
command: string,
options: any,
callback?: (error: ExecException | null, stdout: string, stderr: string) => void
): ChildProcess {
if (callback) {
execCallback = callback;
}
return this;
});
const actualValue = executeCommandPromise('somefile', 'ls -a');
execCallback!(new Error('some error happened'), '', '');
await expect(actualValue).rejects.toThrowError('some error happened');
expect(cp.exec).toBeCalledWith('ls -a', { cwd: 'somefile' }, execCallback!);
});
});
覆盖率 100% 的单元测试结果:
PASS src/stackoverflow/57589104/index.spec.ts (8.518s)
executeCommandPromise
✓ should exec command correctly (15ms)
✓ should throw error when exec command failed (10ms)
console.warn src/stackoverflow/57589104/index.ts:469
Error: some error happened
at /Users/ldu020/workspace/github.com/mrdulin/jest-codelab/src/stackoverflow/57589104/index.spec.ts:45:19
at step (/Users/ldu020/workspace/github.com/mrdulin/jest-codelab/src/stackoverflow/57589104/index.spec.ts:33:23)
at Object.next (/Users/ldu020/workspace/github.com/mrdulin/jest-codelab/src/stackoverflow/57589104/index.spec.ts:14:53)
at /Users/ldu020/workspace/github.com/mrdulin/jest-codelab/src/stackoverflow/57589104/index.spec.ts:8:71
at new Promise (<anonymous>)
at Object.<anonymous>.__awaiter (/Users/ldu020/workspace/github.com/mrdulin/jest-codelab/src/stackoverflow/57589104/index.spec.ts:4:12)
at Object.<anonymous> (/Users/ldu020/workspace/github.com/mrdulin/jest-codelab/src/stackoverflow/57589104/index.spec.ts:29:55)
at Object.asyncJestTest (/Users/ldu020/workspace/github.com/mrdulin/jest-codelab/node_modules/jest-jasmine2/build/jasmineAsyncInstall.js:102:37)
at resolve (/Users/ldu020/workspace/github.com/mrdulin/jest-codelab/node_modules/jest-jasmine2/build/queueRunner.js:43:12)
at new Promise (<anonymous>)
at mapper (/Users/ldu020/workspace/github.com/mrdulin/jest-codelab/node_modules/jest-jasmine2/build/queueRunner.js:26:19)
at promise.then (/Users/ldu020/workspace/github.com/mrdulin/jest-codelab/node_modules/jest-jasmine2/build/queueRunner.js:73:41)
at process._tickCallback (internal/process/next_tick.js:68:7)
----------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files | 100 | 100 | 100 | 100 | |
index.ts | 100 | 100 | 100 | 100 | |
----------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: 10.086s
源代码: https ://github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/57589104
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.