All jobs

Add Coverage Thresholds to a Jest Suite

Yes. An agent measures what your Jest suite covers, sets coverageThreshold in jest.config.js just under it, makes CI run with --coverage, and opens a PR.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

The agent starts from how coverage is already collected, because a threshold only fires on a run that measures it. In an Express API laid out like hagopj13/node-express-boilerplate, jest.config.js sets testEnvironment: 'node', restoreMocks: true, coveragePathIgnorePatterns for node_modules, src/config, src/app.js and tests, and four coverageReporters (text, lcov, clover, html), but no coverageThreshold. package.json pins jest at ^26.0.1, which yarn.lock resolves to 26.6.3, and has three ways to run it: test (jest -i --colors --verbose --detectOpenHandles), coverage (jest -i --coverage) and coverage:coveralls. .travis.yml starts a mongodb service, exports MONGODB_URL, JWT_SECRET and the token expiry variables, runs yarn lint and yarn test, and only sends coverage to Coveralls in after_success. So the number is measured on every build and enforced on none.

It then measures before it sets anything. The seven test files under tests/ are three Supertest integration suites (auth, user, docs) and four unit suites for the user model, the paginate and toJSON plugins and the error middleware; the integration ones call setupTestDB() from tests/utils/setupTestDB.js, which connects Mongoose to config.mongoose.url and clears every collection before each test. The agent starts a MongoDB in its sandbox, exports the same variables Travis does, runs yarn install --frozen-lockfile and yarn coverage, and reads the table Jest prints for src/controllers, src/middlewares, src/models, src/routes/v1, src/services, src/utils and src/validations, each with statements, branches, functions and lines.

The thresholds go in jest.config.js as coverageThreshold: { global: { statements, branches, functions, lines } }, each a few points under the figure it just measured, so the pull request itself passes and the next change that drops coverage fails. Branches get the most room, since they are the figure that moves most when a validation or an error path is added. It does not set per-path thresholds for directories whose numbers come from a handful of files, and it says so in the description. Jest 26 reports a miss as Jest: "global" coverage threshold for lines (…%) not met: …% and exits non-zero, but only when --coverage is on, so yarn test keeps its speed and yarn coverage becomes the check.

That is the last change: .travis.yml runs yarn coverage in place of yarn test, so the threshold is enforced where the Coveralls upload already reads the lcov file, and docker-compose.test.yml, whose node-app command is yarn test, is left as the local runner. The agent then raises one threshold above the measured figure for a single run, quotes the not met line and the exit code in the description, restores it, and runs yarn lint, since the .husky/pre-commit hook runs lint-staged with eslint on every staged .js file and jest.config.js is one.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "Add coverage thresholds to this Jest suite. Read jest.config.js, package.json, yarn.lock, .travis.yml, tests/utils/setupTestDB.js and docker-compose.test.yml first. Start a MongoDB, export the variables .travis.yml sets, run yarn coverage, and read the coverage table. Add coverageThreshold with global statements, branches, functions and lines to jest.config.js, each a few points under the measured figure, with the widest margin on branches. Change the Travis script to run yarn coverage instead of yarn test and leave after_success as it is. Do not change any test. Prove the threshold fires by raising one figure above the measurement for one run, quote the failure and the exit code, and restore it. Run yarn lint and open a pull request.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
  1. Get an API key. In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
  2. Send the request. POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
  3. Read what came back. Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

What comes back

What comes back is a pull request that changes two files: jest.config.js gains a coverageThreshold block with four global figures, and .travis.yml runs yarn coverage where it ran yarn test. No test is added or changed and no dependency moves, since Jest collects coverage itself; yarn.lock is untouched.

The description quotes the coverage table from the agent's run, the four thresholds beside the figures they were set from, and the one deliberate failure: the not met line Jest printed with a threshold raised above the measured value, and the exit code that followed. The run's record keeps both Jest runs in full, with the MongoDB connection the integration suites made.

Before merging, a reviewer decides how much margin to leave. The agent leaves a few points so the pull request passes on the day it opens; a team that wants coverage to only rise sets each figure to the measured value and raises it as tests land.

Run it on every merged pull request

Once the threshold is in CI, every pull request checks it, so the useful run on Rush is the one that moves it up. An automation with a github trigger on pull_request.closed.merged starts a run that executes yarn coverage on the merged branch, compares the table with the figures in jest.config.js, and opens a pull request raising any threshold the suite now clears by a margin, with the table in its description.

The trigger adds the merged pull request to the prompt after it; the prompt itself says what margin to keep. A run that finds nothing to raise opens no pull request, so the only ones a reviewer sees are the ratchets.

The use case this job follows, with its automation and trigger: Write the missing tests.

Why run it on Rush?

Setting a threshold without a measurement is a guess that fails on the next push, and measuring needs a MongoDB and the same environment variables CI exports. The agent runs the suite where it has both, reads the real table, and sets the figures from it; nobody has to install Mongo on a laptop to find out the number.

The run keeps the output of every command: the coverage table, the deliberate failure and the lint. A reviewer reads what Jest printed, not a description of it, and the same job runs across every repository a team keeps, one sandbox each, with each threshold set from its own suite.

Frequently asked questions

Will yarn test fail when coverage drops?

No. Jest checks coverageThreshold only on a run with --coverage, which is the coverage script. yarn test, the test:watch script and the Docker test compose file keep running without the check; CI is where it fires.

Why global thresholds and not one per directory?

Per-path thresholds need enough files behind each figure to be stable; a directory with two files swings by whole points on one function. The agent sets the four global figures and leaves per-path ones as a note, with the table that would justify them.

Does this need a MongoDB in CI?

It already does. The integration suites connect to MONGODB_URL, and .travis.yml starts a mongodb service for them. The coverage run uses the same service; only the script name changes.

What about Coveralls?

Unchanged. after_success still runs coverage:coveralls, which sends text-lcov output. The threshold fails the build earlier, so Coveralls only receives runs that cleared it.

More Jest jobs

The same job on other stacks

Keep reading