0
Jestが初めてです。関数が呼び出されたかどうかをテストするために使用しようとしています。私はmock.calls.lengthがすべてのテストでリセットされずに蓄積されていることに気付きました。すべてのテストの前にどうすればいいですか?私は次のテストが前のテストの結果に依存しないようにしたい。Jest模擬関数をリセットする方法すべてのテストの前に呼び出し回数をカウントします。
私は前に害虫がいるのは知っています - 私はそれを使うべきですか? mock.calls.lengthをリセットする最良の方法は何ですか?ありがとうございました。
コード例:
Sum.js:各試験後のモック関数をクリアする:私はそれを処理することが見出さ
import local from 'api/local';
export default {
addNumbers(a, b) {
if (a + b <= 10) {
local.getData();
}
return a + b;
},
};
Sum.test.js
import sum from 'api/sum';
import local from 'api/local';
jest.mock('api/local');
// For current implementation, there is a difference
// if I put test 1 before test 2. I want it to be no difference
// test 1
test('should not to call local if sum is more than 10',() => {
expect(sum.addNumbers(5, 10)).toBe(15);
expect(local.getData.mock.calls.length).toBe(0);
});
// test 2
test('should call local if sum <= 10',() => {
expect(sum.addNumbers(1, 4)).toBe(5);
expect(local.getData.mock.calls.length).toBe(1);
});