2

I am trying to test my entire cli dialogs using oclif and @oclif/test. Prompts are made with inquirer.

Everything work fine, except the use of the .stdin( mock.

Feature extract looks like this:

...
const { sessionToken } = await inquirer.prompt([{
    type: 'password',
    name: 'sessionToken',
    message: 'Paste your token:',
    validate: token => 'Invalid token.',
}]);
...

Test code is:

  describe('with an invalid token', () => {
    fancy
      .stdout({ print: true })
      .command(['login', '-e', '[email protected]'])
      .stdin("bad_token")
      .it('should display invalid token', ctx => {
        process.stdin.setEncoding('utf8');
        process.stdin.once('data', data => {
          expect(data).toEquals("bad_token");
          expect(ctx.stdout).to.contain('Invalid token.');
        });
      });
  });

Actual: The test stop on "Paste your token:"

Expected: The test continues and displays "Invalid token.", like when manually testing.

3
  • I've ran into the same issue, did you ever managed to overcome it? Commented Jul 19, 2020 at 8:10
  • No, I drop @oclif/test and I've created my on small test lib. Commented Jul 21, 2020 at 18:54
  • Would be interested in sharing it? I'm having trouble implementing this type of test and would be grateful for some inspiration. Commented Jul 22, 2020 at 7:02

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.