c8 icon indicating copy to clipboard operation
c8 copied to clipboard

Line in constructor of class that extends another class, calls super, and has a return statement erroneously marked as uncovered

Open aral opened this issue 4 years ago • 0 comments

  • Version: v14.15.4
  • Platform: Linux 5.4.0-58-generic #64~18.04.1-Ubuntu SMP Wed Dec 9 17:11:11 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

Summary

When:

  1. class A extends B and
  2. class A’s constructor calls super() and
  3. class A constructor contains a return statement

Then:

The line containing the closing brace of the constructor method is shown as uncovered by c8.

To reproduce

(Testing with latest c8, tape and tap-nyc in a "type": "module" node project.)

  1. Create class A in A.js:
import B from './B.js'

export default class A extends B {
  constructor () {
    super()
    return
  }
}
  1. Create class B in B.js:
export default class B {}
  1. Create the test in test.js:
import test from 'tape'
import A from './A.js'

test('bug', t => {
  const a = new A()
  t.true(a instanceof A, 'the returned object is as expected')
  t.end()
})
  1. Run coverage:
 npx c8 node test.js | npx tap-nyc

What should happen

Coverage should be 100%

What actually happens

Class A’s coverage is 66.67%, with line 7 flagged as uncovered.

Workaround

Ignore the return statement and the line after it. e.g., modify the constructor in class A from the above example to match the following:

import B from './B.js'

export default class A extends B {
  constructor () {
    super()
    /* c8 ignore next 2 */
    return
  }
}

aral avatar Feb 26 '21 14:02 aral