ccampbell / luna-testing

Simple, modern, opinionated JavaScript unit testing
https://craig.is/testing/code
MIT License
151 stars 6 forks source link

Code coverage report is empty #7

Closed Asuza closed 6 years ago

Asuza commented 6 years ago

Given the following file structure:

src
 - example.js
test
 - my-first-test.js

example.js

module.exports = {
    everything: function () {
        return 'cool';
    }
}

my-first-test.js

let { everything } = require('../src/example');

export function testThatEverythingIsCool(t) {
    let result = everything();
    t.assert(result === 'cool', 'Make sure that everything is cool');
}

Running npx luna test/* --node --verbose results in the following:

🌙  Running tests…
✔︎  [test/my-first-test.js] testThatEverythingIsCool
   All tests passed!

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

=============================== Coverage summary ===============================
Statements   : Unknown% ( 0/0 )
Branches     : Unknown% ( 0/0 )
Functions    : Unknown% ( 0/0 )
Lines        : Unknown% ( 0/0 )
================================================================================

   HTML coverage report available at coverage/lcov-report/index.html
   Took 0.55 seconds

Maybe I'm running it wrong somehow, but shouldn't it be showing 100% coverage for this simple test?

ccampbell commented 6 years ago

Ya this is certainly strange. I am looking into it now. If you change example.js to be

export function everything() {
    return 'cool';
}

And the test file to be

import { everything } from '../src/example';

export function testThatEverythingIsCool(t) {
    let result = everything();
    t.assert(result === 'cool', 'Make sure that everything is cool');
}

Then it gets back the expected response:

$ npx luna test/* --node --verbose
🌙  Running tests…
✔︎  [test/my-first-test.js] testThatEverythingIsCool
đź’Ż  All tests passed!

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

=============================== Coverage summary ===============================
Statements   : 100% ( 1/1 )
Branches     : 100% ( 0/0 )
Functions    : 100% ( 1/1 )
Lines        : 100% ( 1/1 )
================================================================================

đź’ľ  HTML coverage report available at coverage/lcov-report/index.html
⚡️  Took 0.72 seconds

I think this has to do with how the code coverage is being calculated. I am using a rollup plugin to do it, and that means the plugin probably will not instrument any code that uses require() statements. I understand that may be pretty limiting for what you are trying to achieve here. I need to think on it. I know nyc can be used for these kinds of things, but I wanted to avoid having to use that to wrap Luna.

ccampbell commented 6 years ago

I wanted to also point out that this wouldn’t be a problem if all of your sources use ES6 modules, but you are right that node may not support that out of the box for a while. For other require statements that resolve to third party node modules (located in node_modules), it would probably be fine if they do not show up in the coverage reporting since they are not part of the source code.

Node v10.4.1 supports it if you use the --experimental-modules flag when running your application.

Asuza commented 6 years ago

Thanks for the info! I just went down the rabbit hole reading about ES6 module support in Node and it's exhausting. I might be able to do that for personal projects, just not for what I'm doing at the moment.

ccampbell commented 6 years ago

Ya I totally understand that. I will continue to investigate what might be possible on my end in the meantime. One other possibility is you could ditch the code coverage built into Luna -x flag and then use nyc to generate them. I’m not certain it will work, but it may.

Since Luna was mainly designed to run for browser based code and to be forward thinking, I don’t want to spend a ton of time trying to support coverage reports for a coding standard that will likely be phased out soon (the tests themselves will still execute fine).

Asuza commented 6 years ago

No worries, it's not important. Though while playing around, it seems that while -x does work to stop coverage, --no-coverage does not work.

ccampbell commented 6 years ago

Haha yup. that looks like a bug with yargs though. Or at least my expectation of how it should work. If you pass --no-coverage=1 it works, but passing it alone does not work.

Oh wow. It is the library itself being smarter than I am. If you name a flag --no-{flag} it comes in as flag: false. Instead of 'no-{flag}': true which was what I expected here. Personally I think the library is trying to be too smart, it should add both, but it is an easy fix

ccampbell commented 6 years ago

Aha! I found a solution…

If you do

npm install nyc

Then run as

npx nyc luna test/* --node --verbose

it works!