Skip to content

Rework to using classes instead of prototypes #318

Open
@43081j

Description

In much of this library, we use functions as constructors and add methods to the prototype, using util.inherits for inheritance

We should probably rework this to use classes now that they're widely available.

For example:

chai-http/lib/request.js

Lines 307 to 318 in cc58a4b

function TestAgent(app) {
if (!(this instanceof TestAgent)) return new TestAgent(app);
if (typeof app === 'function') app = http.createServer(app);
(Agent || Request).call(this);
this.app = app;
if (typeof app !== 'string' && app && app.listen && app.address && !app.address()) {
this.app = app.listen(0)
}
}
util.inherits(TestAgent, Agent || Request);
TestAgent.prototype.close = function close(callback) {

Could be:

class TestAgent extends (Agent || Request) {
  constructor(app) {
    super();
    if (typeof app === 'function') app = http.createServer(app);
    this.app = app;
    if (typeof app !== 'string' && app && app.listen && app.address && !app.address()) {
      this.app = app.listen(0)
    }
  }

  close(callback) {
    // ...
  }
}

Metadata

Assignees

No one assigned

    Labels

    5.xNext major of the library

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions