diff --git a/index.js b/index.js index 8fd46db..dc206a4 100644 --- a/index.js +++ b/index.js @@ -3,9 +3,10 @@ /** * Module dependencies. */ -var methods = require('methods'); -var Test = require('./lib/test'); -var http = require('http'); +const methods = require('methods'); +const http = require('http'); +const Test = require('./lib/test.js'); +const agent = require('./lib/agent.js'); /** * Test against the given `app`, @@ -16,7 +17,7 @@ var http = require('http'); * @api public */ module.exports = function(app) { - var obj = {}; + const obj = {}; if (typeof app === 'function') { app = http.createServer(app); // eslint-disable-line no-param-reassign @@ -42,4 +43,4 @@ module.exports.Test = Test; /** * Expose the agent function */ -module.exports.agent = require('./lib/agent'); +module.exports.agent = agent; diff --git a/lib/agent.js b/lib/agent.js index c6ee786..0afb7c7 100644 --- a/lib/agent.js +++ b/lib/agent.js @@ -4,16 +4,10 @@ * Module dependencies. */ -var Agent = require('superagent').agent; -var methods = require('methods'); -var http = require('http'); -var Test = require('./test'); - -/** - * Expose `Agent`. - */ - -module.exports = TestAgent; +const { agent: Agent } = require('superagent'); +const methods = require('methods'); +const http = require('http'); +const Test = require('./test.js'); /** * Initialize a new `TestAgent`. @@ -50,7 +44,7 @@ TestAgent.prototype.host = function(host) { // override HTTP verb methods 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, this._host); + const req = new Test(this.app, method.toUpperCase(), url, this._host); req.ca(this._ca); req.cert(this._cert); req.key(this._key); @@ -69,3 +63,9 @@ methods.forEach(function(method) { }); TestAgent.prototype.del = TestAgent.prototype.delete; + +/** + * Expose `Agent`. + */ + +module.exports = TestAgent; diff --git a/lib/test.js b/lib/test.js index 58fa87c..d835fbd 100644 --- a/lib/test.js +++ b/lib/test.js @@ -10,6 +10,8 @@ const { Server } = require('https'); const { deepStrictEqual } = require('assert'); const { Request } = require('superagent'); +/** @typedef {import('superagent').Response} Response */ + class Test extends Request { /** * Initialize a new `Test` with the given `app`, @@ -111,18 +113,16 @@ class Test extends Request { * @api public */ end(fn) { - const self = this; const server = this._server; - const end = Request.prototype.end; - end.call(this, function (err, res) { + super.end((err, res) => { + const localAssert = () => { + this.assert(err, res, fn); + }; + if (server && server._handle) return server.close(localAssert); localAssert(); - - function localAssert() { - self.assert(err, res, fn); - } }); return this; diff --git a/test/supertest.js b/test/supertest.js index 0a77dec..7561d1f 100644 --- a/test/supertest.js +++ b/test/supertest.js @@ -1,6 +1,5 @@ 'use strict'; -const request = require('..'); const https = require('https'); const fs = require('fs'); const path = require('path'); @@ -9,6 +8,7 @@ const express = require('express'); const bodyParser = require('body-parser'); const cookieParser = require('cookie-parser'); const nock = require('nock'); +const request = require('../index.js'); process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';