Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/453.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
Javascript 使用jest(或任何其他技术)模拟jquery$及其函数_Javascript_Jquery_Testing_Mocking_Spy - Fatal编程技术网

Javascript 使用jest(或任何其他技术)模拟jquery$及其函数

Javascript 使用jest(或任何其他技术)模拟jquery$及其函数,javascript,jquery,testing,mocking,spy,Javascript,Jquery,Testing,Mocking,Spy,我有一些使用jquery的javascript: var $newItemButton = $('#newItemButton'); var $newItemForm = $('#newItemForm'); var $textInput = $('input:text'); $newItemButton.show(); $newItemForm.hide(); $('#showForm').on('click', function(){ console.

我有一些使用jquery的javascript:

  var $newItemButton = $('#newItemButton');
  var $newItemForm = $('#newItemForm');
  var $textInput = $('input:text');

  $newItemButton.show();
  $newItemForm.hide();

    $('#showForm').on('click', function(){
    console.log("SHOW FORM CLICKED");
    $newItemButton.hide();
    $newItemForm.show();
  });

  $newItemForm.on('submit', function(e){
    e.preventDefault();
    var newText = $textInput.val();
    $('li:last').after('<li>' + newText + '</li>');
    $newItemForm.hide();
    $newItemButton.show();
    $textInput.val('');
  });
但不是所有对象上的
hide()
,只在
$newItemButton
上。 所以,总的来说,我的问题是如何模拟/监视jquery
$
及其各种函数,以便在单元测试中控制它们

    var spy = jest.spyOn($.fn, 'hide').mockImplementation(() =>{});
    $('#showForm').click();
    expect(spy).toHaveBeenCalled();