Skip to content

Commit

Permalink
doc: recommend writing tests in new files and including comments
Browse files Browse the repository at this point in the history
The previous phrasing encouraged or did not discourage appending
new test cases to existing files - a practice that can reduce
the debuggability of the tests over time as they get bigger and
bigger, some times thousands of lines long with hundreds of
test cases, and make the CI output increasingly difficult to
read when one of the test cases fail in a very long test.

This patch updates the guideline to explicitly discourage appending
test cases this way. Also recommend including an opening comment
to describe what the test does to optimize the test towards the
scenario when it fails.

PR-URL: #57028
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Richard Lau <[email protected]>
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: Ulises Gascón <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Jake Yuesong Li <[email protected]>
Reviewed-By: Chengzhong Wu <[email protected]>
Reviewed-By: Tierney Cyren <[email protected]>
  • Loading branch information
joyeecheung authored and targos committed Feb 17, 2025
1 parent 5af35d1 commit 50ba04e
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions doc/contributing/writing-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,22 @@ Add tests when:
## Test directory structure

See [directory structure overview][] for outline of existing test and locations.
When deciding on whether to expand an existing test file or create a new one,
consider going through the files related to the subsystem.
For example, look for `test-streams` when writing a test for `lib/streams.js`.

## How to write a good test

A good test should be written in a style that is optimial for debugging
when it fails.

In principle, when adding a new test, it should be placed in a new file.
Unless there is strong motivation to do so, refrain from appending
new test cases to an existing file. Similar to the reproductions we ask
for in the issue tracker, a good test should be as minimal and isolated as
possible to facilitate debugging.

A good test should come with comments explaining what it tries to test,
so that when it fails, other contributors can fix it with the full context
of its intention, and be able to modify it in good confidence when the context
changes.

## Test structure

Expand Down

0 comments on commit 50ba04e

Please sign in to comment.