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

Rerun Test Coverage Multiple Times Produce Different Coverage Results #1124

Closed
1 task done
ZehuaZhang opened this issue May 30, 2019 · 10 comments
Closed
1 task done

Comments

@ZehuaZhang
Copy link

ZehuaZhang commented May 30, 2019

Link to bug demonstration repository

I've followed this link in nyc npm, since we use typescript

Run tests multiple times with test coverage enabled results in different results-

Expected Behavior

image

Observed Behavior

Half times is 85%
image

Half times is 68%
image

Interesting fact
When the percentage is higher, which is correct, I could see each code file coverage
But when the percentage is lower, which is incorrect, when I click each code file, some files instead of showing the code and coverage for each line, shows below message

image

Troubleshooting steps

  • still occurring when I put cache: false in my nyc config
    This does nothing for me

Environment Information

# paste the output here

System:
    OS: macOS 10.14.4
    CPU: (12) x64 Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
    Memory: 567.67 MB / 16.00 GB
  Binaries:
    Node: 12.3.1 - /usr/local/bin/node
    Yarn: 1.16.0 - /usr/local/bin/yarn
    npm: 6.9.0 - /usr/local/bin/npm
  npmPackages:
    nyc: ^14.1.1 => 14.1.1 
    source-map-support: ^0.5.12 => 0.5.12 
    ts-node: ^8.1.0 => 8.2.0 
    typescript: ^3.1.1 => 3.5.1 
@coreyfarrell
Copy link
Member

We require a link to a minimal repository demonstrating your issue.

@tilap35173
Copy link

I created a small project that demonstrating this issue.

The link: https://github.com/tilap35173/nyc-issues

@godu
Copy link

godu commented Jun 5, 2019

I have the same issue with nyc/ava.

After some investigation, I identifie some troubes.
In @tilap35173 's example, we have two report whict must be merge :

{
    "./nyc-issues/src/compareNumbers.ts": {
        "path": "./nyc-issues/src/compareNumbers.ts",
        "s": {
            "0": 0,
            "1": 0,
            "2": 0,
            "3": 0,
            "4": 0,
            "5": 0,
            "6": 0
        },
		...
    },
	...
}
{
    "./nyc-issues/src/compareNumbers.ts": {
        "path": "./nyc-issues/src/compareNumbers.ts",
        "s": {
            "0": 1,
            "1": 1,
            "2": 1,
            "3": 1,
            "4": 0,
            "5": 0,
            "6": 0,
            "7": 1,
            "8": 28,
            "9": 6,
            "10": 22,
            "11": 1,
            "12": 21,
            "13": 3,
            "14": 18,
            "15": 1
        },
		...
    }
}

We can see that istanbul doesn't identify the same list of statement.
Cannot read property 'start' of undefined doesn't throws when nyc try to merge the second before the first (reports are sort by filename).

But why do we have two different mapping ? In processinfo folder, we have more informations.

{
    "uuid": "d779d080-2dfb-40e1-bb91-2097dcc97f13",
    "argv": [
        "/Users/arthurweber/.nvm/versions/node/v8.15.1/bin/node",
        "/Users/arthurweber/Sites/nyc-issues/node_modules/.bin/nyc",
        "mocha"
    ],
	...
}
{
    "uuid": "e8849878-fe5a-4639-8d58-81a8a26b69b7",
    "argv": [
        "/Users/arthurweber/.nvm/versions/node/v8.15.1/bin/node",
        "/Users/arthurweber/Sites/nyc-issues/node_modules/mocha/bin/_mocha",
        "test/**/*.spec.ts",
        "--no-config",
        "--no-package",
        "--no-opts",
        "--diff",
        "--color",
        "--extension",
        "ts",
        "--extension",
        "js",
        "--reporter",
        "min",
        "--require",
        "ts-node/register",
        "--ui",
        "bdd",
        "--slow",
        "75",
        "--timeout",
        "2000"
    ],
	...
}

In addition, I try to remove --all option. The first report isn't generated.

In my opinion, nyc try to parse source files because of --all, but it doesn't use the same process as mocha (or ava in my case).

Maybe we need to pass some --require options to nyc ?

@godu
Copy link

godu commented Jun 5, 2019

I may have a solution :

  • .nycrc
{
	...
	"require": ["ts-node/register"]
}

@coreyfarrell
Copy link
Member

@godu thanks for catching this. Having nyc perform the require for all transpilers is very important when you enable the nyc.all option. Without sources are not pre-proccessed by ts-node/register during processing of all: true, but ts-node/register is run by mocha which effects files when they are required by tests (so they two stages see different results). This can cause weird issues.

@godu
Copy link

godu commented Jun 5, 2019

We could add this configuration as default in my opinion.
istanbuljs/istanbuljs#415

coreyfarrell added a commit to coreyfarrell/nyc that referenced this issue Aug 1, 2019
Sometimes the coverage data produced by `nyc --all` is incompatible with
the coverage data produced by actual test runs.  This is generally due
to configuration error but results in inconsistent coverage reports or
in some cases causes `nyc report` to crash.  The workaround is
implemented in istanbul-lib-coverage to drop coverage data associated
with `nyc --all` when coverage data from a test run is found.  This
commit tags the coverage data when appropriate so the coverage merge
logic knows what to do.

Fixes istanbuljs#1113, istanbuljs#1124, istanbuljs#1148
coreyfarrell added a commit that referenced this issue Aug 2, 2019
…#1155)

* fix: Drop coverage produced by `nyc --all` for files that were tested

Sometimes the coverage data produced by `nyc --all` is incompatible with
the coverage data produced by actual test runs.  This is generally due
to configuration error but results in inconsistent coverage reports or
in some cases causes `nyc report` to crash.  The workaround is
implemented in istanbul-lib-coverage to drop coverage data associated
with `nyc --all` when coverage data from a test run is found.  This
commit tags the coverage data when appropriate so the coverage merge
logic knows what to do.

Fixes #1113, #1124, #1148
@stale
Copy link

stale bot commented Aug 4, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Aug 4, 2019
@bcoe
Copy link
Member

bcoe commented Aug 6, 2019

@coreyfarrell my hunch is this might be addressed by some of the work you've been doing around --all.

@stale stale bot removed the stale label Aug 6, 2019
@coreyfarrell
Copy link
Member

Yes I had expected the PR to close this issue, apparently I formatted incorrectly.

This issue should be resolved in nyc@15. Any interested parties can follow #1104 for progress on the new release.

@tananai
Copy link

tananai commented Dec 2, 2019

@godu thank you so much! It works

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

6 participants