jest
jest copied to clipboard
How to set coverageThreshold with CLI
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.
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?
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
for package.json you need to escape
"test:coverage": "jest --coverage --coverageThreshold='{\"global\":{\"statements\":\"80\"}}'",
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
--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>)
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}}"
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.
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?
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,
},
},
Can we please just get --coverageThresholdPercentage=100
for 100% on everything for more fine grained --coverageThresholdPercentage=100,100,90,80
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.
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.
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.