istanbuljs / nyc

the Istanbul command line interface
https://istanbul.js.org/
ISC License
5.55k stars 353 forks source link

No coverage collected for simple export const #1507

Open dominikschreiber opened 1 year ago

dominikschreiber commented 1 year ago

Repro

Also available as Gist: https://gist.github.com/dominikschreiber/76172650805f139787b151ef70222a22

Consider a file

// file.mjs
export const num = 1;

and a test

// test.mjs
import {num} from './file.mjs';
import assert from 'node:assert';
import {describe, it} from 'node:test';

describe('num', function() {
  it('should be 1', function() {
    assert.equal(num, 1);
  });
});

Now run that test with

# repro.sh
npx nyc -a -n file.mjs -r text -c false node --test test.mjs

Expected Behavior

All lines of file.mjs are tested, so there should be 100% coverage, i.e.

----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files |     100 |      100 |     100 |     100 |
 file.mjs |     100 |      100 |     100 |     100 | 
----------|---------|----------|---------|---------|-------------------

Observed Behavior

----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files |       0 |      100 |     100 |       0 |
 file.mjs |       0 |      100 |     100 |       0 | 1
----------|---------|----------|---------|---------|-------------------

Troubleshooting steps

Environment Information

System:
  OS: macOS 13.2
  CPU: (10) arm64 Apple M1 Max
  Memory: 1.76 GB / 32.00 GB
Binaries:
  Node: 19.5.0 - ~/.nvm/versions/node/v19.5.0/bin/node
  npm: 9.3.1 - ~/.nvm/versions/node/v19.5.0/bin/npm
npmPackages:
    nyc: ^15.1.0 => 15.1.0 
dominikschreiber commented 1 year ago

Small update: c8 behaves as expected given the same input:

npx c8 --all -n file.mjs -r text node --test test.mjs
# …
----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files |     100 |      100 |     100 |     100 |
 file.mjs |     100 |      100 |     100 |     100 |
----------|---------|----------|---------|---------|-------------------