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

Jest coverage targets the wrong file #3968

Closed
dispix opened this issue Jul 5, 2017 · 12 comments
Closed

Jest coverage targets the wrong file #3968

dispix opened this issue Jul 5, 2017 · 12 comments

Comments

@dispix
Copy link

dispix commented Jul 5, 2017

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

Bug

What is the current behavior?

Considering this file tree:


app/services/
├── categories
│   ├── Parser.js
│   ├── __mocks__
│   │   └── Parser.js
│   ├── actions.js
│   ├── constants.js
│   ├── reducer
│   │   ├── data.js
│   │   ├── index.js
│   │   ├── list.js
│   │   └── tests
│   │       ├── data.test.js
│   │       └── list.test.js
│   ├── sagas.js
│   ├── selectors.js
│   └── tests
│       ├── Parser.test.js
│       ├── actions.test.js
│       └── selectors.test.js
├── index.js
├── items
│   ├── Parser.js
│   ├── __mocks__
│   │   └── Parser.js
│   ├── actions.js
│   ├── constants.js
│   ├── reducer
│   │   ├── data.js
│   │   ├── delete.js
│   │   ├── fetch.js
│   │   ├── index.js
│   │   ├── list.js
│   │   ├── next.js
│   │   ├── tests
│   │   │   ├── data.test.js
│   │   │   ├── delete.test.js
│   │   │   ├── fetch.test.js
│   │   │   ├── list.test.js
│   │   │   ├── next.test.js
│   │   │   └── update.test.js
│   │   └── update.js
│   ├── sagas.js
│   ├── selectors.js
│   └── tests
│       ├── Parser.test.js
│       ├── actions.test.js
│       └── selectors.test.js
└── router
    ├── reducer.js
    ├── selectors.js
    └── tests
        ├── reducer.test.js
        └── selectors.test.js

And my coverage when I run the npm run jest -- --coverage app/services/items/reducer/tests/list.test.js command:

---------------------------------|----------|----------|----------|----------|----------------|
File                             |  % Stmts | % Branch |  % Funcs |  % Lines |Uncovered Lines |
---------------------------------|----------|----------|----------|----------|----------------|
All files                        |     5.98 |     8.41 |    10.75 |     9.14 |                |
 services/categories             |        0 |        0 |        0 |        0 |                |
  Parser.js                      |        0 |        0 |        0 |        0 |1,3,22,27,28,30 |
  actions.js                     |        0 |      100 |        0 |        0 |... 20,27,34,38 |
  constants.js                   |        0 |      100 |      100 |        0 |         1,7,13 |
  selectors.js                   |        0 |      100 |        0 |        0 |1,7,12,20,23,25 |
 services/categories/reducer     |    33.33 |    45.83 |    33.33 |    42.11 |                |
  data.js                        |        0 |        0 |        0 |        0 |... 26,30,32,34 |
  list.js                        |      100 |      100 |      100 |      100 |                |
 services/items/reducer          |       70 |    80.82 |     87.5 |    73.21 |                |
  data.js                        |      100 |      100 |      100 |      100 |                |
  delete.js                      |      100 |      100 |      100 |      100 |                |
  fetch.js                       |      100 |      100 |      100 |      100 |                |
  list.js                        |        0 |        0 |        0 |        0 |... 40,42,44,46 |
  next.js                        |      100 |      100 |      100 |      100 |                |
  update.js                      |      100 |      100 |      100 |      100 |                |
 services/router                 |        0 |        0 |        0 |        0 |                |
  reducer.js                     |        0 |        0 |        0 |        0 |... 32,39,41,43 |
  selectors.js                   |        0 |      100 |        0 |        0 |1,7,12,20,23,25 |
---------------------------------|----------|----------|----------|----------|----------------|

(I removed some unrelated part of the coverage and file tree).

Jest coverage targets the services/categories/reducer/list file when running the tests on services/items/reducer/list. I import the right file and it is actually testing the right file, meaning that voluntarily changing something in services/items/reducer/list makes the tests fail.

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.

This comes from a private repository and I don't think I can give you a runnable example 😞

What is the expected behavior?

The coverage should target the right file.

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

OS: MacOS Sierra 10.12.5 (16F73)
Node: v7.10.0
Yarn: v0.24.6
Jest: v20.0.4
Jest configuration:

