【问题标题】:JEST Mock implementation from a builder来自构建器的 JEST Mock 实现
【发布时间】:2021-03-17 12:13:30
【问题描述】:

我正在尝试测试某些东西,但我无法找到解决方案

const Config = require('client-config')
const cfgM = Config.CFGBuilder.builder().build();

class ConfigService {
  static get(key) {
    return cfgM.getProperty(key);
  }
}

module.exports = ConfigService;

这就是我想要测试的。我的 configManager 中的 get

const Config = require('my-config');

const { expect } = chai;
const ConfigService = require('../ConfigService');


jest.mock('my-config', () => ({
  CFGBuilder:
  {
    builder: () => ({
      build: () => ({
        getProperty: jest.fn(),
      }),
    }),
  },
}));

describe('testing config service', () => {
  describe('testing get', () => {
    it('should return db.name', () => {
      Config.CFGBuilder.builder().build().getProperty.mockImplementation(key => 'mykey');
      expect(ConfigService.get('key')).to.be('mykey');
    });
  });
});

事情是我从ConfigServivce.get('key')得到未定义

免责声明:我没有编写客户端配置模块。这只是我必须使用的一个模块

【问题讨论】:

    标签: node.js unit-testing jestjs mocking


    【解决方案1】:

    你没有模拟cfgM.getProperty () 的返回值或实现,这就是你得到undefined 的原因。对于方法的链式调用,可以使用.mockReturnThis(),而不是返回嵌套对象。

    这里是单元测试解决方案:

    index.js:

    const Config = require('client-config');
    const cfgM = Config.CFGBuilder.builder().build();
    
    class ConfigService {
      static get(key) {
        return cfgM.getProperty(key);
      }
    }
    
    module.exports = ConfigService;
    

    index.test.js:

    const Config = require('client-config');
    const ConfigService = require('./');
    
    jest.mock(
      'client-config',
      () => ({
        CFGBuilder: {
          builder: jest.fn().mockReturnThis(),
          build: jest.fn().mockReturnThis(),
          getProperty: jest.fn(),
        },
      }),
      { virtual: true },
    );
    
    describe('testing config service', () => {
      describe('testing get', () => {
        it('should return db.name', () => {
          Config.CFGBuilder.builder()
            .build()
            .getProperty.mockImplementation((key) => 'mykey');
          expect(ConfigService.get('key')).toBe('mykey');
        });
      });
    });
    

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

     PASS  stackoverflow/63198582/index.test.js (13.614s)
      testing config service
        testing get
          ✓ should return db.name (3ms)
    
    ----------|---------|----------|---------|---------|-------------------
    File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
    ----------|---------|----------|---------|---------|-------------------
    All files |     100 |      100 |     100 |     100 |                   
     index.js |     100 |      100 |     100 |     100 |                   
    ----------|---------|----------|---------|---------|-------------------
    Test Suites: 1 passed, 1 total
    Tests:       1 passed, 1 total
    Snapshots:   0 total
    Time:        15.747s, estimated 18s
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-05
      • 2020-03-12
      • 2020-02-25
      • 1970-01-01
      • 2021-10-11
      • 2019-06-21
      • 2020-04-02
      • 2022-12-02
      相关资源
      最近更新 更多