代码之家  ›  专栏  ›  技术社区  ›  Alex Efremov

如何在每次测试之前重置Jest模拟函数调用计数

  •  177
  • Alex Efremov  · 技术社区  · 8 年前

    我是个新手,我正试图用它来测试函数是否被调用。我注意到了嘲笑。电话。长度不是每次测试都重置,而是累积。如何在每次测试前将其设为0?我不希望我的下一次测试取决于前一次的结果。

    我知道这是在开玩笑——我应该用它吗?重置模拟的最佳方法是什么。电话。长非常感谢。

    代码示例:

    总和js公司:

    import local from 'api/local';
    
    export default {
      addNumbers(a, b) {
        if (a + b <= 10) {
          local.getData();
        }
        return a + b;
      },
    };
    

    总和测验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);
    });
    
    6 回复  |  直到 7 年前
        1
  •  288
  •   Alex Efremov    6 年前

    我找到了一种处理方法:在每次测试后清除mock函数:

    加到总和上。测验js公司:

    afterEach(() => {
      local.getData.mockClear();
    });
    

    如果要在每次测试后清除所有模拟函数,请使用 clearAllMocks

    afterEach(() => {
      jest.clearAllMocks();
    });
    
        2
  •  46
  •   Daniel Marín    7 年前

    正如@AlexEfremov在评论中指出的那样。您可能想使用 clearAllMocks 每次试验后:

    afterEach(() => {
        jest.clearAllMocks();
    });
    

    请记住,这将清除所有模拟函数的调用计数,但这可能是正确的方法。

        3
  •  27
  •   Andrey Vetlugin    4 年前

    您可以配置Jest,以便在每次测试后重置或清除模拟,方法是将其中一个参数放入 jest.config.js :

    module.exports = {
      resetMocks: true,
    };
    

    或

    module.exports = {
      clearMocks: true,
    };
    

    以下是文档:

    https://jestjs.io/docs/en/configuration#resetmocks-boolean

    重置模拟[布尔]

    默认值:false

    在每次测试之前自动重置模拟状态。相当于调用jest。每次测试前重置所有Mocks()。这将导致任何模拟都会删除其假实现,但不会恢复其初始实现。

    https://jestjs.io/docs/configuration#clearmocks-boolean

    clearMocks[布尔]

    默认值:false

    在每次测试之前自动清除模拟调用、实例和结果。相当于调用jest。每次测试前清除所有mocks()。这不会删除可能已提供的任何模拟实现。

        4
  •  3
  •   Mifdha Milan    4 年前

    jest.clearAllMocks(); 没有为我清除所有的嘲笑。

    afterEach(() => {
        jest.restoreAllMocks();
    }); 
    

    帮我终于澄清了间谍的玩笑

        5
  •  0
  •   nick    4 年前

    每次测试后清除单个模拟函数(这可能对点击此url的人有用)

    import { methodName } from '../Path-to-file-with-methodName';
    
    methodName.mockReturnValue(null );
    
        describe('my component', ()=> {
          afterEach(() => {
            methodName.mockClear();
          });
          
          it('should call my method on mount', () => {
            const wrapper = mount(<AComponent  {...props} />);
            expect(methodName).toHaveBeenCalledTimes(1);
          })
          it('should call my method on mount again', () => {
            const wrapper = mount(<AComponent  {...props} />);
            expect(methodName).toHaveBeenCalledTimes(1);
          })
        });
    
     
    
        6
  •  -1
  •   Ivan    5 年前

    可以将--resetMocks选项添加到命令中: npx jest --resetMocks

    在每次测试之间自动重置模拟状态。相当于 使命感 jest.resetAllMocks()