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

refactor: convert var to const / let #748

Merged
merged 2 commits into from
Jan 4, 2022
Merged
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
78 changes: 34 additions & 44 deletions lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 { deepStrictEqual } = require('assert');
const { Request } = require('superagent');

class Test extends Request {
/**
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
Expand All @@ -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',
Expand All @@ -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);
}

Expand All @@ -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);
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);
}
Expand All @@ -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
Expand All @@ -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 + '"');
}
}
Expand All @@ -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 + '"'
);
Expand All @@ -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) {
Expand All @@ -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()]
Expand All @@ -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;
Expand Down