@@ -12,15 +12,15 @@ with code `0` on success. A test will fail if:
12
12
- It never exits. In this case, the test runner will terminate the test because
13
13
it sets a maximum time limit.
14
14
15
- Tests can be added for multiple reasons :
15
+ Add tests when :
16
16
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.
20
20
21
21
## Test structure
22
22
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:
24
24
25
25
``` javascript
26
26
1 ' use strict' ;
@@ -59,11 +59,12 @@ the nature of the test requires that the test run without it.
59
59
The second line loads the ` common ` module. The ` common ` module is a helper
60
60
module that provides useful tools for the tests.
61
61
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:
67
68
68
69
``` javascript
69
70
require (' ../common' );
@@ -86,49 +87,49 @@ const assert = require('assert');
86
87
const http = require (' http' );
87
88
```
88
89
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
94
95
[ ASCII] ( http://man7.org/linux/man-pages/man7/ascii.7.html ) order (digits, upper
95
96
case, ` _ ` , lower case).
96
97
97
98
### ** Lines 10-21**
98
99
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
100
101
HTTP server accepts ` non-ASCII ` characters in the headers of an incoming
101
102
request. Interesting things to notice:
102
103
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.
108
109
- The use of ` common.mustCall ` to check that some callbacks/listeners are
109
110
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
111
112
exit gracefully. Remember that for a test to succeed, it must exit with a
112
113
status code of 0.
113
114
114
115
## General recommendations
115
116
116
117
### Timers
117
118
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
120
121
explanation go [ here] ( https://github.com/nodejs/testing/issues/27 ) .
121
122
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
124
125
depending on the platform. For example:
125
126
126
127
``` javascript
127
128
const timer = setTimeout (fail, common .platformTimeout (4000 ));
128
129
```
129
130
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 .
132
133
133
134
### The * common* API
134
135
@@ -193,9 +194,9 @@ var server = http.createServer(common.mustCall(function(req, res) {
193
194
### Flags
194
195
195
196
Some 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
197
198
test 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.
199
200
A test that would require ` internal/freelist ` could start like this:
200
201
201
202
``` javascript
0 commit comments