简体   繁体   English

在 Jest 测试中重置 ajax 模拟调用计数

[英]Resetting ajax mock calls count in Jest tests

I have test structure represented below, my code uses ajax calls so I'm mocking as represented in the Jest tutorial :我有下面表示的测试结构,我的代码使用 ajax 调用,所以我正在模拟 Jest 教程中表示的:

describe('it behavior', function() {
  it('is as it is', function() {
    jQuery.ajax.mock.calls[0][0].success({
      data: []
    });
    ...
  });
  it('is is not as it was', function() {
    jQuery.ajax.mock.calls[1][0].success({
      data: [1, 2, 3]
    });
    ...
  });
});

What bothers me most is this jQuery.ajax.mock.calls[X][0] in every case, mostly because I made typos or forget to increment call number in mock.最让我烦恼的是这个jQuery.ajax.mock.calls[X][0]在每种情况下,主要是因为我打错字或忘记在模拟中增加电话号码。

Is it possible to reset mock call count in afterEach callback or some pattern for test organisation in that case?在这种情况下,是否可以在afterEach回调或测试组织的某种模式中重置模拟调用计数? Preferably without any extra test dependencies.最好没有任何额外的测试依赖项。

Try this:试试这个:

describe('something', function() {
  beforeEach(function() {
    jQuery.ajax = jest.genMockFunction()
  })

  it('does something', function() {
    // do something
  })
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM