Skip to content

Commit

Permalink
Merge pull request #324 from visionmedia/eslint-refactor
Browse files Browse the repository at this point in the history
Eslint refactor
  • Loading branch information
mikelax committed Mar 31, 2016
2 parents ecced93 + af3c013 commit 480b9bb
Show file tree
Hide file tree
Showing 6 changed files with 348 additions and 285 deletions.
22 changes: 22 additions & 0 deletions .eslintrc
@@ -0,0 +1,22 @@
{
"extends": "airbnb/legacy",
"env": {
"node": true,
"mocha": true
},
"rules": {
// disabled - disagree with airbnb
"func-names": [0],
"space-before-function-paren": [0],
"consistent-return": [0],

// Disabled but may want to refactor code eventually
"no-proto": [1],
"no-shadow": [1],
"no-use-before-define": [2, "nofunc"],

// IMHO, more sensible overrides to existing airbnb error definitions
"max-len": [2, 100, 4, {"ignoreComments": true, "ignoreUrls": true}],
"no-unused-expressions": [2, { "allowShortCircuit": true, "allowTernary": true }]
}
}
16 changes: 8 additions & 8 deletions lib/agent.js
Expand Up @@ -3,10 +3,10 @@
* Module dependencies.
*/

var Agent = require('superagent').agent
, methods = require('methods')
, http = require('http')
, Test = require('./test');
var Agent = require('superagent').agent;
var methods = require('methods');
var http = require('http');
var Test = require('./test');

/**
* Expose `Agent`.
Expand All @@ -22,9 +22,9 @@ module.exports = TestAgent;
* @api public
*/

