【问题标题】:Why branch is not covered with rest parameters in Jest?为什么 Jest 中的分支没有被其余参数覆盖?
【发布时间】:2019-09-15 17:43:15
【问题描述】:

我目前正在尝试覆盖 100% 的功能,但我面临一个未覆盖的分支,它不明白为什么它没有被覆盖,甚至不明白如何修复和覆盖它。

test coverage result here

它尝试了许多不同的测试,但并没有改变任何东西。如果问题来自我或 Jest,我不知道?

我的功能

export const removeProductInList = (state: IItems, action: IBasketRemoveProductAction) => {
    const {
        payload: { uuid },
    } = action;
    const { [uuid]: itemToRemove, ...restOfItems } = state;
    return restOfItems;
};

我的测试

product1 = { id: 1 };
product2 = { id: 2 };
mockUuid1 = 'IdTest';
mockUuid2 = 'IdTest2';
mockItem1 = { product: product1, quantity: 1 };
mockItem2 = { product: product2, quantity: 1 };
mockListItems = {
      [mockUuid1]: mockItem1,
      [mockUuid2]: mockItem2,
};

it('should handle PRODUCT__REMOVE', () => {
        expect(removeProductInList({ [mockUuid1]: mockItem1 }, removeProduct(mockUuid1))).toEqual({});

        expect(removeProductInList(mockListItems, removeProduct(mockUuid1))).toEqual({ [mockUuid2]: mockItem2 });

        expect(removeProductInList({}, removeProduct('acac'))).toEqual({});
    });

我希望通过我的实际测试涵盖所有功能。

我的笑话版本是23.4.1,以防万一。

【问题讨论】:

  • 您可能会使用 state 仅使用 uuid 属性对其进行测试

标签: javascript typescript ecmascript-6 jestjs


【解决方案1】:

您只需在 ts-jest 中的 tsConfig 文件中指定目标 esnext。

// jest.config.js

module.exports =  {
 ...
 'ts-jest': {
   ....
   tsConfig: './tsconfig.json'),
},

// tsconfig.json

{
  ...,
  "target": "esnext",
}

【讨论】:

  • 谢谢!我确实怀疑是配置问题
【解决方案2】:

尝试用额外的属性测试removeProductList

expect(removeProductInList({ [mockUuid1]: mockItem1, extraProp: 'someValue' }, removeProduct(mockUuid1))).toEqual({});

【讨论】:

  • 我认为那是我在那次测试中所做的,不是吗? expect(removeProductInList(mockListItems, removeProduct(mockUuid1))).toEqual({ [mockUuid2]: mockItem2 });
猜你喜欢
  • 2011-07-03
  • 2021-02-21
  • 1970-01-01
  • 2022-10-26
  • 2023-01-03
  • 2020-05-12
  • 2015-02-02
  • 2016-06-21
  • 1970-01-01
相关资源
最近更新 更多