Painless Javascript Testing

The Pain

  • Ruby 有几种
  • Java 有几种测试框架
  • JavaScript 有几种...

为什么这么多测试框架

跟后端代码不太一样的东西无非就是

  • DOM
  • AJAX

    因此男厕的也就是他们

DOM - 举个🌰

it('should hide specific column by display filter', function () {
    $option1 = $('.ui-multiselect-checkboxes input[type=checkbox]:eq(1)');
    $option1.attr("checked", false).trigger("click");
    expect($($table.find('td:eq(2)'))).toBeHidden();
  });
  • promise: 使用 pit() 来测试 thenable 的对象, 比如 repo 的例子,就 keyi 写成
    pit('should populate properties with data from github api', function(){
    return repo.fetch().then(
      expect(repo.followers).toBe(23);
    );
    });
  • Timer mocks: 可以使用 mock 的 timer 和 ticks, 也就是你可以加速 所有的setTimeout, setInterval, clearTimeout, clearInterval行为. 不需要等待.
    setTimeout(function() { callback(); }, 1000);
    expect(callback).not.toBeCalled();
    jest.runAllTimers();
    expect(callback).toBeCalled()