如何在setupTests中覆盖Jest模拟

时间:2018-06-21 07:28:01

标签: javascript unit-testing jestjs

我在jest.mock('./Component')中添加了setupTests.js,因为我不想在每个有Component的测试中都这样做。

但是现在,当我想测试Component时,它就被嘲笑了。

如何拥有原始Component来覆盖或重置它?

到目前为止,我已经尝试过:

  • jest.unmock(moduleName)
  • jest.dontMock(moduleName)
  • jest.clearAllMocks()
  • jest.resetAllMocks()
  • jest.restoreAllMocks()
  • jest.resetModules()

和:

jest.mock('./index', () => ({
  Component: 'Component'
}));

1 个答案:

答案 0 :(得分:1)

jest.unmock(moduleName)应该起作用,但仅当在测试的$('form').on('click', '.required_group', function() { var flag=false; $('.required_group').each(function(e){ if (this.checked) { falg=true; } }); if(!flag){ // Do your code for Error Message } }); 块之前使用了它。

Jest会以自己的方式来转换您的测试,以使所有的模拟/取消模拟魔术发生。

相关问题