Jest: Coverage folder not created

Created on 7 Feb 2017  Â·  7Comments  Â·  Source: facebook/jest


Do you want to request a feature or report a bug?

Report Bug

What is the current behavior?

When running jest --coverage no /coverage folder is output. This is showing the settings of Jest in the package.json

  "jest": {
    "coverageDirectory": "/coverage/",
    "coveragePathIgnorePatterns": [
      "/node_modules/",
      "/.node/",
      "/jest/"
    ]
  },

If the current behavior is a bug, please provide the steps to reproduce and either a repl.it demo through https://repl.it/languages/jest or a minimal repository on GitHub that we can yarn install and yarn test.

Run jest --coverage see coverage folder created then removed. The command line experience still shows the coverage output, but not .lcov or html report is generated that persists.

What is the expected behavior?

The /coverage folder should persist and be replaced next run.

Please provide your exact Jest configuration and mention your Jest, node, yarn/npm version and operating system.

node: 6.9.1
npm: 4.1.2
jest: 18.1.0
yarn: 0.20.0

Confirmed on both OSX and GitLab CI Node docker image (linux).

Output from GitLab-CI

$ npm --silent run test:coverage || true
 PASS  __tests__/pages/index.test.js
  With Enzyme
    ✓ App shows "Hello world!" (22ms)
  With Snapshot Testing
    ✓ App shows "Hello world!" (16ms)

----------|----------|----------|----------|----------|----------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines |Uncovered Lines |
----------|----------|----------|----------|----------|----------------|
All files |      100 |      100 |      100 |      100 |                |
 index.js |      100 |      100 |      100 |      100 |                |
----------|----------|----------|----------|----------|----------------|
Test Suites: 1 passed, 1 total
Tests:       2 passed, 2 total
Snapshots:   1 passed, 1 total
Time:        3.004s
Ran all test suites.
$ ls -al
total 320
drwxrwxrwx.   9 root root   4096 Feb  7 13:52 .
drwxrwxrwx.   4 root root   4096 Feb  7 13:51 ..
drwxrwxrwx.   5 root root   4096 Feb  7 13:51 .git
-rw-rw-rw-.   1 root root   1575 Feb  7 13:51 .gitignore
-rw-rw-rw-.   1 root root    584 Feb  7 13:51 .gitlab-ci.yml
-rw-rw-rw-.   1 root root   1098 Feb  7 13:51 .gitlab-ci.yml.bkup
-rw-rw-rw-.   1 root root     12 Feb  7 13:51 CHANGELOG.md
-rw-rw-rw-.   1 root root    628 Feb  7 13:51 README.md
drwxrwxrwx.   3 root root   4096 Feb  7 13:51 __tests__
drwxrwxrwx.   2 root root   4096 Feb  7 13:51 assets
drwxrwxrwx.   2 root root   4096 Feb  7 13:51 env
-rw-r--r--.   1 root root   3958 Feb  7 13:52 eslint-report.html
drwxr-xr-x.   3 root root   4096 Feb  7 13:52 jest
drwxr-xr-x. 688 root root  24576 Feb  7 13:52 node_modules
-rw-rw-rw-.   1 root root   2369 Feb  7 13:51 package.json
drwxrwxrwx.   2 root root   4096 Feb  7 13:51 pages
-rw-rw-rw-.   1 root root 168719 Feb  7 13:51 yarn.lock
Creating cache reports/15-setup-gitlab-ci-pipeline...
Created cache
Uploading artifacts...
eslint-report.html: found 1 matching files         
WARNING: coverage/: no matching files    

Notice that ls -al shows no coverage/ directory.

Locally when I run jest --coverage it throws permissions errors because it can not create folder /coverage. I run under sudo jest --coverage and the folder is created and remove. sudo is not needed on GitLab CI. I did everything I could to remove need for sudo locally and can not confirm what is causing it. No other application requires this. jest/ folder is created without sudo fine.

Most helpful comment

Hi, I could solve this problem by passing coverageDirectory: './coverage/', or even coverageDirectory: '<rootdir>/coverage/', in my _./.jestrc.js_ file (no permission errors nor sudo needed!). Maybe this helps you too...

I call it with:

"scripts": {
  "test": "jest --coverage --config=./.jestrc.js"
}

All 7 comments

Unfortunately this is most probably problem with your user (or process like node) permissions, not with Jest.

As a workaround you can create the directory manually or with some kind of script with right permissions.

When I create a folder manually, it still removes it. Even when I give it the correct permissions.

Additionally every other folder has no issues being created, including the /jest folder and the __snapshots__ folder.

This only occurs with the --coverage option. Additionally, if you read above, this happens on the CI service by GitLab. That image is a node docker container I did not make. That means that it is not restricted to just my environment.

Please re-read issue and do not dismiss. Please reopen.

On Feb 7, 2017, 5:06 PM -0600, Michał Pierzchała notifications@github.com, wrote:
>

As a workaround you can create the directory manually or with some kind of script with right permissions.

—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub (https://github.com/facebook/jest/issues/2825#issuecomment-278174014), or mute the thread (https://github.com/notifications/unsubscribe-auth/ABwmA7mekPM__K9dvjQtwVApYswb_yagks5raPjzgaJpZM4L5hds).

After more research I can confirm a workaround. The contents of the package.json file is preventing Jest to write to the coverage folder. I was wrong above, if I create a coverage folder, it just does not write too it.

By having the following in package.json, two things occur -

  "jest": {
    "coverageDirectory": "/coverage/",
    "coveragePathIgnorePatterns": [
      "/node_modules/",
      "/.node/",
      "/jest/"
    ]
  },
  1. Need sudo to not trigger access error for coverage.
    a. If i create a coverage folder with correct permissions, says I can not right to coverage-final.json
  2. Even with sudo, error is not triggered, but files are not written too.

This was resolved by removing coverage data from package.json. Is one of these command deprecated causing the issue?

Hi, I could solve this problem by passing coverageDirectory: './coverage/', or even coverageDirectory: '<rootdir>/coverage/', in my _./.jestrc.js_ file (no permission errors nor sudo needed!). Maybe this helps you too...

I call it with:

"scripts": {
  "test": "jest --coverage --config=./.jestrc.js"
}

Seems like there is a workaround, closing to keep the issue queue small.

I know that this has been closed for a while, but thought I'd post this to bring proper closure to this issue for the sake of others that may come here in the future seeking answers. The OP stated the following:

This was resolved by removing coverage data from package.json

This statement suggests that the intention was to generate the coverage folder in the root of the node package (i.e., at the same level with the package.json file), seeing that that is what would happen when the coverage settings are removed from the jest config in the package.json file. However, by setting the coverageDirectory to /coverage/, what is being stated is that the intention is to generate the coverage folder in the root (i.e., top most level) directory of the machine and because the current user account does not own that directory, it throws a permissions error. If the intention is for the coverage folder to be generated in the root of the node package (as opposed to the topmost directory on the machine), then the coverageDirectory setting needs to be set to ./coverage/ instead of /coverage/. So really, this is not a bug. It's actually the expected behaviour, and @deadratfink's solution is not a workaround, but the actual way to achieve what is really intended.

Was this page helpful?
0 / 5 - 0 ratings