我正在使用 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/