Popular packages:
Update: Don't waste your time for native readline
, use inquirer
! ;)
Task:
- Show an instruction about requesting name and age.
Please fill in the following form.
- Show prompt and request a name.
Name? >
- Wait for the prompt and ask age.
Age? >
- Write back the given data using a terminal table format, like
name | age |
---|---|
Joe | 25 |
Instead of using the built in readline
, there is a great wrapper: inquirer
.
- We have to mock
inquirer
andinquirer.prompt
.
import inquirer from 'inquirer';
jest.mock('inquirer')
test('some test', () => {
inquirer.prompt = jest.fn().mockResolvedValue({ questionName: 'answer' });
const result = await myAsyncMethod();
expect(result).toEqual('answer');
}
- Setup
Question[]
array:
const questions: Question[] = [{ message: 'The question', name: 'questionName' }]
- Prompt
const answers: Answers = await prompt(questions);
That's it.
oclif
tutorial: https://oclif.io/docs/introduction.html