diff options
author | Tobias Nießen <tniessen@tnie.de> | 2022-07-23 23:44:26 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-07-23 23:44:26 +0300 |
commit | 7ef069e483e015a803660125cdbfa81ddfa0357b (patch) | |
tree | c14c33a2fd2f1ce80355fa11f79a053d973bace6 | |
parent | df7c49c91de8d3a16d6b27a85498da34535ac0e4 (diff) |
test: simplify ReplStream.wait()
PR-URL: https://github.com/nodejs/node/pull/43857
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
-rw-r--r-- | test/parallel/test-repl-preview.js | 25 | ||||
-rw-r--r-- | test/parallel/test-repl-top-level-await.js | 25 |
2 files changed, 16 insertions, 34 deletions
diff --git a/test/parallel/test-repl-preview.js b/test/parallel/test-repl-preview.js index 620f41296e9..6eb2a169918 100644 --- a/test/parallel/test-repl-preview.js +++ b/test/parallel/test-repl-preview.js @@ -2,6 +2,7 @@ const common = require('../common'); const assert = require('assert'); +const events = require('events'); const { REPLServer } = require('repl'); const { Stream } = require('stream'); const { inspect } = require('util'); @@ -32,26 +33,16 @@ class REPLStream extends Stream { if (chunkLines.length > 1) { this.lines.push(...chunkLines.slice(1)); } - this.emit('line'); + this.emit('line', this.lines[this.lines.length - 1]); return true; } - wait() { + async wait() { this.lines = ['']; - return new Promise((resolve, reject) => { - const onError = (err) => { - this.removeListener('line', onLine); - reject(err); - }; - const onLine = () => { - if (this.lines[this.lines.length - 1].includes(PROMPT)) { - this.removeListener('error', onError); - this.removeListener('line', onLine); - resolve(this.lines); - } - }; - this.once('error', onError); - this.on('line', onLine); - }); + for await (const [line] of events.on(this, 'line')) { + if (line.includes(PROMPT)) { + return this.lines; + } + } } pause() {} resume() {} diff --git a/test/parallel/test-repl-top-level-await.js b/test/parallel/test-repl-top-level-await.js index 4ab2a32ee09..1abcca75f1e 100644 --- a/test/parallel/test-repl-top-level-await.js +++ b/test/parallel/test-repl-top-level-await.js @@ -3,6 +3,7 @@ const common = require('../common'); const ArrayStream = require('../common/arraystream'); const assert = require('assert'); +const events = require('events'); const { stripVTControlCharacters } = require('internal/util/inspect'); const repl = require('repl'); @@ -27,31 +28,21 @@ class REPLStream extends ArrayStream { if (chunkLines.length > 1) { this.lines.push(...chunkLines.slice(1)); } - this.emit('line'); + this.emit('line', this.lines[this.lines.length - 1]); if (callback) callback(); return true; } - wait() { + async wait() { if (this.waitingForResponse) { throw new Error('Currently waiting for response to another command'); } this.lines = ['']; - return new Promise((resolve, reject) => { - const onError = (err) => { - this.removeListener('line', onLine); - reject(err); - }; - const onLine = () => { - if (this.lines[this.lines.length - 1].includes(PROMPT)) { - this.removeListener('error', onError); - this.removeListener('line', onLine); - resolve(this.lines); - } - }; - this.once('error', onError); - this.on('line', onLine); - }); + for await (const [line] of events.on(this, 'line')) { + if (line.includes(PROMPT)) { + return this.lines; + } + } } } |