"jest": {
    "collectCoverageFrom": [
      "app/components/**/*.{js,jsx}",
      "app/services/**/*.{js,jsx}",
      "app/utils/**/*.{js,jsx}",
      "app/containers/**/reducer.js",
      "app/containers/**/selectors.js",
      "app/containers/**/actions.js",
      "app/containers/**/constants.js",
      "!app/**/*.test.{js,jsx}",
      "!app/*/RbGenerated*/*.{js,jsx}",
      "!app/app.js",
      "!app/routes.js",
      "!app/themes.js",
      "!app/global-styles.js",
      "!app/**/sagas.js",
      "!app/services/form/intlForm.js",
      "!app/services/**/index.js"
    ],
    "coverageThreshold": {
      "global": {
        "statements": 98,
        "branches": 91,
        "functions": 98,
        "lines": 98
      }
    },
    "moduleDirectories": [
      "node_modules",
      "app"
    ],
    "moduleNameMapper": {
      ".*\\.(css|less|styl|scss|sass)$": "<rootDir>/internals/mocks/cssModule.js",
      ".*\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/internals/mocks/image.js"
    },
    "setupTestFrameworkScriptFile": "<rootDir>/internals/testing/test-bundler.js",
    "testRegex": "tests/.*\\.test\\.js$"
  },
@dispix
Copy link
Author

dispix commented Jul 5, 2017

Additional informations:

  • If I add a simple console.log in my app/services/items/reducer/list files, the coverage now targets it as expected. I initially added one to make sure the right file was loaded and it not only showed up but also corrected the coverage.
  • Running the same command with --no-cache corrects the issue too.

@stipsan
Copy link
Contributor

stipsan commented Sep 6, 2017

Hey @dispix,

Can you test if this fix resolves your case? #4432

@dispix
Copy link
Author

dispix commented Sep 7, 2017

Hi @stipsan !

I was about to test it but got an error when trying to target the master branch from this repository:

devDependencies: {
  "jest-cli": "facebook/jest#master"
}
error /.../node_modules/jest-cli: Command failed.
Exit code: 1
Command: sh
Arguments: -c yarn run build --silent
Directory: /.../node_modules/jest-cli
Output:
module.js:487
    throw err;
    ^

Error: Cannot find module 'string-length'
    at Function.Module._resolveFilename (module.js:485:15)
    at Function.Module._load (module.js:437:25)
    at Module.require (module.js:513:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/.../node_modules/jest-cli/scripts/build.js:32:22)
    at Module._compile (module.js:569:30)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)
    at Function.Module._load (module.js:458:3)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.

Am I missing something with the way I should target a git repository as a dependency ?

@SimenB
Copy link
Member

SimenB commented Sep 7, 2017

I think you have to clone and build the repo locally. When referencing the git repo you don't get the dev deps necessary for building

@stipsan
Copy link
Contributor

stipsan commented Sep 7, 2017

Like @SimenB says the easiest way to test it is to:

  1. Create your local clone.
  2. Run yarn install in the clone (using yarn v0.27.5 or later as jest is using the workspaces feature).
  3. npm link to compile jest so you get the same files available as when you yarn add jest-cli right off npm.
  4. cd to your project and npm link jest-cli, this should setup a symlink for you to jest.

@dispix
Copy link
Author

dispix commented Sep 8, 2017

Just tried following the steps to link a local version of jest (npm link was throwing errors left and right) and this doesn't seem to fix the issue 😞

Using yarn@0.27.5 and the master branch of this repository.

@SimenB
Copy link
Member

SimenB commented Sep 8, 2017

No need to link, just clone, run yarn and then in your other project, do ../jest/jest

@dispix
Copy link
Author

dispix commented Sep 8, 2017

Doesn't change a thing though, both methods still display the wrong coverage.

@stipsan
Copy link
Contributor

stipsan commented Sep 14, 2017

Very strange. It might still be loading the old packages for babel-jest and jest-runtime since you get the same results.
Any chance you can create a repo that reproduces the problem? It'll help me check for sure if #4432 will fix it, if it doesn't I'll be able to work on a fix using your test case. :)

@jackjocross
Copy link

After upgrading to jest-runtime@21.2.1 and babel-jest@21.2.0 I am no longer experiencing this issue.

@robertmain
Copy link

I'm having a similar issue with Jest generating random or nonsensical coverage data in #4960

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants