Skip to content

Commit 949edd2

Browse files
authored
Merge pull request #82 from AllanOricil/vitest-setup-2
feat: make load, setFlows async/await (enable vitest)
2 parents db9a73f + 00f3f69 commit 949edd2

File tree

1 file changed

+33
-26
lines changed

1 file changed

+33
-26
lines changed

index.js

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ class NodeTestHelper extends EventEmitter {
192192
return this._settings;
193193
}
194194

195-
load(testNode, testFlow, testCredentials, cb) {
195+
async load(testNode, testFlow, testCredentials, cb) {
196196
const log = this._log;
197197
const logSpy = this._logSpy = this._sandbox.spy(log, 'log');
198198
logSpy.FATAL = log.FATAL;
@@ -314,13 +314,16 @@ class NodeTestHelper extends EventEmitter {
314314
}
315315
}
316316
});
317-
return Promise.all(initPromises)
318-
.then(() => redNodes.loadFlows())
319-
.then(() => redNodes.startFlows())
320-
.then(() => {
321-
should.deepEqual(testFlow, redNodes.getFlows().flows);
322-
if(cb) cb();
323-
});
317+
try {
318+
await Promise.all(initPromises);
319+
await redNodes.loadFlows();
320+
await redNodes.startFlows();
321+
should.deepEqual(testFlow, redNodes.getFlows().flows);
322+
if (cb) cb();
323+
} catch (error) {
324+
if (cb) cb(error);
325+
else throw error;
326+
}
324327
}
325328

326329
unload() {
@@ -355,7 +358,7 @@ class NodeTestHelper extends EventEmitter {
355358
* @param {function} [cb] Optional callback (not required when called with await)
356359
* @returns {Promise}
357360
*/
358-
setFlows(testFlow, type, testCredentials, cb) {
361+
async setFlows(testFlow, type, testCredentials, cb) {
359362
const helper = this;
360363
if (typeof testCredentials === 'string' ) {
361364
cb = testCredentials;
@@ -383,18 +386,22 @@ class NodeTestHelper extends EventEmitter {
383386
helper._events.on('flows:started', hander); // call resolve when its done
384387
});
385388
}
386-
return this._redNodes.setFlows(testFlow, testCredentials || {}, type)
387-
.then(waitStarted)
388-
.then(() => {
389-
if(cb) cb();
390-
});
389+
try {
390+
await this._redNodes.setFlows(testFlow, testCredentials || {}, type);
391+
await waitStarted();
392+
393+
if (cb) cb();
394+
} catch (error) {
395+
if (cb) cb(error);
396+
else throw error;
397+
}
391398
}
392399

393400
request() {
394401
return request(this._httpAdmin);
395402
}
396403

397-
async startServer(done) {
404+
async startServer(cb) {
398405
try {
399406
await new Promise((resolve, reject) => {
400407
this._app = express();
@@ -419,30 +426,30 @@ class NodeTestHelper extends EventEmitter {
419426
server.on('error', reject);
420427
});
421428

422-
if (done) done();
423-
} catch (err) {
424-
if (done) done(err);
425-
else throw err;
429+
if (cb) cb();
430+
} catch (error) {
431+
if (cb) cb(error);
432+
else throw error;
426433
}
427434
}
428435

429-
async stopServer(done) {
436+
async stopServer(cb) {
430437
try {
431438
if (this._server) {
432439
await new Promise((resolve, reject) => {
433440
this._comms.stop();
434441

435-
this._server.stop((err) => {
436-
if (err) reject(err);
442+
this._server.stop((error) => {
443+
if (error) reject(error);
437444
else resolve();
438445
});
439446
});
440447
}
441448

442-
if (done) done();
443-
} catch (err) {
444-
if (done) done(err);
445-
else throw err;
449+
if (cb) cb();
450+
} catch (error) {
451+
if (cb) cb(error);
452+
else throw error;
446453
}
447454
}
448455

0 commit comments

Comments
 (0)