https://github.com/jamesgeorge007/cli-prompts-test
Write e2e tests for CLI apps with ease
https://github.com/jamesgeorge007/cli-prompts-test
cli-apps command-line commanderjs e2e-tests enquirer hacktoberfest hacktoberfest2020 inquirer integration-testing javascript nodejs test yargs
Last synced: about 2 months ago
JSON representation
Write e2e tests for CLI apps with ease
- Host: GitHub
- URL: https://github.com/jamesgeorge007/cli-prompts-test
- Owner: jamesgeorge007
- License: gpl-3.0
- Created: 2020-08-22T11:33:31.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2023-01-06T13:51:10.000Z (over 2 years ago)
- Last Synced: 2025-04-13T19:14:38.274Z (2 months ago)
- Topics: cli-apps, command-line, commanderjs, e2e-tests, enquirer, hacktoberfest, hacktoberfest2020, inquirer, integration-testing, javascript, nodejs, test, yargs
- Language: JavaScript
- Homepage:
- Size: 458 KB
- Stars: 21
- Watchers: 3
- Forks: 3
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# CLI Prompts Test
> Write e2e tests for CLI apps with ease.
## Installation
```bash
$ npm install --save-dev cli-prompts-test
```## API
### runTest(args, answers, options?)
- `args`: CLI args to pass in.
- `answers`: answers to be passed to `stdout` (simulate user input).
- `options`: Optionally specify the `testPath` (defaults to `process.cwd()`) and `timeout` (defaults to `500ms`) between keystrokes.## Usage
```js
// cli.jsconst enquirer = require("enquirer");
const choices = ["First option", "Second option", "Third option"];
enquirer
.prompt({
type: "select",
name: "option",
message: "Choose from below",
choices,
})
.then(({ option }) => {
console.log(`You chose ${option}`);
});
``````js
// test.jsimport runTest, { DOWN, ENTER } from "cli-prompts-test";
const cliPath = `${__dirname}/cli.js`;
describe("cli-prompts-test", () => {
it("picks first option", async () => {
const { exitCode, stdout } = await runTest([cliPath], [ENTER]);// Assertions
expect(exitCode).toBe(0);
expect(stdout).toContain("You chose First option");
});it("picks second option", async () => {
const { exitCode, stdout } = await runTest([cliPath], [`${DOWN}${ENTER}`]);// Assertions
expect(exitCode).toBe(0);
expect(stdout).toContain("You chose Second option");
});it("picks third option", async () => {
const { exitCode, stdout } = await runTest(
[cliPath],
[`${DOWN}${DOWN}${ENTER}`]
);// Assertions
expect(exitCode).toBe(0);
expect(stdout).toContain("You chose Third option");
});
});
```Find an example [here](https://github.com/madlabsinc/mevn-cli/blob/develop/jest/helpers.js).