javascript - Jest Uncovered Lines - 我如何测试这些线..?

标签 javascript unit-testing jestjs

我正在使用 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 行..?

最佳答案

好吧,我现在坐在 Angular 落里,戴着我的笨蛋帽子。

找到这个: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)
})

关于javascript - Jest Uncovered Lines - 我如何测试这些线..?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46094603/

相关文章:

javascript - 在 Jquery UI 对话框中禁用按钮?

javascript - 为什么在 JavaScript 中使用 "with"关键字?

javascript - Polymer - 分布式节点是否样式化然后分布式?

unit-testing - 如何使用 Gradle 只运行一个单元测试类

c# - C# 代码的单元测试,其中主要处理发生在 SQL 存储过程中

javascript - 在 Jest 中模拟 WebSocket

angular - 将 NgRx 效果作为函数进行单元测试

javascript - 如何使用 FileReader 的异步特性实现进度条和回调

javascript - 在 Jest 测试中使用expect.assertions(x) 与使用fail()

Angular Testing : mock a HttpResponse containing a blob