You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/guides/writing-tests.md
+31-30Lines changed: 31 additions & 30 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -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 http = require('http');
86
87
constassert=require('assert');
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
0 commit comments