How do I get Istanbul to recognize code coverage when using ESM?
Asked Answered
C

1

10

I'm using ESM to loading my modules and I use them in this way:

// More info on why this is needed see (https://github.com/mochajs/mocha/issues/3006)
async function wire(){
    await import("./Sanity.spec.mjs"); 
    await import("./Other.spec.mjs");
    run();
}
wire();

I run these tests using nyc mocha --delay --exit ./test/suite.js, but when I run Istanbul it does not seems to recognize my imports and fails to provide coverage information...

  3 passing (14ms)

----------|----------|----------|----------|----------|-------------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files |        0 |        0 |        0 |        0 |                   |
----------|----------|----------|----------|----------|-------------------|

How can I get Istanbul to recognize the ESM loaded code?

Casillas answered 15/2, 2019 at 20:44 Comment(1)
I had been struggling with this for a while. Haven't find solution. It may be a bug, since in my simple libraries nyc + esm seemes working OK until certain point in time, when my codebase becomes more complex. You can try to tune your command with include and exclude filters, I got non-empty coverage after that, but haven't achieve desired state. I ended up transpile my code to CJS and run coverage on it. Report becomes messier, but at least I got actual info. Hope this helps you. If you'll find solution, you can answer your question. I would love to investigate your solution too.Meacham
R
0

Native ESM support is available from Mocha v7.1.0 (February 2020).

See:

Rendarender answered 23/3, 2021 at 10:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.