Skip to content
This repository has been archived by the owner on Feb 1, 2022. It is now read-only.

Commit

Permalink
test: Add timeout to waitFor(pattern)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Krems committed Oct 14, 2016
1 parent 6a23e0c commit 63b0f9b
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions test/cli/start-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,41 @@ function startCLI(args) {
return output;
},

waitForPrompt() {
waitFor(pattern, timeout = 2000) {
return new Promise((resolve, reject) => {
function checkOutput() {
if (/debug>\s*$/.test(getOutput())) {
child.stdout.removeListener('data', checkOutput);
if (pattern.test(getOutput())) {
tearDown(); // eslint-disable-line no-use-before-define
resolve();
}
}
child.on('exit', () => {
reject(new Error('Child quit while waiting for prompt'));
});

function onChildExit() {
tearDown(); // eslint-disable-line no-use-before-define
reject(new Error(`Child quit while waiting for ${pattern}`));
}

const timer = setTimeout(() => {
tearDown(); // eslint-disable-line no-use-before-define
reject(new Error(`Timeout (${timeout}) while waiting for ${pattern}`));
}, timeout);

function tearDown() {
clearTimeout(timer);
child.stdout.removeListener('data', checkOutput);
child.removeListener('exit', onChildExit);
}

child.on('exit', onChildExit);
child.stdout.on('data', checkOutput);
checkOutput();
});
},

waitForPrompt(timeout = 2000) {
return this.waitFor(/debug>\s*$/, timeout);
},

get output() {
return getOutput();
},
Expand Down

0 comments on commit 63b0f9b

Please sign in to comment.