From 96965bc0834036347083f878a5ad4a03f3cfb12d Mon Sep 17 00:00:00 2001 From: Emily Marigold Klassen Date: Sun, 13 May 2018 17:47:16 -0700 Subject: [PATCH] Allow setting a custom promise implementation (#3) Closes #3 --- index.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 7910e2a..5ee6f1a 100644 --- a/index.js +++ b/index.js @@ -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) => { @@ -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)); }); @@ -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())); }); }; @@ -60,4 +66,7 @@ TestServer.prototype.fetch = function fetch(path, opts) { }); }; +// expose Promise +TestServer.Promise = global.Promise; + module.exports = TestServer;