question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Global and single file coverage thresholds

See original GitHub issue

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

Feature

What is the current behavior?

Currently, when using both global and path/glob thresholds, the global config combines the coverage for remaining files only (as documented).

What is the expected behavior?

I’ll describe my use case. A global threshold is configured as following:

coverageThreshold: {
  global: {
    statements: 85,
    branches: 80,
    functions: 85,
    lines: 85
  }
}

This makes it possible to commit completely untested files as long as the overall coverage is fulfilled. To solve this issue I would like to enforce every file being tested for at least 50%:

coverageThreshold: {
  global: {
    statements: 85,
    branches: 80,
    functions: 85,
    lines: 85
  },
  
  "app/**/*.js": {
    statements: 50,
    branches: 50,
    functions: 50,
    lines: 50
  }
}

However, this won’t work as expected. First, running this config will fail if app/ and global point to the same files (which is expected as “If the file specified by path is not found, error is returned.”). Second, now the global threshold is overruled by the “local” threshold.

It would be great to have an option to use a global threshold which always applies to all files in addition to glob/path thresholds for single file thresholds.

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

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:16
  • Comments:16 (1 by maintainers)

github_iconTop GitHub Comments

8reactions
darekkaycommented, May 29, 2020

@averath Because what we need is to have both checks in place, e.g.:

a) The total coverage should be at least 90% b) The per-file coverage should be at least 50%

In other words, it’s okay if a single file does not meet the 90% coverage (as long as the overall coverage is at least 90%), but it’s not okay if a single file has no coverage at all.

3reactions
darekkaycommented, Jul 28, 2021

@antoniosZ I think the Coverage data for global was not found was a different issue. I could not reproduce it with my example anymore.

However, the main issue is still valid and reproducible both on Windows and a Mac. With the following configuration:

  coverageThreshold: {
    global: {
      statements: 80,
      branches: 80,
      functions: 80,
      lines: 80
    },

    "app/**/*.js": {
      statements: 50,
      branches: 50,
      functions: 50,
      lines: 50
    }
  },

… this example should fail:

------------|---------|----------|---------|---------|-------------------
File        | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
------------|---------|----------|---------|---------|-------------------
All files   |   91.67 |       50 |     100 |   91.67 |
 product.js |      75 |       50 |     100 |      75 | 6
 sum-01.js  |     100 |      100 |     100 |     100 |
 sum-02.js  |     100 |      100 |     100 |     100 |
 sum-03.js  |     100 |      100 |     100 |     100 |
 sum-04.js  |     100 |      100 |     100 |     100 |
------------|---------|----------|---------|---------|-------------------

Test Suites: 5 passed, 5 total
Tests:       5 passed, 5 total
Snapshots:   0 total
Time:        1.109 s
Ran all test suites.

Jest reports a successful result, even though the overall branch coverage does not fulfill the global threshold (80%). As discussed, the reason is that Jest uses global for “all files that are unmatched by other threshold patterns” (in this example, there are no other files). In other words, global rather means everythingElse or unmatched.

Read more comments on GitHub >

github_iconTop Results From Across the Web

reactjs - Global vs single file coverage thresholds when single ...
I am using jest for testing my react components and using HUSKY for pre-commit checks. I'm have a global threshold coverage with below...
Read more >
Configuring code coverage in Jest, the right way
In this brief tutorial we see how to configure code coverage for Jest, ... Jest: "global" coverage threshold for lines (90%) not met:...
Read more >
7.1 Setting Coverage Thresholds - Code with Hugo
7.1 Setting Coverage Thresholds Coverage thresholds allow you to define a percentage under which you jest --coverage run will start failing.
Read more >
Daily Coding Tips №5— Jest Code Coverage Threshold
The above settings is for global. Also, you can set up particular threshold for any particular file path in your code too, such...
Read more >
jest-coverage-thresholds-bumper - npm package - Snyk
A: Imagine that both real and expected coverage are at 90 percent and the margin is 1 percent. If you add a tiny...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found