Skip to content

Commit

Permalink
test,doc: document where common modules go
Browse files Browse the repository at this point in the history
Keep the `require('../common')` separate from other common modules, as
it's the only line that must be there.

PR-URL: #16089
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
gibfahn committed Dec 20, 2017
1 parent b413e09 commit 7e571ae
Showing 1 changed file with 29 additions and 24 deletions.
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

0 comments on commit 7e571ae

Please sign in to comment.