|
1 |
| -# [WIP] CLI Testing Tool |
| 1 | +# CLI Testing Tool |
2 | 2 |
|
3 | 3 | A testing library that allows you to test input and outputs of your CLI command.
|
4 | 4 |
|
5 |
| -*Note: This is a work in progress. The API is likely going to change.* |
| 5 | +*Note: This is a work in progress.* |
6 | 6 |
|
7 | 7 | **Terminal Text Parsing Support Checklist**
|
8 | 8 | Refer to [Full List of Ansi Escape Codes](https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797) that need to be handled.
|
@@ -32,61 +32,80 @@ yarn add --dev cli-testing-tool
|
32 | 32 |
|
33 | 33 | Check out [Interactive Examples on Stackblitz](https://stackblitz.com/edit/node-kfod5b?file=examples%2Fprompts%2Fprompts.test.js)
|
34 | 34 |
|
35 |
| -### Eg 1: Hello World Example |
| 35 | +### Testing Colored Terminal Text |
| 36 | + |
| 37 | +Check out [this example of StackBlitz](https://stackblitz.com/edit/node-kfod5b?file=examples%2Fgraphic-hello-world%2Fgraphic-hello-world.test.js) |
36 | 38 |
|
37 | 39 | ```js
|
38 |
| -// hello-world.test.js |
| 40 | +// colored-greeting.test.js |
39 | 41 | const { createCommandInterface } = require('cli-testing-tool');
|
40 | 42 |
|
41 |
| -test('should print greetings', async () => { |
42 |
| - const commandInterface = createCommandInterface('node ./hello-world.js', { |
43 |
| - cwd: __dirname, // Directory from where you want to run the command |
| 43 | +test('should print colored greetings', async () => { |
| 44 | + const commandInterface = createCommandInterface('node ./graphic-print.js', { |
| 45 | + cwd: __dirname, // considering, the test file is in the same directory as the cli file |
44 | 46 | });
|
45 | 47 | await commandInterface.type('Saurabh\n');
|
46 | 48 | const terminal = await commandInterface.getOutput();
|
47 |
| - expect(terminal.stringOutput).toBe("What's your name?Hi, Saurabh!"); |
| 49 | + |
| 50 | + // ANSI Escape codes are tokenized into readable text token in tokenizedOutput |
| 51 | + // Helpful when libraries like inquirer or prompts add ansi-escape codes. |
| 52 | + expect(terminal.tokenizedOutput).toBe( |
| 53 | + "What's your name?Hi, [BOLD_START][RED_START]Saurabh[COLOR_END][BOLD_END]!" |
| 54 | + ); |
| 55 | + |
| 56 | + // ANSI Escape codes are not tokenized. |
| 57 | + expect(terminal.rawOutput).toBe( |
| 58 | + `What's your name?Hi, \x1B[1m\x1B[31mSaurabh\x1B[39m\x1B[22m!` |
| 59 | + ); |
| 60 | + |
| 61 | + // ANSI Escape codes are removed |
| 62 | + expect(terminal.stringOutput).toBe(`What's your name?Hi, Saurabh!`); |
48 | 63 | });
|
| 64 | + |
49 | 65 | ```
|
50 | 66 |
|
51 |
| -The code that we're testing- |
| 67 | +<details> |
| 68 | +<summary>Code of the CLI that we're testing in above snippet</summary> |
| 69 | + |
52 | 70 | ```js
|
53 |
| -// hello-world.js |
| 71 | +// colored-greeting.js |
54 | 72 | const readline = require('readline').createInterface({
|
55 | 73 | input: process.stdin,
|
56 |
| - output: process.stdout, |
| 74 | + output: process.stdout |
57 | 75 | });
|
58 | 76 |
|
| 77 | +const bold = (str) => `\x1b[1m${str}\x1b[22m`; |
| 78 | +const red = (str) => `\x1b[31m${str}\x1b[39m`; |
| 79 | + |
59 | 80 | readline.question(`What's your name?`, (name) => {
|
60 |
| - console.log(`Hi, ${name}!`); |
| 81 | + console.log(`Hi, ${bold(red('Saurabh'))}!`); |
61 | 82 | readline.close();
|
62 | 83 | });
|
63 | 84 |
|
64 | 85 | ```
|
65 | 86 |
|
66 |
| -### Eg 2: Tokenized Output |
| 87 | +</details> |
67 | 88 |
|
68 |
| -Check out [this example of StackBlitz](https://stackblitz.com/edit/node-kfod5b?file=examples%2Fcolored-output%2Fcolored-output.test.js) |
69 | 89 |
|
70 |
| -Sometimes you may want to test if the output has correct color and graphics. You can use the `.tokenizedOutput` method to get tokens in the output. |
71 | 90 |
|
72 |
| -Check out [list of tokens](https://github.com/saurabhdaware/cli-testing-tool/blob/18e1e12d86cec7b429f949cdd571b13b64fd4747/lib/cli-ansi-parser.js#L28) that library outputs. |
| 91 | +## Options |
73 | 92 |
|
74 |
| -```js |
75 |
| -// colored-output.test.js |
76 |
| -const { createCommandInterface } = require('cli-testing-tool'); |
| 93 | +You can pass options as 2nd param to `createCommandInterface`. |
77 | 94 |
|
78 |
| -test('should have bold red text', async () => { |
79 |
| - const commandInterface = createCommandInterface('node ./colored-output.js', { |
80 |
| - cwd: __dirname, |
81 |
| - }); |
82 |
| - const terminal = await commandInterface.getOutput(); |
83 |
| - expect(terminal.tokenizedOutput).toBe("This has a [BOLD_START][RED_START]red and bold[COLOR_END][BOLD_END] text."); |
84 |
| -}); |
| 95 | +The default options are: |
| 96 | +```js |
| 97 | +const defaultOptions = { |
| 98 | + typeDelay: 100, // number. delay between each `.type()` call |
| 99 | + logData: false, // boolean. if true, logs the command data on terminal |
| 100 | + logError: true, // boolean. if false, won't add command errors on terminal |
| 101 | + cwd: process.cwd(), // string. working directory from where your simulated command is executed |
| 102 | + env: undefined // object | undefined. environment variables object if there are any |
| 103 | +}; |
85 | 104 | ```
|
86 | 105 |
|
87 |
| -[More Examples on Stackblitz](https://stackblitz.com/edit/node-kfod5b?file=examples%2Fprompts%2Fprompts.test.js) |
88 |
| - |
| 106 | +---- |
89 | 107 |
|
90 | 108 | Big Shoutout to
|
91 | 109 | - [@fnky](https://github.com/fnky) for the [list of all ansi escape codes](https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797).
|
92 |
| -- [@netzkolchose](https://github.com/netzkolchose) for [node-ansiterminal](https://github.com/netzkolchose/node-ansiterminal) library. |
| 110 | + |
| 111 | +- [@netzkolchose](https://github.com/netzkolchose) for [node-ansiterminal](https://github.com/netzkolchose/node-ansiterminal) library. |
0 commit comments