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: exclude test files from code coverage by default #55633

Closed
wants to merge 1 commit into from

Conversation

Llorx
Copy link

@Llorx Llorx commented Nov 1, 2024

Is not usual to test the code coverage of test files.

To include test files in code coverage, they must be explicitly included by using coverageIncludeGlobs or --test-coverage-include.

This way, your default code coverage will not include the test files, so you can work out your include/exclude filters based on the first coverage output. I didn't like the idea of suddenly including all the test files just by adding an exclude flag to exclude that_util_file.js that you don't want to test.

What could happen is that you could set an include filter just to test one file, and then you notice that the test file is also being tested. Maybe we can add an extra flag --include-test-files or something like that, so the test files are completely invisible to the user until the user wants to explicitly include them. Although I think that this may be too granular and with the --test-coverage-include/exclude parameters is enough.

Fixes: #53508

@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 Nov 1, 2024
@Llorx
Copy link
Author

Llorx commented Nov 1, 2024

I guess that this is a notable-change, but I can't add labels.

Copy link
Member

@avivkeller avivkeller left a comment

Choose a reason for hiding this comment

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

Good start!

lib/internal/test_runner/test.js Outdated Show resolved Hide resolved
lib/internal/test_runner/coverage.js Show resolved Hide resolved
lib/internal/test_runner/coverage.js Outdated Show resolved Hide resolved
lib/internal/test_runner/coverage.js Outdated Show resolved Hide resolved
lib/internal/test_runner/runner.js Outdated Show resolved Hide resolved
@avivkeller avivkeller added notable-change PRs with changes that should be highlighted in changelogs. experimental Issues and PRs related to experimental features. needs-citgm PRs that need a CITGM CI run. coverage Issues and PRs related to native coverage support. labels Nov 1, 2024
Copy link
Contributor

github-actions bot commented Nov 1, 2024

The notable-change PRs with changes that should be highlighted in changelogs. label has been added by @redyetidev.

Please suggest a text for the release notes if you'd like to include a more detailed summary, then proceed to update the PR description with the text or a link to the notable change suggested text comment. Otherwise, the commit will be placed in the Other Notable Changes section.

lib/internal/test_runner/test.js Outdated Show resolved Hide resolved
doc/api/cli.md Outdated Show resolved Hide resolved
doc/api/test.md Outdated Show resolved Hide resolved
lib/internal/test_runner/coverage.js Show resolved Hide resolved
lib/internal/test_runner/test.js Outdated Show resolved Hide resolved
@avivkeller
Copy link
Member

I've added needs-citgm in case anything is relying on the test files being covered, members: feel free to remove if needed.


This is breaking change, but since code coverage is experimental, it is not semver-major

Copy link
Contributor

@cjihrig cjihrig left a comment

Choose a reason for hiding this comment

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

I'm not sure that this is the right approach. Since we already have coverage excluding functionality, this should probably be more integrated with that. I think that would lead to less invasive changes than this PR currently has.

Another thing that is not clear to me: what happens if someone runs a test file, but that test file imports another file that also includes tests? Should we exclude that imported file from the coverage report, or should we only exclude the files that the user specified?

lib/internal/test_runner/test.js Outdated Show resolved Hide resolved
lib/internal/test_runner/coverage.js Outdated Show resolved Hide resolved
@Llorx
Copy link
Author

Llorx commented Nov 1, 2024

I'm not sure that this is the right approach. Since we already have coverage excluding functionality, this should probably be more integrated with that. I think that would lead to less invasive changes than this PR currently has.

The problem with that is that if I set the "coverage exclude" based on the "test include", if someone sets an exclude, it is going to include all test files again, and is pretty usual to set a simple exclude for a utils file or some small things.

Another thing that is not clear to me: what happens if someone runs a test file, but that test file imports another file that also includes tests? Should we exclude that imported file from the coverage report, or should we only exclude the files that the user specified?

Very good question and I did not think about that. I would say to exclude that file too by default, but then I have to find another approach.

@avivkeller
Copy link
Member

The problem with that is that if I set the "coverage exclude" based on the "test include", if someone sets an exclude, it is going to include all test files again, and is pretty usual to set a simple exclude for a utils file or some small things.

I second this opinion. Regardless of the approach, IMO it should follow the same way that node_modules/ files are excluding, that is, exclude after flags, not with.

