Skip to content

Commit

Permalink
Allow setting a custom promise implementation (#3)
Browse files Browse the repository at this point in the history
Closes #3
  • Loading branch information
forivall authored and amacneil committed May 14, 2018
1 parent 522f12b commit 96965bc
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ function TestServer(app) {
return new TestServer();
}

// allow custom promise
if (!TestServer.Promise) {
throw new Error('native promise missing, set TestServer.Promise to your favorite alternative');
}

this.Promise = TestServer.Promise;
this.server = http.createServer(app);

['delete', 'get', 'head', 'options', 'patch', 'post', 'put'].forEach((method) => {
Expand All @@ -26,7 +32,7 @@ function TestServer(app) {

TestServer.prototype.listen = function listen() {
if (!this.listener) {
this.listener = new Promise((resolve, reject) => {
this.listener = new this.Promise((resolve, reject) => {
this.server.listen(0, () => resolve())
.on('error', (err) => reject(err));
});
Expand All @@ -38,7 +44,7 @@ TestServer.prototype.listen = function listen() {
TestServer.prototype.close = function close() {
this.listener = null;

return new Promise((resolve, reject) => {
return new this.Promise((resolve, reject) => {
this.server.close((err) => (err ? reject(err) : resolve()));
});
};
Expand All @@ -60,4 +66,7 @@ TestServer.prototype.fetch = function fetch(path, opts) {
});
};

// expose Promise
TestServer.Promise = global.Promise;

module.exports = TestServer;

0 comments on commit 96965bc

Please sign in to comment.