【发布时间】:2020-09-10 01:07:05
【问题描述】:
我正在编写一个 cli 工具,并尝试在 Jest 中为它编写测试。我有一些调用 git 的函数,但我需要模拟这些调用的返回,否则它们将不一致。
我用来调用 shell 的代码如下所示。
import { exec } from "child_process";
function execute(command) {
return new Promise((resolve, reject) => {
exec(command, resolve);
});
}
export const getGitDiff = function () {
return execute("git diff")
};
如何在 Jest 中为此编写测试?
我尝试的是
import { getGitDiff } from './getGitDiff';
describe('get git diff', () => {
it('should send "git diff" to stdin', () => {
const spy = jest.spyOn(process.stdin, 'write');
return getGitDiff().then(() => {
expect(spy).toHaveBeenCalled();
})
});
});
【问题讨论】:
标签: javascript node.js testing jestjs