Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jest Uncovered Lines - How do I test these lines..?

I'm using Jest with the coverage option on and I'm getting:

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

So I have 17 uncovered lines, but I'm not sure how to cover them.

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)
  })
})

So I'm testing the argument and return values.

How do I fully test this function, including the 17 lines..?

like image 240
Stephen Last Avatar asked Sep 07 '17 10:09

Stephen Last


People also ask

What is uncovered line Jest?

Testing uncovered linesThey are marked red, because they are completely untouched by any test. These lines are within an if-statement that is only entered when the passed two values together will be greater than 100.

What is branch in test coverage Jest?

Branch coverage is a requirement that, for each branch in the program (e.g., if statements, loops), each branch have been executed at least once during testing. (It is sometimes also described as saying that each branch condition must have been true at least once and false at least once during testing.)


2 Answers

Ok, I am now sitting in the corner with my dunce hat on.

Found this: https://github.com/istanbuljs/nyc/issues/35#issuecomment-121008298

Uncovered Lines = 17 isn't a count of the uncovered lines, it's a list with only one value, that is, line 17: total: total || 1,.

Fixed with...

test('passing zero forces the default total of 1', () => {
  const bar = progressBar(0)
  expect(bar.total).toBe(1)
})
like image 166
Stephen Last Avatar answered Oct 19 '22 04:10

Stephen Last


If you know what line number is uncovered, but can't figure out why the line is uncovered:

If the 'uncovered lines' are in yellow, it means they're partially covered. You can find out what specific attributes are not covered by producing an HTML report of the coverage:

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

Then open coverage/lcov-report/index.html which will show the specific codepaths which are untested

In my case, a function had a default arg foo = false, it was being tested with different values for the arg, but the default wasn't being tested so it was showing up as yellow.

like image 12
mikemaccana Avatar answered Oct 19 '22 04:10

mikemaccana