From 03f0084eacfde780961a21e2d0306d1c5f085646 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jimmy=20Wa=CC=88rting?= Date: Tue, 4 Jan 2022 21:50:30 +0100 Subject: [PATCH 1/2] refactor: convert var to const / let --- lib/test.js | 76 +++++++++++++++++++++++------------------------------ 1 file changed, 33 insertions(+), 43 deletions(-) diff --git a/lib/test.js b/lib/test.js index 3414b49..87c9491 100644 --- a/lib/test.js +++ b/lib/test.js @@ -4,12 +4,11 @@ * Module dependencies. */ -var request = require('superagent'); -var util = require('util'); -var http = require('http'); -var https = require('https'); -var assert = require('assert'); -var Request = request.Request; +const { inspect } = require('util'); +const { STATUS_CODES } = require('http'); +const { Server } = require('https'); +const assert = require('assert'); +const { Request } = require('superagent'); class Test extends Request { /** @@ -42,13 +41,11 @@ class Test extends Request { * @api private */ serverAddress(app, path) { - var addr = app.address(); - var port; - var protocol; + const addr = app.address(); if (!addr) this._server = app.listen(0); - port = app.address().port; - protocol = app instanceof https.Server ? 'https' : 'http'; + const port = app.address().port; + const protocol = app instanceof Server ? 'https' : 'http'; return protocol + '://127.0.0.1:' + port + path; } @@ -114,9 +111,9 @@ class Test extends Request { * @api public */ end(fn) { - var self = this; - var server = this._server; - var end = Request.prototype.end; + const self = this; + const server = this._server; + const end = Request.prototype.end; end.call(this, function (err, res) { if (server && server._handle) return server.close(localAssert); @@ -140,14 +137,13 @@ class Test extends Request { * @api private */ assert(resError, res, fn) { - var errorObj; - var i; + let errorObj; // check for unexpected network errors or server not running/reachable errors // when there is no response and superagent sends back a System Error // do not check further for other asserts, if any, in such case // https://nodejs.org/api/errors.html#errors_common_system_errors - var sysErrors = { + const sysErrors = { ECONNREFUSED: 'Connection refused', ECONNRESET: 'Connection reset by peer', EPIPE: 'Broken pipe', @@ -164,7 +160,7 @@ class Test extends Request { } // asserts - for (i = 0; i < this._asserts.length && !errorObj; i += 1) { + for (let i = 0; i < this._asserts.length && !errorObj; i += 1) { errorObj = this._assertFunction(this._asserts[i], res); } @@ -185,26 +181,24 @@ class Test extends Request { * @api private */// eslint-disable-next-line class-methods-use-this _assertBody(body, res) { - var isregexp = body instanceof RegExp; - var a; - var b; + const isRegexp = body instanceof RegExp; // parsed - if (typeof body === 'object' && !isregexp) { + if (typeof body === 'object' && !isRegexp) { try { assert.deepStrictEqual(body, res.body); } catch (err) { - a = util.inspect(body); - b = util.inspect(res.body); + const a = inspect(body); + const b = inspect(res.body); return error('expected ' + a + ' response body, got ' + b, body, res.body); } } else if (body !== res.text) { // string - a = util.inspect(body); - b = util.inspect(res.text); + const a = inspect(body); + const b = inspect(res.text); // regexp - if (isregexp) { + if (isRegexp) { if (!body.test(res.text)) { return error('expected body ' + b + ' to match ' + body, body, res.body); } @@ -223,9 +217,9 @@ class Test extends Request { * @api private */// eslint-disable-next-line class-methods-use-this _assertHeader(header, res) { - var field = header.name; - var actual = res.header[field.toLowerCase()]; - var fieldExpected = header.value; + const field = header.name; + const actual = res.header[field.toLowerCase()]; + const fieldExpected = header.value; if (typeof actual === 'undefined') return new Error('expected "' + field + '" header field'); // This check handles header values that may be a String or single element Array @@ -252,11 +246,9 @@ class Test extends Request { * @api private */// eslint-disable-next-line class-methods-use-this _assertStatus(status, res) { - var a; - var b; if (res.status !== status) { - a = http.STATUS_CODES[status]; - b = http.STATUS_CODES[res.status]; + const a = STATUS_CODES[status]; + const b = STATUS_CODES[res.status]; return new Error('expected ' + status + ' "' + a + '", got ' + res.status + ' "' + b + '"'); } } @@ -270,11 +262,9 @@ class Test extends Request { * @api private */// eslint-disable-next-line class-methods-use-this _assertStatusArray(statusArray, res) { - var b; - var expectedList; if (!statusArray.includes(res.status)) { - b = http.STATUS_CODES[res.status]; - expectedList = statusArray.join(', '); + const b = STATUS_CODES[res.status]; + const expectedList = statusArray.join(', '); return new Error( 'expected one of "' + expectedList + '", got ' + res.status + ' "' + b + '"' ); @@ -290,7 +280,7 @@ class Test extends Request { * @api private */// eslint-disable-next-line class-methods-use-this _assertFunction(fn, res) { - var err; + let err; try { err = fn(res); } catch (e) { @@ -309,11 +299,11 @@ class Test extends Request { */ function wrapAssertFn(assertFn) { - var savedStack = new Error().stack.split('\n').slice(3); + const savedStack = new Error().stack.split('\n').slice(3); return function(res) { - var badStack; - var err = assertFn(res); + let badStack; + const err = assertFn(res); if (err instanceof Error && err.stack) { badStack = err.stack.replace(err.message, '').split('\n').slice(1); err.stack = [err.toString()] @@ -337,7 +327,7 @@ function wrapAssertFn(assertFn) { */ function error(msg, expected, actual) { - var err = new Error(msg); + const err = new Error(msg); err.expected = expected; err.actual = actual; err.showDiff = true; From dc60d5d2b8429b044e53acd0fc1d9cbba1cd50f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jimmy=20Wa=CC=88rting?= Date: Tue, 4 Jan 2022 21:55:33 +0100 Subject: [PATCH 2/2] destruct assert --- lib/test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/test.js b/lib/test.js index 87c9491..58fa87c 100644 --- a/lib/test.js +++ b/lib/test.js @@ -7,7 +7,7 @@ const { inspect } = require('util'); const { STATUS_CODES } = require('http'); const { Server } = require('https'); -const assert = require('assert'); +const { deepStrictEqual } = require('assert'); const { Request } = require('superagent'); class Test extends Request { @@ -186,7 +186,7 @@ class Test extends Request { // parsed if (typeof body === 'object' && !isRegexp) { try { - assert.deepStrictEqual(body, res.body); + deepStrictEqual(body, res.body); } catch (err) { const a = inspect(body); const b = inspect(res.body);