@Llorx
Copy link
Author

Llorx commented Nov 1, 2024

Everything updated as stated. It is cleaner now :-)

One thing I can't resolve easily is a test file that requires another test file which is not directly included in the test globs. I do not have an unhacky way to track this (maybe checking the call stack when the test method is called? I don't like it...). Still, the documentation is pretty clear about the "inluded/excluded test files" now. I think that with this feature (at least in the first version with the known limitation) we have to aim for the majority of the userland, which is usually one test file for a batch of tests.

EDIT: There are more file changes because of merging again from master to this PR, as the test_runner was modified while this PR was discussed and it conflicted with the changes... :-/

Copy link
Member

@avivkeller avivkeller left a comment

Choose a reason for hiding this comment

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

Better! just a few more concerns

lib/internal/test_runner/runner.js Outdated Show resolved Hide resolved
lib/internal/test_runner/runner.js Outdated Show resolved Hide resolved
lib/internal/test_runner/test.js Outdated Show resolved Hide resolved
test/parallel/test-runner-coverage-source-map.js Outdated Show resolved Hide resolved
test/parallel/test-runner-run-coverage.mjs Show resolved Hide resolved
@avivkeller
Copy link
Member

Additionally: Can you rebase the merge commits, the github bot doesn't like them.

@avivkeller
Copy link
Member

One thing I can't resolve easily is a test file that requires another test file which is not directly included in the test globs.

In my opinion, this feature isn't really needed, at least in this PR. If that's something that is chosen to be added, I believe it should be its own discussion. For now, we can safely say that the test files themselves are not counted for code coverage. Nice and simple.

In the future, this change might be preferable, but for now I don't feel strongly that it's needed.

However, if someone feels strongly for it, a theoretical solution would be to add any files that import node:test and run a test to the files list, and this should cover imports as well. But still, IMO a change like that deserves its own PR/discussion.

@Llorx
Copy link
Author

Llorx commented Nov 2, 2024

Additionally: Can you rebase the merge commits, the github bot doesn't like them.

Sure!

EDIT: Done. Way cleaner now, yep.

@Llorx Llorx force-pushed the coverage-exclude-test-files branch 2 times, most recently from cc09908 to c5c8ea7 Compare November 2, 2024 11:02
@Llorx
Copy link
Author

Llorx commented Nov 2, 2024

Done. Also added the cli entrypoint as a testFile to follow the same logic (and edited the affected tests as the behaviour changed)

@avivkeller
Copy link
Member

avivkeller commented Nov 2, 2024

The implementation LGTM :-)

I've removed needs-CITGM, as this is an experimental feature

@avivkeller avivkeller removed the needs-citgm PRs that need a CITGM CI run. label Nov 2, 2024
Is not usual to test the code coverage of test files.

Fixes: nodejs#53508
@Llorx Llorx force-pushed the coverage-exclude-test-files branch from 9c6e862 to 2df9e6e Compare November 2, 2024 12:10
Comment on lines +2355 to +2357
By default, the files being tested are excluded from code coverage. They can be explicitly
included via this flag.

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
By default, the files being tested are excluded from code coverage. They can be explicitly
included via this flag.
By default, the test files are excluded from code coverage. They can be explicitly
included via this flag.

@@ -1359,6 +1359,8 @@ changes:
This property is only applicable when `coverage` was set to `true`.
If both `coverageExcludeGlobs` and `coverageIncludeGlobs` are provided,
files must meet **both** criteria to be included in the coverage report.
By default, the files being tested are excluded from code coverage. They can be explicitly
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
By default, the files being tested are excluded from code coverage. They can be explicitly
By default, the test files are excluded from code coverage. They can be explicitly

