【问题标题】:Jest Uncovered Lines - How do I test these lines..?Jest Uncovered Lines - 我如何测试这些线..?
【发布时间】:2018-02-16 02:08:03
【问题描述】:

我使用 Jest 并启用了覆盖选项,我得到:

--------------------------|----------|----------|----------|----------|----------------|
File                      |  % Stmts | % Branch |  % Funcs |  % Lines |Uncovered Lines |
--------------------------|----------|----------|----------|----------|----------------|
progress-bar.js           |      100 |       75 |      100 |      100 |             17 |
--------------------------|----------|----------|----------|----------|----------------|

所以我有 17 条未覆盖的线,但我不知道如何覆盖它们。

progress-bar.js

import ProgressBar from 'progress'
import isNumber from 'lodash/isNumber'
import config from '../../config/global'

function progressBar (total) {
  if (!isNumber(total)) throw new Error(`progressBar() 'total' arg is not a number.`)

  const barCfg = config.progressBar

  const tokens = `${barCfg.bar} ${barCfg.info}`

  const options = {
    width: barCfg.width,
    total: total || 1,
    complete: barCfg.complete,
    incomplete: barCfg.incomplete
  }

  const bar = new ProgressBar(tokens, options)

  bar.render()

  return bar
}

export default progressBar

progress-bar.test.js

import ProgressBar from 'progress'
import progressBar from './progress-bar'

describe('progressBar()', () => {
  test('returns instanceof ProgressBar', () => {
    const actual = progressBar(5) instanceof ProgressBar
    const expected = true
    expect(actual).toBe(expected)
  })

  test('throw error if arg "total" is not a number', () => {
    expect(() => { progressBar('moo') }).toThrow()
    expect(() => { progressBar(null) }).toThrow()
  })

  test('progress bar progress/ticking', () => {
    const bar = progressBar(5)
    expect(bar.total).toBe(5)
    expect(bar.curr).toBe(0)
    bar.tick()
    expect(bar.curr).toBe(1)
    bar.tick()
    expect(bar.curr).toBe(2)
    bar.tick()
    expect(bar.curr).toBe(3)
    bar.tick()
    expect(bar.curr).toBe(4)
    bar.tick()
    expect(bar.curr).toBe(5)
    expect(bar.complete).toBe(true)
  })
})

所以我正在测试参数和返回值。

如何全面测试这个功能,包括 17 行..?

【问题讨论】:

    标签: javascript unit-testing jestjs


    【解决方案1】:

    好的,我现在正坐在角落里,戴着我的傻帽。

    找到这个:https://github.com/istanbuljs/nyc/issues/35#issuecomment-121008298

    Uncovered Lines = 17 不是未覆盖行的计数,它是一个只有一个值的列表,即第 17 行:total: total || 1,

    固定...

    test('passing zero forces the default total of 1', () => {
      const bar = progressBar(0)
      expect(bar.total).toBe(1)
    })
    

    【讨论】:

    • 我有数百万未发现的行,并且认为依赖图在 JS 中很疯狂 :)
    【解决方案2】:

    对于有此问题但知道这是行号的其他人

    如果“未覆盖的线”为黄色,则表示它们被部分覆盖。您可以通过生成覆盖范围的 HTML 报告找出哪些特定属性未转换:

    npx jest --coverage --coverageDirectory='coverage'

    然后打开coverage/lcov-report/index.html,它将显示未经测试的特定代码路径

    在我的例子中,一个函数有一个默认的 arg foo = false,它正在使用不同的 arg 值进行测试,但没有测试默认值,所以它显示为黄色。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-09
      • 2019-03-11
      • 1970-01-01
      • 1970-01-01
      • 2016-01-19
      • 1970-01-01
      • 2015-08-15
      • 2021-03-17
      相关资源
      最近更新 更多