X Tutup
The Wayback Machine - https://web.archive.org/web/20231224182317/https://github.com/nodejs/node/pull/51182
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test_runner: added coverage threshold support for tests #51182

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

pulkit-30
Copy link
Contributor

@pulkit-30 pulkit-30 commented Dec 16, 2023

fixes #48739

Added support for coverage threshold comparison.

  1. pass coverage threshold value via cli.
  2. if threshold didn;t match then log it.

example:

code:

import assert from "node:assert";
import test from "node:test";

test("run test", () => {
    assert.strictEqual(1,1)
})

output:

cmd: ./node --experimental-test-coverage=110 index.mjs

✔ run test (4.236583ms)
ℹ tests 1
ℹ suites 0
ℹ pass 1
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 32.217125
ℹ start of coverage report
ℹ ----------------------------------------------------------
ℹ file      | line % | branch % | funcs % | uncovered lines
ℹ ----------------------------------------------------------
ℹ index.mjs | 100.00 |   100.00 |  100.00 | 
ℹ ----------------------------------------------------------
ℹ all files | 100.00 |   100.00 |  100.00 |
ℹ ----------------------------------------------------------
ℹ coverage threshold for lines not met
ℹ end of coverage report

code:

import { spec } from 'node:test/reporters';
import { run } from 'node:test';
import process from 'node:process';

run({ files: ['./index.test.mjs'], coverage: {
    lines: 100,
    branches: 100,
    functions: 100,
} })
  .compose(spec)
  .pipe(process.stdout);

output:

✔ hello test (10.640833ms)
ℹ tests 1
ℹ suites 0
ℹ pass 1
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 306.317042
ℹ start of coverage report
ℹ ---------------------------------------------------------------
ℹ file           | line % | branch % | funcs % | uncovered lines
ℹ ---------------------------------------------------------------
ℹ index.test.mjs | 100.00 |   100.00 |  100.00 | 
ℹ ---------------------------------------------------------------
ℹ all files      | 100.00 |   100.00 |  100.00 |
ℹ ---------------------------------------------------------------
ℹ end of coverage report

@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/test_runner

@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. test_runner labels Dec 16, 2023
@pulkit-30 pulkit-30 marked this pull request as ready for review December 17, 2023 07:34
@meyfa
Copy link
Contributor

meyfa commented Dec 17, 2023

I think this should also set the exit code to a nonzero value such that CI systems will mark the job as failed.

Copy link
Member

@ljharb ljharb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree on the exit code; also, coverage always has 4 metrics, lines, functions, branches, and statements.

lib/internal/test_runner/harness.js Outdated Show resolved Hide resolved
@pulkit-30
Copy link
Contributor Author

I think this should also set the exit code to a nonzero value such that CI systems will mark the job as failed.
I agree on the exit code;

working on it 👍🏼

@pulkit-30 pulkit-30 force-pushed the fix-48739 branch 2 times, most recently from bfcfe72 to 52100c7 Compare December 19, 2023 13:07
Comment on lines 765 to 769
const failedCoverage = ArrayPrototypeFilter(
ObjectKeys(coverage.threshold), (key) => !coverage.threshold[key]);
if (failedCoverage.length > 0) {
console.error(`test coverage failed for ${ArrayPrototypeJoin(failedCoverage, ', ')}`);
process.exit(1);
Copy link
Contributor Author

@pulkit-30 pulkit-30 Dec 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here I added the logic to exit the test when coverage threshold doesn't match.

running test will be exited with status code 1.

output:

 hello test (0.64525ms)
 sub test (0.078625ms)
 tests 2
 suites 0
 pass 2
 fail 0
 cancelled 0
 skipped 0
 todo 0
 duration_ms 6.242459
 start of coverage report
 ---------------------------------------------------------------
 file           | line % | branch % | funcs % | uncovered lines
 ---------------------------------------------------------------
 index.test.mjs | 100.00 |   100.00 |  100.00 |
 sub/index.mjs  | 100.00 |   100.00 |  100.00 |
 sum/index.mjs  | 100.00 |   100.00 |  100.00 |
 ---------------------------------------------------------------
 all files      | 100.00 |   100.00 |  100.00 |
 ---------------------------------------------------------------
 minimum coverage failed for line. expected: 101 | actual: 100
 minimum coverage failed for function. expected: 101 | actual: 100
 minimum coverage failed for branch. expected: 101 | actual: 100
 end of coverage report

cc @ljharb @meyfa

@ljharb
Copy link
Member

ljharb commented Dec 19, 2023

It’s still missing “statements”.

@pulkit-30
Copy link
Contributor Author

pulkit-30 commented Dec 19, 2023

It’s still missing “statements”.

I think we don't collect statement coverage summary.
https://github.com/nodejs/node/blob/main/lib/internal/test_runner/coverage.js#L241-L263

@MoLow please confirm about statement summary here and how can we support that.

@@ -500,3 +500,42 @@ describe('require(\'node:test\').run', { concurrency: true }, () => {
for await (const _ of stream);
});
});

describe('coverage report for test stream', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have 3 tests here with the same name, please rename

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my bad 😞.
fixed now 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-ci PRs that need a full CI run. test_runner
Projects
None yet
Development

Successfully merging this pull request may close these issues.

New CLI flag to exit with an error status if test coverage is incomplete
5 participants
X Tutup