jest icon indicating copy to clipboard operation
jest copied to clipboard

How to set coverageThreshold with CLI

Open robinhe opened this issue 6 years ago • 10 comments

As the code, https://github.com/facebook/jest/blob/3e805bc082c8da90c33a4dff9d3e1b0804a4386d/packages/jest-cli/src/cli/args.js#L211

The argument should be a JSON string, but the below command always doesn't work.

 jest --coverage --coverageThreshold='{\"global\":{\"branches\":90,\"functions\":90,\"lines\":90,\"statements\":90}'

It should be failed since it does not reach the target. However, it is always successful. image

robinhe avatar Dec 13 '18 14:12 robinhe

Hey, I actually don't see this in the docs. https://jestjs.io/docs/en/cli.html#options Perhaps cli docs are out of date?

natealcedo avatar Dec 21 '18 14:12 natealcedo

Funnily enough if there is coverageThreshold defined in the jest.config.js and then one passes through --coverageThreshold={} it ignores the coverageThreshold in the config file. So it does do something.

Edit: Ahh I did manage to set it via --coverageThreshold='{"global":{"statements":"20"}}' - no spaces where very important

Globegitter avatar May 20 '19 07:05 Globegitter

for package.json you need to escape

    "test:coverage": "jest --coverage --coverageThreshold='{\"global\":{\"statements\":\"80\"}}'",

ctappy avatar Jul 22 '20 22:07 ctappy

For not-100% exceptions I'm taking like that:

{
"scripts": {
"test/%subproject%"
: "jest --selectProjects %subproject% --coverageThreshold=\"'$(cat %subproject%.coverage.json)'\""
}}

JSON has indents and newlines, no problem

askirmas avatar Sep 10 '20 05:09 askirmas

--coverageThreshold='{"global":{"statements":"80"}}'"

have you had the problem where this is treated as an invalid json object before?

It works on Mac, but on windows the following error appears:

> jest --coverage --collectCoverageFrom=*.ts --collectCoverageFrom=*/**/*.ts --coverageThreshold='{"global":{"statements":"85"}}'

SyntaxError: Unexpected token g in JSON at position 1
    at JSON.parse (<anonymous>)

victor-z-xiong avatar Nov 02 '20 00:11 victor-z-xiong

On Windows I had to escape \

"test:coverage": "jest --coverage --changedSince=master --coverageThreshold=\"{\\\"global\\\":{\\\"statements\\\":80,\\\"branches\\\":80}}\"",

which results

jest --coverage --changedSince=master --coverageThreshold="{\"global\":{\"statements\":80,\"branches\":80}}"

nikolay-borzov avatar Nov 03 '21 13:11 nikolay-borzov

This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Nov 03 '22 13:11 github-actions[bot]

Putting the JSON object in the CLI parameter is a good workaround but makes the CLI usability a bit cumbersome and ugly. I'm migrating from mocha and this is the only thing that is worse here...

Any chance to move this forward?

Thiamath avatar Nov 26 '22 20:11 Thiamath

I made this solution for my project, to only push with husky when I have minimum coverage.

Package.json

"scripts": {
  "test": "jest --clearCache && jest --passWithNoTests --no-cache --runInBand --coverageThreshold=\"{}\"",
  "test:push": "jest --clearCache && jest --passWithNoTests --no-cache --runInBand"
},

Jest.config.ts:

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

joaoazevedoJS avatar Jan 02 '23 17:01 joaoazevedoJS

Can we please just get --coverageThresholdPercentage=100 for 100% on everything for more fine grained --coverageThresholdPercentage=100,100,90,80

Shereef avatar Jan 26 '23 04:01 Shereef

This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Jan 26 '24 05:01 github-actions[bot]

This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

github-actions[bot] avatar Feb 25 '24 05:02 github-actions[bot]

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[bot] avatar Mar 28 '24 00:03 github-actions[bot]