Configuring jest for code coverage when code is symlinked in node_modules

Viewed 869

I am attempting to restructure a Node project using Jest for testing. Initially, I had all of my .js and .test.js files in the same directory, and all was well in the world— i.e. jest --coverage did what I expected.

I've changed my directory structure now, and testing works, but my coverage reporting does not.

Here is my directory structure, edited to remove node_modules except for a symlink to my lib folder. I am following this hint for requiring my module by symlink.

❯ tree
.
├── README.md
├── build
│   └── index.js
├── jest.config.js
├── lib
│   ├── path.js
│   ├── pen.js
│   ├── section.js
│   ├── sprite.js
│   └── utilities.js
├── node_modules
│   ├── itch <= symlink to my `lib` folder
├── package-lock.json
├── package.json
└── test
    ├── env
    │   └── foo.html
    ├── path.test.js
    ├── pen.test.js
    ├── section.test.js
    ├── sprite.test.js
    └── utilities.test.js

Here is my jest.config.js

module.exports = {
  verbose: true,
  collectCoverage: true,
  collectCoverageFrom: ["**/lib/**/*.js", "**/test/**/*.js"]
};

and here is my scripts in package.json

"scripts": {
    "test": "jest",
    "build": "mkdir -p ./dist && browserify index.js --standalone Itch > ./dist/itch.js"
}

npm test works, except my code coverage report is all 0, as:

Test Suites: 5 passed, 5 total
Tests:       38 passed, 38 total
Snapshots:   0 total
Time:        1.316s
Ran all test suites.
--------------|----------|----------|----------|----------|----------------|
File          |  % Stmts | % Branch |  % Funcs |  % Lines |Uncovered Lines |
--------------|----------|----------|----------|----------|----------------|
All files     |        0 |        0 |        0 |        0 |                |
 path.js      |        0 |        0 |        0 |        0 |... 109,111,115 |
 pen.js       |        0 |        0 |        0 |        0 |... 106,107,113 |
 section.js   |        0 |        0 |        0 |        0 |... 89,90,92,95 |
 sprite.js    |        0 |        0 |        0 |        0 |... 200,202,205 |
 utilities.js |        0 |        0 |        0 |        0 |... 55,57,58,62 |
--------------|----------|----------|----------|----------|----------------|

But when I return to the .js and .test.js files being in the same directory, my code coverage is 100%.

I assume my issue is either with my collectCoverageFrom variable and/or my use of a symlink to require my modules.

Prompted by this issue, I also tried adding a package.json with the same name attribute as my package into my lib directory, to no avail.

In order to test the idea that the issue was with the symlink, I also tried removing the symlink and moving my lib folder directly into my node_modules folder. Then, when I ran npm test, I received all Unknown test coverage, viz.

Test Suites: 5 passed, 5 total
Tests:       40 passed, 40 total
Snapshots:   0 total
Time:        1.576s
Ran all test suites.
----------|----------|----------|----------|----------|----------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines |Uncovered Lines |
----------|----------|----------|----------|----------|----------------|
All files |  Unknown |  Unknown |  Unknown |  Unknown |                |
----------|----------|----------|----------|----------|----------------|

So now, I'm a bit stuck. Any help would be appreciated. Thank you!

0 Answers
Related