function TestAgent(app, options){
function TestAgent(app, options) {
if (!(this instanceof TestAgent)) return new TestAgent(app, options);
if ('function' == typeof app) app = http.createServer(app);
if (typeof app === 'function') app = http.createServer(app); // eslint-disable-line no-param-reassign
if (options) this._ca = options.ca;
Agent.call(this);
this.app = app;
Expand All @@ -37,8 +37,8 @@ function TestAgent(app, options){
TestAgent.prototype.__proto__ = Agent.prototype;

// override HTTP verb methods
methods.forEach(function(method){
TestAgent.prototype[method] = function(url, fn){
methods.forEach(function(method) {
TestAgent.prototype[method] = function(url, fn) { // eslint-disable-line no-unused-vars
var req = new Test(this.app, method.toUpperCase(), url);
req.ca(this._ca);

Expand Down
90 changes: 54 additions & 36 deletions lib/test.js
Expand Up @@ -2,12 +2,12 @@
* Module dependencies.
*/

var request = require('superagent')
, util = require('util')
, http = require('http')
, https = require('https')
, assert = require('assert')
, Request = request.Request;
var request = require('superagent');
var util = require('util');
var http = require('http');
var https = require('https');
var assert = require('assert');
var Request = request.Request;

/**
* Expose `Test`.
Expand All @@ -31,7 +31,7 @@ function Test(app, method, path) {
this.buffer();
this.app = app;
this._asserts = [];
this.url = 'string' == typeof app
this.url = typeof app === 'string'
? app + path
: this.serverAddress(app, path);
}
Expand All @@ -51,11 +51,14 @@ Test.prototype.__proto__ = Request.prototype;
* @api private
*/

Test.prototype.serverAddress = function(app, path){
Test.prototype.serverAddress = function(app, path) {
var addr = app.address();
var port;
var protocol;

if (!addr) this._server = app.listen(0);
var port = app.address().port;
var protocol = app instanceof https.Server ? 'https' : 'http';
port = app.address().port;
protocol = app instanceof https.Server ? 'https' : 'http';
return protocol + '://127.0.0.1:' + port + path;
};

Expand All @@ -75,27 +78,28 @@ Test.prototype.serverAddress = function(app, path){
* @api public
*/

Test.prototype.expect = function(a, b, c){
Test.prototype.expect = function(a, b, c) {
// callback
if ('function' == typeof a) {
if (typeof a === 'function') {
this._asserts.push(a);
return this;
}
if ('function' == typeof b) this.end(b);
if ('function' == typeof c) this.end(c);
if (typeof b === 'function') this.end(b);
if (typeof c === 'function') this.end(c);

// status
if ('number' == typeof a) {
if (typeof a === 'number') {
this._asserts.push(this._assertStatus.bind(this, a));
// body
if ('function' != typeof b && arguments.length > 1)
if (typeof b !== 'function' && arguments.length > 1) {
this._asserts.push(this._assertBody.bind(this, b));
}
return this;
}

// header field
if ('string' == typeof b || 'number' == typeof b || b instanceof RegExp) {
this._asserts.push(this._assertHeader.bind(this, {name: ''+a, value: b}));
if (typeof b === 'string' || typeof b === 'number' || b instanceof RegExp) {
this._asserts.push(this._assertHeader.bind(this, { name: '' + a, value: b }));
return this;
}

Expand All @@ -113,17 +117,17 @@ Test.prototype.expect = function(a, b, c){
* @api public
*/

Test.prototype.end = function(fn){
Test.prototype.end = function(fn) {
var self = this;
var server = this._server;
var end = Request.prototype.end;

end.call(this, function(err, res){
end.call(this, function(err, res) {
if (server && server._handle) return server.close(assert);

assert();

function assert(){
function assert() {
self.assert(err, res, fn);
}
});
Expand All @@ -140,18 +144,19 @@ Test.prototype.end = function(fn){
* @api private
*/

Test.prototype.assert = function(resError, res, fn){
Test.prototype.assert = function(resError, res, fn) {
var error;
var i;

// asserts
for (var i = 0; i < this._asserts.length && !error; ++i) {
for (i = 0; i < this._asserts.length && !error; ++i) {
error = this._assertFunction(this._asserts[i], res);
}

// set unexpected superagent error if no other error has occurred.
if (!error && resError instanceof Error &&
(!res || resError.status !== res.status))
if (!error && resError instanceof Error && (!res || resError.status !== res.status)) {
error = resError;
}

fn.call(this, error || null, res);
};
Expand All @@ -167,20 +172,23 @@ Test.prototype.assert = function(resError, res, fn){

Test.prototype._assertBody = function(body, res) {
var isregexp = body instanceof RegExp;
var a;
var b;

// parsed
if ('object' == typeof body && !isregexp) {
if (typeof body === 'object' && !isregexp) {
try {
assert.deepEqual(body, res.body);
} catch (err) {
var a = util.inspect(body);
var b = util.inspect(res.body);
a = util.inspect(body);
b = util.inspect(res.body);
return error('expected ' + a + ' response body, got ' + b, body, res.body);
}
} else {
// string
if (body !== res.text) {
var a = util.inspect(body);
var b = util.inspect(res.text);
a = util.inspect(body);
b = util.inspect(res.text);

// regexp
if (isregexp) {
Expand All @@ -206,11 +214,19 @@ Test.prototype._assertBody = function(body, res) {
Test.prototype._assertHeader = function(header, res) {
var field = header.name;
var actual = res.header[field.toLowerCase()];
if (null == actual) return new Error('expected "' + field + '" header field');
var fieldExpected = header.value;
if (fieldExpected == actual) return;

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
if ((actual instanceof Array && actual.toString() === fieldExpected) ||
fieldExpected === actual) {
return;
}
if (fieldExpected instanceof RegExp) {
if (!fieldExpected.test(actual)) return new Error('expected "' + field + '" matching ' + fieldExpected + ', got "' + actual + '"');
if (!fieldExpected.test(actual)) {
return new Error('expected "' + field + '" matching ' +
fieldExpected + ', got "' + actual + '"');
}
} else {
return new Error('expected "' + field + '" of "' + fieldExpected + '", got "' + actual + '"');
}
Expand All @@ -226,9 +242,11 @@ Test.prototype._assertHeader = function(header, res) {
*/

Test.prototype._assertStatus = function(status, res) {
var a;
var b;
if (res.status !== status) {
var a = http.STATUS_CODES[status];
var b = http.STATUS_CODES[res.status];
a = http.STATUS_CODES[status];
b = http.STATUS_CODES[res.status];
return new Error('expected ' + status + ' "' + a + '", got ' + res.status + ' "' + b + '"');
}
};
Expand All @@ -245,7 +263,7 @@ Test.prototype._assertFunction = function(check, res) {
var err;
try {
err = check(res);
} catch(e) {
} catch (e) {
err = e;
}
if (err instanceof Error) return err;
Expand Down
4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"scripts": {
"pretest": "npm install",
"test": "mocha --require should --reporter spec --check-leaks"
"test": "eslint lib/** test/** && mocha --require should --reporter spec --check-leaks"
},
"dependencies": {
"superagent": "^1.7.2",
Expand All @@ -14,6 +14,8 @@
"devDependencies": {
"body-parser": "~1.13.2",
"cookie-parser": "~1.3.5",
"eslint": "^2.5.3",
"eslint-config-airbnb": "^6.2.0",
"express": "~4.12.4",
"mocha": "~2.2.5",
"should": "~7.0.2"
Expand Down
10 changes: 10 additions & 0 deletions test/.eslintrc
@@ -0,0 +1,10 @@
{
"rules": {
// errors - disabled for chai test support
"no-unused-expressions": [0],
// allow function args for superagent
"no-unused-vars": [2, {"args": "none"}],
// allow updates to response for certain tests
"no-param-reassign": [2, {"props": false}]
}
}

0 comments on commit 480b9bb

Please sign in to comment.