Skip to content

uscreen/tap-esm-coverage-issue

Repository files navigation

tap-esm-coverage-issue

How to get test coverage reported with tap testing esm?

CommonJS

module.exports.classify = (n) => {
  if(n >= 2) return 'large'
  return 'small'
}

using const tap = require('tap') in test.js to test, perfectly reports coverage:

❯ tap test.js

test.js
  test(1)
    ✓ should be equal

  test(2)
    ✓ should be equal


  2 passing (504.939ms)
----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files |     100 |      100 |     100 |     100 |
 index.js |     100 |      100 |     100 |     100 |
----------|---------|----------|---------|---------|-------------------

ES Modules

export const classify = (n) => {
  if(n >= 2) return 'large'
  return 'small'
}

using import tap from 'tap' in test.mjs to test, reports no coverage at all:

❯ tap test.mjs

test.mjs
  test(1)
    ✓ should be equal

  test(2)
    ✓ should be equal


  2 passing (254.006ms)
----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files |       0 |        0 |       0 |       0 |
----------|---------|----------|---------|---------|-------------------

Workaround: use c8

with c8 wrapping tap, coverage metrics get reported natively (--no-coverage option used to supress the empty tap report):

❯ c8 tap test.mjs --no-coverage

test.mjs
  test(1)
    ✓ should be equal

  test(2)
    ✓ should be equal


  2 passing (187.043ms)
-----------|---------|----------|---------|---------|-------------------
File       | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
-----------|---------|----------|---------|---------|-------------------
All files  |     100 |      100 |     100 |     100 |
 index.mjs |     100 |      100 |     100 |     100 |
-----------|---------|----------|---------|---------|-------------------

About

How to get test coverage reported with tap testing esm

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published