yarn and coverage with Istanbul

Created on 30 Nov 2018  路  20Comments  路  Source: yarnpkg/yarn

Using nyc (aka istanbul) and yarn with a version newer than 1.10.1 does not give me the coverage result.

package.json:

{
...
    "scripts": {
        "coverage": "nyc --reporter=lcov --reporter=text-summary yarn test"
    }
    "devDependencies": {
        "nyc": "13.1.0"
    }
...
}

With yarn:

$ yarn coverage
...
=============================== Coverage summary ===============================
Statements   : Unknown% ( 0/0 )
Branches     : Unknown% ( 0/0 )
Functions    : Unknown% ( 0/0 )
Lines        : Unknown% ( 0/0 )
================================================================================

using nyc directly:

$ ./node_modules/.bin/nyc --reporter=lcov --reporter=text-summary yarn test
...
=============================== Coverage summary ===============================
Statements   : 85.01% ( 2689/3163 )
Branches     : 73.39% ( 979/1334 )
Functions    : 92.45% ( 722/781 )
Lines        : 85.14% ( 2612/3068 )
================================================================================

using npm (or changing package.json to use npm test instead of yarn test)

$ npm run coverage
...
=============================== Coverage summary ===============================
Statements   : 85.01% ( 2689/3163 )
Branches     : 73.39% ( 979/1334 )
Functions    : 92.45% ( 722/781 )
Lines        : 85.14% ( 2612/3068 )
================================================================================

I use nodejs 10.14 and ubuntu 18.04.
This looks like:
https://github.com/yarnpkg/yarn/issues/6650
https://github.com/yarnpkg/yarn/issues/6678
https://github.com/yarnpkg/yarn/issues/6608

triaged

Most helpful comment

Alright, upon upgrading the real yarn on my machine, it doesn't work. Since I had 2 yarns running, I hardcoded the path in the package.json, like so:

{
...
    "scripts": {
        "coverage": "nyc --reporter=lcov --reporter=text-summary /home/quentin/.npm-packages/bin/yarn test"
    }
...
}

And it worked, but not for the reason I though. If I don't hardcode the path to yarn, even with 1.13.0, it doesn't report coverage with nyc. If I make nyc run which yarn, it gives me this:

$ yarn coverage
yarn run v1.13.0
$ nyc --reporter=lcov --reporter=text-summary which yarn
/tmp/yarn--1547824229222-0.5258100608719858/yarn

This /tmp/yarn--1547824229222-0.5258100608719858/yarn contains:

#!/bin/sh

exec "/usr/bin/node" "/usr/bin/yarn" "$@"

Which looks like the same problem that was linked by @jeremad. I just accidentally bypassed it by hardcoding the path to yarn.

All 20 comments

Watching..

Confirmed, downgraded to 10.9 and fixed this issue

Can confirm this is the case with Node 11.4.0 and yarn 1.12.3, nyc 13.1.0. Switched back to npm, and coverage report returned to expected output.

Will be fixed in the next release, by the end of the week (fixed in https://github.com/yarnpkg/yarn/pull/6747)

Great, thanks.

Btw, I tested 1.13.0 pre-release with nyc, and it works now!

Alright, upon upgrading the real yarn on my machine, it doesn't work. Since I had 2 yarns running, I hardcoded the path in the package.json, like so:

{
...
    "scripts": {
        "coverage": "nyc --reporter=lcov --reporter=text-summary /home/quentin/.npm-packages/bin/yarn test"
    }
...
}

And it worked, but not for the reason I though. If I don't hardcode the path to yarn, even with 1.13.0, it doesn't report coverage with nyc. If I make nyc run which yarn, it gives me this:

$ yarn coverage
yarn run v1.13.0
$ nyc --reporter=lcov --reporter=text-summary which yarn
/tmp/yarn--1547824229222-0.5258100608719858/yarn

This /tmp/yarn--1547824229222-0.5258100608719858/yarn contains:

#!/bin/sh

exec "/usr/bin/node" "/usr/bin/yarn" "$@"

Which looks like the same problem that was linked by @jeremad. I just accidentally bypassed it by hardcoding the path to yarn.

I'm still getting Unknown% ( 0/0 ) for converage when calling a yarn command with nyc.

I'm using yarn 1.13.0 and nyc v13.1.0.

Is there another idea for a fix? Has anyone found a good work-around?

The absolute path to yarn work-around might not work in my case due to this library being run by different developers on different environments where yarn lives on different paths.

@sjl2 It looks like it has something to do with the new node version, it works in 11.6 and not in 11.7. -> https://github.com/istanbuljs/nyc/issues/921#issuecomment-457390609

Haven't looked into it myself yet, I'll see if I can make it work.

Interesting. Well, I can confirm it's also occurring on node 8.10.0 as well. Unfortunately, upgrading Node (besides minor versions) will not be an option because this is for an AWS Lambda service.

Confirmed issue on node 8.11.1 as well, so doesn't appear to be node related, especially considering npm can run it fine.

I'm seeing the same issue in npm as well FWIW

same. yarn + nyc not showing coverage :(

Same over here! What's the workaround other than calling nyc npm test

Are there any known workarounds for this issue, besides using npm?
I seem to be running into this issue with yarn 1.19.1, node 8.11.4, and nyc 14.1.1
For me it seems to just be outputting 0% coverage (not Unknown%), but npm still outputs the expected amount.

Same here with yarn 1.19.1, node 12.3.1 and nyc 14.1.1.

nyc yarn mocha ... leads to 0% coverage on all files.
nyc npm run mocha ... leads to fine coverage results.

I've seen what appears to be an incompatibility between yarn and spawn-wrap. To everyone having this issue please test nyc@next which by default uses a different method of wrapping child processes. Note that this updated version requires node.js 8 and is a semver-major release, so please check the release notes posted to https://github.com/istanbuljs/nyc/issues/1104#issuecomment-555103942. If your project depends on babel-plugin-istanbul or other istanbul* modules it is important that you update all to the next version, I've seen issues when testers accidentally mixed latest and next libraries.

The hope is for nyc and related modules to be out of pre-release soon but this is a very complex module so I'm being cautious.

@coreyfarrell it works with nyc@next (currently [email protected]).

Was _banging my head against the wall_ with this in Yarn 1.21.0, upgraded to nyc@next and it's working 馃憤馃徏 Great work whoever fixed this!

fixed with nyc 15.0.0

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MunifTanjim picture MunifTanjim  路  3Comments

chiedo picture chiedo  路  3Comments

victornoel picture victornoel  路  3Comments

davidmaxwaterman picture davidmaxwaterman  路  3Comments

sebmck picture sebmck  路  3Comments