node.js - 开玩笑:child_process.exec.mockImplentation 不是函数
问题描述
我有一个使用该功能的child_process.exec
功能:
//serverUtils.js:
const { promisify } = require('util');
const exec = promisify(require('child_process').exec);
async getUpstreamRepo() {
try {
const forkConfig = (await exec('git remote get-url upstream')).stdout;
let upstreamRepo = forkConfig.replace('git@github.com:', '');
upstreamRepo = upstreamRepo.replace(/\r?\n|\r/g, '');
return upstreamRepo;
} catch (error) {
console.error(error);
throw error;
}
},
在查看了这个 SO post 之后,我尝试像这样模拟 exec 调用:
//serverUtils.test.js:
const child_process = require('child_process');
jest.mock('child_process')
describe('Test Class', () => {
....
it('check upstream repo', async () => {
child_process.exec.mockImplentation(jest.fn().
mockReturnValueOnce('git@github.com:mock/url.git'))
await expect(serverScript.getUpstreamRepo()).
resolves.toEqual('mock/url.git');
});
}
但是,我得到child_process.exec.mockImplentation is not a function
了正如链接的帖子所解释的那样,“ Jest 文档说,在模拟 Node 的核心模块时,需要调用 jest.mock('child_process') 。” ——我显然是这样做的。
解决方案
您看到的错误是因为您正在调用mockImplentation
而不是mockImplementation
. 不幸的是,当您更正该错字时,测试仍然无法通过。
promisify
这是因为您正在调用exec
方法,允许将其用作承诺。幕后promisify
工作是从基于异步回调的函数(其中回调放置在最后一个参数并以错误作为第一个参数和数据作为第二个参数调用)转换为基于承诺的函数。
因此,为了使该promisify
方法能够正常工作,您必须模拟该exec
方法,以便它调用回调参数以便解决 Promise。
另外,请注意,您正在stdout
从调用结果中读取参数exec
,因此在返回的数据中,您必须发送具有该属性的对象。
考虑到所有这些:
it('check upstream repo', async () => {
child_process.exec.mockImplementation((command, callback) => {
callback(null, { stdout: 'git@github.com:mock/url.git' });
});
await expect(serverScript.getUpstreamRepo()).
resolves.toEqual('mock/url.git');
});
另一种可能的解决方案是直接模拟该promisify
方法:
jest.mock('util', () => ({
promisify: jest.fn(() => {
return jest.fn().mockResolvedValue({ stdout: 'git@github.com:mock/url.git' });
})
}));
describe('Test Class', () => {
it('check upstream repo', async () => {
await expect(serverScript.getUpstreamRepo()).
resolves.toEqual('mock/url.git');
});
});
推荐阅读
- node.js - 在 Express 中使用 sentry 和自定义错误处理程序
- python - 如何在 Python 中制作热方程的 3D 模型?
- batch-file - 如何打开和关闭回声
- database - Laravel 多用户 id 表
- python - Pandas 数据框不允许我在 python 中删除第一个空列?
- javascript - 可以在自己的容器中使用 sqllite 吗?
- html - 当我设置身体的高度/宽度时出现问题
- c - 如何在没有malloc的情况下从mmap更改地址的值?
- python - 尝试设置两个 python 烧瓶服务器,80 以外的端口将不起作用
- java - 使用 JSONAssert 查找 json diff 失败