【问题标题】:Jest coverage issue笑话覆盖问题
【发布时间】:2018-03-06 15:39:13
【问题描述】:

几天前我开始使用 Jest,我试图获得 100% 的覆盖率,但我有一个简单的问题,我无法解决。

这是 Jest Coverage 的示例

 File          |% Stmts |% Branch |% Funcs |  % Lines | Uncovered Line #s |  
  src/lib      |  66.67 |       0 |     10 |      100 |                   |
   theme.js    |  66.67 |       0 |     10 |      100 |             64,81 |

因此,我找到了第 64 行和第 81 行。这里是 styled-components 的简单函数。

export const fontSize = (fontSize, lineHeight) => (`
  font-size: ${fontSize}px;
  line-height: ${lineHeight ? `${lineHeight}px` : '1.1'}; // that's 64
`);

export const gradient = (gradType, gradDirection, gradStart, gradEnd) => (`
  background: ${gradType}-gradient(${gradType === 'linear' ? gradDirection+',' : ''} ${gradStart}, ${gradEnd}); // that's 81
`);

如何测试它们?我试过了,但还是得到了 64 和 81 发现的信息。

const grad = {
      gradType: 'linear',
      gradDirection: 'toTop',
      gradStart: '#000000',
      gradEnd: '#ffffff',
    };

expect(`background:${grad.gradType}-gradient(${grad.gradType === 'linear' ? grad.gradDirection+',' : ''}${grad.gradStart},${grad.gradEnd});`)
      .toEqual(`background:linear-gradient(toTop,#000000,#ffffff`);`);

【问题讨论】:

    标签: reactjs unit-testing jestjs


    【解决方案1】:

    为了测试你的函数,你必须调用它:

    // theme.test.js
    const { fontSize } = require('./theme');
    
    expect(fontSize('size', 'height')).toBe(`
      font-size: sizepx;
      line-height: heightpx; // that's 64
    `);
    
     expect(fontSize('size', null)).toBe(`
      font-size: sizepx;
      line-height: 1.1; // that's 64
    `);
    

    【讨论】:

    • 非常感谢!现在我明白了!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-16
    相关资源
    最近更新 更多