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,doc: document where common modules go #16089

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 29 additions & 24 deletions doc/guides/writing-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,39 +25,44 @@ Let's analyze this basic test from the Node.js test suite:
```javascript
'use strict'; // 1
const common = require('../common'); // 2

// This test ensures that the http-parser can handle UTF-8 characters // 4
// in the http header. // 5

const assert = require('assert'); // 7
const http = require('http'); // 8

const server = http.createServer(common.mustCall((req, res) => { // 10
res.end('ok'); // 11
})); // 12
server.listen(0, () => { // 13
http.get({ // 14
port: server.address().port, // 15
headers: { 'Test': 'Düsseldorf' } // 16
}, common.mustCall((res) => { // 17
assert.strictEqual(res.statusCode, 200); // 18
server.close(); // 19
})); // 20
}); // 21
const fixtures = require('../common/fixtures'); // 3

// This test ensures that the http-parser can handle UTF-8 characters // 5
// in the http header. // 6

const assert = require('assert'); // 8
const http = require('http'); // 9

const server = http.createServer(common.mustCall((req, res) => { // 11
res.end('ok'); // 12
})); // 13
server.listen(0, () => { // 14
http.get({ // 15
port: server.address().port, // 16
headers: { 'Test': 'Düsseldorf' } // 17
}, common.mustCall((res) => { // 18
assert.strictEqual(res.statusCode, 200); // 19
server.close(); // 20
})); // 21
}); // 22
// ... // 23
```

### **Lines 1-2**
### **Lines 1-3**

```javascript
'use strict';
const common = require('../common');
const fixtures = require('../common/fixtures');
```

The first line enables strict mode. All tests should be in strict mode unless
the nature of the test requires that the test run without it.

The second line loads the `common` module. The [`common` module][] is a helper
module that provides useful tools for the tests.
module that provides useful tools for the tests. Some common functionality has
been extracted into submodules, which are required separately like the fixtures
module here.

Even if a test uses no functions or other properties exported by `common`,
the test should still include the `common` module before any other modules. This
Expand All @@ -70,7 +75,7 @@ assigning it to an identifier:
require('../common');
```

### **Lines 4-5**
### **Lines 5-6**

```javascript
// This test ensures that the http-parser can handle UTF-8 characters
Expand All @@ -80,7 +85,7 @@ require('../common');
A test should start with a comment containing a brief description of what it is
designed to test.

### **Lines 7-8**
### **Lines 8-9**

```javascript
const assert = require('assert');
Expand All @@ -95,7 +100,7 @@ The require statements are sorted in
[ASCII][] order (digits, upper
case, `_`, lower case).

### **Lines 10-21**
### **Lines 11-22**

This is the body of the test. This test is simple, it just tests that an
HTTP server accepts `non-ASCII` characters in the headers of an incoming
Expand Down