Skip to content

Commit

Permalink
Merge pull request #82 from AllanOricil/vitest-setup-2
Browse files Browse the repository at this point in the history
feat: make load, setFlows async/await (enable vitest)
  • Loading branch information
knolleary authored Nov 5, 2024
2 parents db9a73f + 00f3f69 commit 949edd2
Showing 1 changed file with 33 additions and 26 deletions.
59 changes: 33 additions & 26 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ class NodeTestHelper extends EventEmitter {
return this._settings;
}

load(testNode, testFlow, testCredentials, cb) {
async load(testNode, testFlow, testCredentials, cb) {
const log = this._log;
const logSpy = this._logSpy = this._sandbox.spy(log, 'log');
logSpy.FATAL = log.FATAL;
Expand Down Expand Up @@ -314,13 +314,16 @@ class NodeTestHelper extends EventEmitter {
}
}
});
return Promise.all(initPromises)
.then(() => redNodes.loadFlows())
.then(() => redNodes.startFlows())
.then(() => {
should.deepEqual(testFlow, redNodes.getFlows().flows);
if(cb) cb();
});
try {
await Promise.all(initPromises);
await redNodes.loadFlows();
await redNodes.startFlows();
should.deepEqual(testFlow, redNodes.getFlows().flows);
if (cb) cb();
} catch (error) {
if (cb) cb(error);
else throw error;
}
}

unload() {
Expand Down Expand Up @@ -355,7 +358,7 @@ class NodeTestHelper extends EventEmitter {
* @param {function} [cb] Optional callback (not required when called with await)
* @returns {Promise}
*/
setFlows(testFlow, type, testCredentials, cb) {
async setFlows(testFlow, type, testCredentials, cb) {
const helper = this;
if (typeof testCredentials === 'string' ) {
cb = testCredentials;
Expand Down Expand Up @@ -383,18 +386,22 @@ class NodeTestHelper extends EventEmitter {
helper._events.on('flows:started', hander); // call resolve when its done
});
}
return this._redNodes.setFlows(testFlow, testCredentials || {}, type)
.then(waitStarted)
.then(() => {
if(cb) cb();
});
try {
await this._redNodes.setFlows(testFlow, testCredentials || {}, type);
await waitStarted();

if (cb) cb();
} catch (error) {
if (cb) cb(error);
else throw error;
}
}

request() {
return request(this._httpAdmin);
}

async startServer(done) {
async startServer(cb) {
try {
await new Promise((resolve, reject) => {
this._app = express();
Expand All @@ -419,30 +426,30 @@ class NodeTestHelper extends EventEmitter {
server.on('error', reject);
});

if (done) done();
} catch (err) {
if (done) done(err);
else throw err;
if (cb) cb();
} catch (error) {
if (cb) cb(error);
else throw error;
}
}

async stopServer(done) {
async stopServer(cb) {
try {
if (this._server) {
await new Promise((resolve, reject) => {
this._comms.stop();

this._server.stop((err) => {
if (err) reject(err);
this._server.stop((error) => {
if (error) reject(error);
else resolve();
});
});
}

if (done) done();
} catch (err) {
if (done) done(err);
else throw err;
if (cb) cb();
} catch (error) {
if (cb) cb(error);
else throw error;
}
}

Expand Down

0 comments on commit 949edd2

Please sign in to comment.