From b6929e2aa99238d07f75eb32b361bc2d6ab7bb41 Mon Sep 17 00:00:00 2001 From: Gibson Fahnestock Date: Sun, 8 Oct 2017 10:26:55 -0700 Subject: [PATCH] test,doc: document where common modules go Keep the `require('../common')` separate from other common modules, as it's the only line that must be there. PR-URL: https://github.com/nodejs/node/pull/16089 Reviewed-By: Luigi Pinca Reviewed-By: Ruben Bridgewater Reviewed-By: James M Snell --- doc/guides/writing-tests.md | 53 ++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 24 deletions(-) diff --git a/doc/guides/writing-tests.md b/doc/guides/writing-tests.md index 19626799f73acb..7dc86facfd00c5 100644 --- a/doc/guides/writing-tests.md +++ b/doc/guides/writing-tests.md @@ -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 @@ -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 @@ -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'); @@ -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