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

Closed
wants to merge 1 commit into 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 { test } from 'node:test';
import assert from 'node:assert';

test('running test', {}, () => {
    assert.strictEqual(1, 1);
});

output:

cmd: ./node --experimental-test-coverage --experimental-minimal-test-coverage 80 index.mjs

✔ running test (0.998708ms)
ℹ tests 1
ℹ suites 0
ℹ pass 1
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 8.136042
ℹ start of coverage report
ℹ ---------------------------------------------------------------
ℹ file           | line % | branch % | funcs % | uncovered lines
ℹ ---------------------------------------------------------------
ℹ index.test.mjs |  77.78 |    66.67 |  100.00 | 8-9
ℹ ---------------------------------------------------------------
ℹ all files      |  77.78 |    66.67 |  100.00 |
ℹ ---------------------------------------------------------------
ℹ minimum coverage failed for line. expected: 80% | actual: 77.78%
ℹ minimum coverage failed for branch. expected: 80% | actual: 66.67%
ℹ end of coverage report

code:

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

run({
  files: [path.resolve('./index.test.mjs')],
  coverage: true,
  minimumCoverage: {
    branch: 80,
    function: 80,
    line: 80,
  },
})
  .compose(spec)
  .pipe(process.stdout);

output:

✔ running test (1.034708ms)
ℹ tests 1
ℹ suites 0
ℹ pass 1
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 90.49975
ℹ start of coverage report
ℹ ---------------------------------------------------------------
ℹ file           | line % | branch % | funcs % | uncovered lines
ℹ ---------------------------------------------------------------
ℹ index.test.mjs |  77.78 |    66.67 |  100.00 | 8-9
ℹ ---------------------------------------------------------------
ℹ all files      |  77.78 |    66.67 |  100.00 |
ℹ ---------------------------------------------------------------
ℹ minimum coverage failed for line. expected: 80% | actual: 77.78%
ℹ minimum coverage failed for branch. expected: 80% | actual: 66.67%
ℹ 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 Issues and PRs related to the test runner subsystem. 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
@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.

@pulkit-30
Copy link
Contributor Author

PTAL @nodejs/test_runner

Copy link
Member

@atlowChemi atlowChemi left a comment

Choose a reason for hiding this comment

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

Overall LGTM, should add documentation though

src/node_options.cc Outdated Show resolved Hide resolved
@pulkit-30
Copy link
Contributor Author

Overall LGTM, should add documentation though

Thanks for review 🙌🏼 . will add documentation.

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

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

@atlowChemi Any idea about this?

Copy link
Member

@tniessen tniessen left a comment

Choose a reason for hiding this comment

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

There's a reason we don't use such a threshold in this repository -- it tends to be a fragile indicator.

On a side note, the first commit message does not adhere to our requirements and must be amended.

doc/api/cli.md Outdated Show resolved Hide resolved
doc/api/cli.md Outdated Show resolved Hide resolved
doc/api/cli.md Outdated
Comment on lines 826 to 828
To Specify the minimum percentage threshold value for code coverage.
Used with `--experimental-test-coverage` flag. If the minimum threshold
value is not met, the test will exit with status code 1.
Copy link
Member

Choose a reason for hiding this comment

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

The documentation doesn't say anything about what coverage metric is meant here.

Copy link
Member

Choose a reason for hiding this comment

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

Indeed; typically in code coverage there are 4 metrics (statements remains missing), and all 4 are independently able to be set to a threshold.

Copy link
Member

@benjamingr benjamingr left a comment

Choose a reason for hiding this comment

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

Really good work, I'm not convinced we need/want the feature but the code and everyting else looks solid <3

@pulkit-30
Copy link
Contributor Author

Really good work, I'm not convinced we need/want the feature but the code and everyting else looks solid <3

Thanks @benjamingr,
I see this feature request. I don't want to force the inclusion of this feature, but I'm curious if there's an alternative method to ensure 100% code coverage.

Additionally, it might be beneficial to consider adding a coverage option within the test_runner/run() function. WDYT @nodejs/test_runner

@tniessen
Copy link
Member

tniessen commented Jan 3, 2024

I'm curious if there's an alternative method to ensure 100% code coverage.

FWIW, Node.js could always emit coverage data in some standardized format that could then be consumed by a different tool (e.g., codecov or so).

Also, "100% code coverage" is a somewhat odd metric. It by no means guarantees the absence of bugs yet is almost impossible to achieve in many cases.

@MoLow
Copy link
Member

MoLow commented Jan 4, 2024

FWIW, Node.js could always emit coverage data in some standardized format that could then be consumed by a different tool (e.g., codecov or so).

node already does that https://nodejs.org/api/test.html#event-testcoverage

@cjihrig
Copy link
Contributor

cjihrig commented Aug 13, 2024

This appears to have stalled out and now has conflicts. I'm going to close this for now.

@cjihrig cjihrig closed this Aug 13, 2024
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 Issues and PRs related to the test runner subsystem.
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
10 participants