@@ -61,6 +61,8 @@ for (const coverage of coverages) {
const result = spawnSync(process.execPath, [
'--test',
'--experimental-test-coverage',
'--no-warnings',
'--test-coverage-include=**',
Copy link
Member

Choose a reason for hiding this comment

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

I have mixed feelings about all these --test-coverage-include=** flags.
IMHO, since we're changing the default behaviour, it might make sense to avoid a large number of tests focused on a 'non-default' behaviour.
Although we could address this in a separate PR, I think it would be better, if it makes sense to you, to handle it here to avoid a "test-refactor only" PR later.

WDYT?

cc @nodejs/test_runner @redyetidev

Copy link
Member

@avivkeller avivkeller Nov 4, 2024

Choose a reason for hiding this comment

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

I also have mixed feelings about it, as ** would also include anything in node_modules. I'm honestly okay with keeping the test files in the coverage report, and having the user exclude them manually with the exclude flag.

Copy link
Author

@Llorx Llorx Nov 4, 2024

Choose a reason for hiding this comment

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

--test-coverage-include=** was a quick update here as for testing I prefer to update the arrange/act to behave exactly equal as before the update, than modify the assert to handle a different result than before. In this case easier to understand the arrange/act and update it accordingly so it behaves equally as before, than understanding the internals and what result it should output to be correct.

The thing with the filters is that I can't think of any reason a user would want the test files in the coverage report, so we are going to force all the developers to handle a dirty output that they don't actually want, or force them to use the --test-coverage-include flag, but either way is not comfortable. I always liked the premise of nodejs and how convenient it is without too much hassle, not like other languages/frameworks. It usually aims for the majority of user needs, so it is not usual to find yourself fiddling too much as there's high chance that the defaults fit your needs. In the case of the code coverage is the contrary right now.

Copy link
Member

@avivkeller avivkeller Nov 4, 2024

Choose a reason for hiding this comment

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

In the Node.js test files, the ** is fine.

I've self-addressed my concerns:
My concern was that users that want to include test files would use --test-coverage-include=**, which will also include node_modules/, which they may not want. I suppose it's not that bad for users who, for some reason, need the test files (and only the test files) to use --test-coverage-include=** --test-coverage-exclude=node_modules/.

Copy link
Author

@Llorx Llorx Nov 4, 2024

Choose a reason for hiding this comment

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

Ahhh I see, that's true, and that's not comfortable neither... I think that the more we are talking, the more this is heading to have an exclusive --coverage-include-test-files flag (or similar) that is disabled by default... Makes sense as right now test files is an exclusive option named "testFiles" in the TestCoverage class not linked to the include or exclude in any direct way, but just when applying the filters.

Copy link
Member

Choose a reason for hiding this comment

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

Maybe, but that could be done in a followup if users show that they need it. For now, maybe this is okay?

Copy link
Author

Choose a reason for hiding this comment

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

Well, for me it is okay obviosuly because it fits my needs haha. We can see how people reacts to this. This is my first PR so I don't know how this works. This first goes to the non LTS and when people complain/debate/whatever it goes to the LTS if okay, right?

Copy link
Member

Choose a reason for hiding this comment

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

This first goes to the non LTS and when people complain/debate/whatever it goes to the LTS if okay, right?

Yes, that's how it works. I'm happy to see where this goes.

@MoLow
Copy link
Member

MoLow commented Nov 4, 2024

+1 on @cjihrig concerns.

Copy link

codecov bot commented Nov 18, 2024

Codecov Report

Attention: Patch coverage is 88.88889% with 1 line in your changes missing coverage. Please review.

Project coverage is 87.90%. Comparing base (d37214b) to head (2df9e6e).
Report is 325 commits behind head on main.

Files with missing lines Patch % Lines
lib/internal/test_runner/harness.js 50.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #55633      +/-   ##
==========================================
- Coverage   88.43%   87.90%   -0.54%     
==========================================
  Files         654      654              
  Lines      187720   187724       +4     
  Branches    36145    35822     -323     
==========================================
- Hits       166015   165018     -997     
- Misses      14949    15888     +939     
- Partials     6756     6818      +62     
Files with missing lines Coverage Δ
lib/internal/test_runner/coverage.js 64.45% <100.00%> (+0.10%) ⬆️
lib/internal/test_runner/runner.js 89.06% <100.00%> (+0.13%) ⬆️
lib/internal/test_runner/harness.js 92.41% <50.00%> (-0.27%) ⬇️

... and 96 files with indirect coverage changes

@cjihrig
Copy link
Contributor

cjihrig commented Dec 17, 2024

It looks like this has been superseded by #56060, so I'll close this out. Thanks for the PR though.

@cjihrig cjihrig closed this Dec 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
coverage Issues and PRs related to native coverage support. experimental Issues and PRs related to experimental features. needs-ci PRs that need a full CI run. notable-change PRs with changes that should be highlighted in changelogs. test_runner Issues and PRs related to the test runner subsystem.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Exclude test files from coverage report
6 participants