@@ -12,15 +12,15 @@ with code `0` on success. A test will fail if:
1212- It never exits. In this case, the test runner will terminate the test because
1313 it sets a maximum time limit.
1414
15- Tests can be added for multiple reasons :
15+ Add tests when :
1616
17- - When adding new functionality.
18- - When fixing regressions and bugs.
19- - When expanding test coverage.
17+ - Adding new functionality.
18+ - Fixing regressions and bugs.
19+ - Expanding test coverage.
2020
2121## Test structure
2222
23- Let's analyze this very basic test from the Node.js test suite:
23+ Let's analyze this basic test from the Node.js test suite:
2424
2525``` javascript
26261 ' use strict' ;
@@ -59,11 +59,12 @@ the nature of the test requires that the test run without it.
5959The second line loads the ` common ` module. The ` common ` module is a helper
6060module that provides useful tools for the tests.
6161
62- Even if no functions or other properties exported by ` common ` are used in a
63- test, the ` common ` module should still be included. This is because the ` common `
64- module includes code that will cause tests to fail if variables are leaked into
65- the global space. In situations where no functions or other properties exported
66- by ` common ` are used, it can be included without assigning it to an identifier:
62+ Even if a test uses no functions or other properties exported by ` common ` ,
63+ the test should still include the ` common ` module before any other modules. This
64+ is because the ` common ` module includes code that will cause a test to fail if
65+ the test leaks variables into the global space. In situations where a test uses
66+ no functions or other properties exported by ` common ` , include it without
67+ assigning it to an identifier:
6768
6869``` javascript
6970require (' ../common' );
@@ -86,49 +87,49 @@ const http = require('http');
8687const assert = require (' assert' );
8788```
8889
89- These modules are required for the test to run. Except for special cases, these
90- modules should only include core modules.
91- The ` assert ` module is used by most of the tests to check that the assumptions
92- for the test are met.
93- Note that require statements are sorted, in
90+ The test checks functionality in the ` http ` module.
91+
92+ Most tests use the ` assert ` module to confirm expectations of the test.
93+
94+ The require statements are sorted in
9495[ ASCII] ( http://man7.org/linux/man-pages/man7/ascii.7.html ) order (digits, upper
9596case, ` _ ` , lower case).
9697
9798** Lines 10-21**
9899
99- This is the body of the test. This test is quite simple, it just tests that an
100+ This is the body of the test. This test is simple, it just tests that an
100101HTTP server accepts ` non-ASCII ` characters in the headers of an incoming
101102request. Interesting things to notice:
102103
103- - If the test doesn't depend on a specific port number then always use 0 instead
104- of an arbitrary value, as it allows tests to be run in parallel safely, as the
105- operating system will assign a random port. If the test requires a specific
106- port, for example if the test checks that assigning a specific port works as
107- expected, then it is ok to assign a specific port number.
104+ - If the test doesn't depend on a specific port number, then always use 0
105+ instead of an arbitrary value, as it allows tests to run in parallel safely,
106+ as the operating system will assign a random port. If the test requires a
107+ specific port, for example if the test checks that assigning a specific port
108+ works as expected, then it is ok to assign a specific port number.
108109- The use of ` common.mustCall ` to check that some callbacks/listeners are
109110 called.
110- - The HTTP server is closed once all the checks have run. This way, the test can
111+ - The HTTP server closes once all the checks have run. This way, the test can
111112 exit gracefully. Remember that for a test to succeed, it must exit with a
112113 status code of 0.
113114
114115## General recommendations
115116
116117### Timers
117118
118- The use of timers is discouraged, unless timers are being tested . There are
119- multiple reasons for this. Mainly, they are a source of flakiness. For a thorough
119+ Avoid timers unless the test is specifically testing timers . There are multiple
120+ reasons for this. Mainly, they are a source of flakiness. For a thorough
120121explanation go [ here] ( https://github.com/nodejs/testing/issues/27 ) .
121122
122- In the event a timer is needed, it's recommended using the
123- ` common.platformTimeout() ` method, that allows setting specific timeouts
123+ In the event a test needs a timer, consider using the
124+ ` common.platformTimeout() ` method. It allows setting specific timeouts
124125depending on the platform. For example:
125126
126127``` javascript
127128const timer = setTimeout (fail, common .platformTimeout (4000 ));
128129```
129130
130- will create a 4-seconds timeout, except for some platforms where the delay will
131- be multiplied for some factor .
131+ will create a 4-second timeout on most platforms but a longer timeout on slower
132+ platforms .
132133
133134### The * common* API
134135
@@ -193,9 +194,9 @@ var server = http.createServer(common.mustCall(function(req, res) {
193194### Flags
194195
195196Some tests will require running Node.js with specific command line flags set. To
196- accomplish this, a ` // Flags: ` comment should be added in the preamble of the
197+ accomplish this, add a ` // Flags: ` comment in the preamble of the
197198test followed by the flags. For example, to allow a test to require some of the
198- ` internal/* ` modules, the ` --expose-internals ` flag should be added .
199+ ` internal/* ` modules, add the ` --expose-internals ` flag.
199200A test that would require ` internal/freelist ` could start like this:
200201
201202``` javascript
0 commit comments