Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jestjs Jest是否有基于输入有条件地返回值的方法?_Jestjs - Fatal编程技术网

Jestjs Jest是否有基于输入有条件地返回值的方法?

Jestjs Jest是否有基于输入有条件地返回值的方法?,jestjs,Jestjs,我习惯于使用Sinon.js大量地使用with args有条件地返回假值,从而对Sinon.js进行模拟(实际上是伪造的,但不管怎样)。现在我正在尝试迁移到Jest,我很抱歉在模拟API中找不到任何类似的东西 我想要的是: const t = sinon.stub(); t.withArgs('foo').returns('bar'); console.log(t('foo')); // 'bar' console.log(t('qux')); // undefined 看来开玩笑的唯一方

我习惯于使用Sinon.js大量地使用
with args
有条件地返回假值,从而对Sinon.js进行模拟(实际上是伪造的,但不管怎样)。现在我正在尝试迁移到Jest,我很抱歉在模拟API中找不到任何类似的东西

我想要的是:

const t = sinon.stub();
t.withArgs('foo').returns('bar'); 

console.log(t('foo')); // 'bar'
console.log(t('qux')); // undefined
看来开玩笑的唯一方法是使用
mockImplementation
,这意味着我必须手工编写arg匹配器


有没有更好的方法来开玩笑呢?

到目前为止,还没有任何Jest内置方法

已打开一个问题和一个功能建议

基于,有一个第三方库执行类似的操作: