【问题标题】:how to jest mock for a custom library如何为自定义库开玩笑
【发布时间】:2020-07-17 07:39:53
【问题描述】:

abc.js

import { form } from '@myCustomLib/validator'
const _validator = new form.particulars.Validator()

function sampleFunctionIWantToTest(formInfo) {
   var error = _validator.fullValidation(formInfo)

   if(error) {return true}

   return false
}

我想为这个函数写一个测试。我想模拟结果 _validator.fullValidation(formInfo)

如何模拟?

【问题讨论】:

    标签: javascript unit-testing mocking jestjs


    【解决方案1】:

    您可以使用jest.mock(moduleName, factory, options) 模拟@myCustomLib/validator 包。

    例如

    abc.js:

    import { form } from '@myCustomLib/validator';
    const _validator = new form.particulars.Validator();
    
    function sampleFunctionIWantToTest(formInfo) {
      var error = _validator.fullValidation(formInfo);
    
      if (error) {
        return true;
      }
    
      return false;
    }
    
    export { sampleFunctionIWantToTest };
    

    abc.test.js:

    import { form } from '@myCustomLib/validator';
    
    const validatorMock = {
      fullValidation: jest.fn(),
    };
    
    jest.mock(
      '@myCustomLib/validator',
      () => {
        const formMock = {
          particulars: {
            Validator: jest.fn(() => validatorMock),
          },
        };
        return { form: formMock };
      },
      { virtual: true },
    );
    
    describe('62949328', () => {
      afterAll(() => {
        jest.resetAllMocks();
      });
      it('should return true', () => {
        const mError = new Error('error message');
        validatorMock.fullValidation.mockReturnValueOnce(mError);
        const { sampleFunctionIWantToTest } = require('./abc');
        const actual = sampleFunctionIWantToTest();
        expect(actual).toBeTruthy();
        expect(form.particulars.Validator).toBeCalledTimes(1);
      });
    
      it('should return false', () => {
        validatorMock.fullValidation.mockReturnValueOnce(null);
        const { sampleFunctionIWantToTest } = require('./abc');
        const actual = sampleFunctionIWantToTest();
        expect(actual).toBeFalsy();
        expect(form.particulars.Validator).toBeCalledTimes(1);
      });
    });
    

    100% 覆盖率的单元测试结果:

     PASS  stackoverflow/62949328/abc.test.js (13.298s)
      62949328
        ✓ should return true (6ms)
        ✓ should return false (1ms)
    
    ----------|---------|----------|---------|---------|-------------------
    File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
    ----------|---------|----------|---------|---------|-------------------
    All files |     100 |      100 |     100 |     100 |                   
     abc.js   |     100 |      100 |     100 |     100 |                   
    ----------|---------|----------|---------|---------|-------------------
    Test Suites: 1 passed, 1 total
    Tests:       2 passed, 2 total
    Snapshots:   0 total
    Time:        15.161s
    

    jestjs 版本:"jest": "^25.5.4",

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-07-12
      • 2021-09-17
      • 2017-11-01
      • 2018-06-02
      • 2021-09-06
      • 1970-01-01
      • 2020-07-07
      相关资源
      最近更新 更多