Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nyc test coverage on branches is not accurate - Seems to be happening with nyc@v15, mocha@10.2.0 and Node@v18.17 #1540

Open
1 task done
varunrajasekhar opened this issue Dec 21, 2023 · 8 comments

Comments

@varunrajasekhar
Copy link

varunrajasekhar commented Dec 21, 2023

Link to bug demonstration repository

Expected Behavior

Branch Coverage to be giving more accurate value

Observed Behavior

Branch Coverage seems to be wrong from our end

Troubleshooting steps

  • still occurring when I put cache: false in my nyc config

The Nyc branch coverage seems to be a bit off to me. I think it is giving me wrong branch coverages.The value and the tests dont seem to be valid to me. We recently upgrade our legacy app from istanbul to nyc. Istanbul had much more valid coverage listed.

Our app is using Node 18.17.0 (cannot downgrade - only option is to upgrade) Nyc - 15.1.0, corresponding mocha => 10.2.0, sinon => 1.17.3 (Since we use sinon stubs)

Our "npm run test" scripts is "test": "nyc node_modules/.bin/mocha -- -u bdd -R spec test/server//.js --exit",

nyc.config.js { "all": true, "include": ["test/server/controllers/.js","test/server/services/.js","test/server/utils/**.js"], "exclude":["test/server/index","test/server/coverage/lcov-report", "target", "server"], "reporter":["html", "cobertura", "text", "text-summary"], "check-coverage": true, "report-dir":"./target/coverage" }

What am I supposed to do on this to have the correct branch coverage ?

I am also attaching the istanbul test report

This one is Istanbul Coverage report

Environment Information

Screenshot 2023-12-21 at 10 17 02 AM This one is Nyc report

Screenshot 2023-12-21 at 10 15 26 AM
This one is istanbul report

paste the output here

@varunrajasekhar
Copy link
Author

@varunrajasekhar
Copy link
Author

Seems to me that branch coverage is not getting updated in the nyc
image
Please check this here. The Object is always empty
@Lalem001 @XhmikosR @addaleax @coreyfarrell @bcoe

@jjuannn
Copy link

jjuannn commented Dec 29, 2023

I think i'm having a similar problem.- Seems that the report branch coverage is not being so accurate since it marks me like i'm not going through certaing lines that i'm already covering.

image

image

Did you managed to make it work of any way? @varunrajasekhar

@varunyarlagaddaTR
Copy link

varunyarlagaddaTR commented Dec 29, 2023

@jjuannn - I think it is the way nyc intercepts the branch coverage etc. I see in the core logic of nyc methods, the branchCoverage Object is unable to retrieve the correct mapping (always shows empty obj for all the files). I couldnt figure out why it does so. May it has something to do with istanbul v/s nyc configurations. Still waiting for response from the @SUPPORTERS, @maintainers

@zhangboli51
Copy link

having exactly same issue
node21 + nyc 15 +mocha 10

@zhangboli51
Copy link

My issue is resolved , in my case it's caused by @babel/node. which is used to support ES6 modules, but nyc seems confused with some ES6 module. So I have to stick with CommonJS modules, and this not only impact file where it contains ES6 module , but also other files not using ES6.... (so weird)
for example i have ES6 code :

export default helper

then I have to change to

module.exports = helper;

@gavinvangent
Copy link

gavinvangent commented Oct 10, 2024

I'm having this issue now too ... trying to upgrade a couple older projects to all the new things (latest typescript, tslint to eslint, node 20, etc)

What used to give me 100% for everything, is now failing on branches. Notice the uncovered lines mentioned is all the way from the start of each file (line 1) to the last line of each file:

image

Looking at the helpers.ts file:
image

And the html for that file:
image

My current setup:

Node 20.17.0
mocha 10.7.3
nyc 17.1.0
tsx 4.19.1

.mocharc.json

{
    "import": "tsx",
    "require": [
        "test/support/setup.ts"
    ],
    "spec": "test/**/*.test.ts",
    "reporter": "spec",
    "check-leaks": true
}

and test/support/setup.ts

import { should, use } from 'chai';
import chaiAsPromised from 'chai-as-promised';
import sinonChai from 'sinon-chai';

use(chaiAsPromised);
use(sinonChai);

should();

@gavinvangent
Copy link

gavinvangent commented Oct 10, 2024

as @zhangboli51 stated, if i change my exports to use module.exports = ... my coverage is correct. (i dont use @babel/node so unrelated)

example for helpers.ts

import { InvocationResponse } from '@aws-sdk/client-lambda';
import { NotFoundError, ForbiddenError, NotImplementedError, NotSupportedError, InputInvalidError, AppError } from './errors';

class LambdaHelpers {
    static parseResponse<T>(res: InvocationResponse): T {
        const payload = JsonHelpers.jsonParser<any>(new TextDecoder('utf8').decode(res.Payload));

        if (res.FunctionError) {
            switch (payload.errorType) {
                case NotFoundError.name:
                    throw new NotFoundError(payload.errorMessage);
                case ForbiddenError.name:
                    throw new ForbiddenError(payload.errorMessage);
                case NotImplementedError.name:
                    throw new NotImplementedError(payload.errorMessage);
                case NotSupportedError.name:
                    throw new NotSupportedError(payload.errorMessage);
                case InputInvalidError.name:
                    throw new InputInvalidError(payload.errorMessage);
            }

            throw new AppError(payload.errorMessage);
        }

        return payload;
    }
}

class JsonHelpers {
    static jsonParser<T>(value: string): T {
        return JSON.parse(value, JsonHelpers.jsonReviver);
    }

    static jsonReviver<T>(key: string, value: any): T {
        const dateFormat = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z$/;

        if (typeof value === 'string' && dateFormat.test(value)) {
            return new Date(value) as any as T;
        }

        return value;
    }
}

module.exports = {
    LambdaHelpers,
    JsonHelpers
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants