Jest: How to set coverageThreshold with CLI

Created on 13 Dec 2018  路  5Comments  路  Source: facebook/jest

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

All 5 comments

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>)
Was this page helpful?
0 / 5 - 0 ratings