Skip to content

Commit 525ec80

Browse files
feat(v0.3.0): continue typing after getOutput (#5)
* move init code to function * write example test for multiple type calls * refactor: remove unused code * feat: remove test word from readme * 0.3.0
1 parent 032dc09 commit 525ec80

File tree

9 files changed

+91
-37
lines changed

9 files changed

+91
-37
lines changed

.changeset/smooth-lamps-divide.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules
2-
.vscode
2+
.vscode
3+
playground.js

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# CLI Testing Tool
22

3-
// test
4-
53
A testing library that allows you to test input and outputs of your CLI command.
64

75
*Note: This is WIP but it should be ready enough for most common CLI use-cases I can think of*

cli-examples/__tests__/select.test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const path = require('path');
2+
const { createCommandInterface } = require('../../lib');
3+
const {
4+
FORWARD_ARROW,
5+
THREE_DOTS,
6+
SELECT_ICON
7+
} = require('../test-utils/icons');
8+
9+
test('select', async () => {
10+
const commandInterface = createCommandInterface('node ./select.js', {
11+
cwd: path.join(__dirname, '..'),
12+
typeDelay: 500
13+
});
14+
const terminalBeforeDownArrow = await commandInterface.getOutput();
15+
expect(terminalBeforeDownArrow.stringOutput).toMatch(
16+
// eslint-disable-next-line max-len
17+
`? test:${THREE_DOTS} \n? test:${FORWARD_ARROW} \n${SELECT_ICON}test 1\ntest 2`
18+
);
19+
expect(terminalBeforeDownArrow.stringOutput).toMatch('');
20+
// move to next item
21+
await commandInterface.keys.arrowDown();
22+
const terminalAfterDownArrow = await commandInterface.getOutput();
23+
expect(terminalAfterDownArrow.stringOutput).toMatch(
24+
// eslint-disable-next-line max-len
25+
`? test:${THREE_DOTS} \n? test:${FORWARD_ARROW} \n${SELECT_ICON}test 1\n? test:${FORWARD_ARROW} \ntest 1\n${SELECT_ICON}test 2`
26+
);
27+
expect(1).toBe(1);
28+
});

cli-examples/select.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const prompts = require('prompts');
2+
3+
const questions = [
4+
{
5+
type: 'autocomplete',
6+
message: `test: `,
7+
name: 'selectedProject',
8+
choices: [
9+
{
10+
title: 'test 1',
11+
value: 'test1'
12+
},
13+
{
14+
title: 'test 2',
15+
value: 'test2'
16+
}
17+
],
18+
limit: 4
19+
}
20+
];
21+
22+
(async () => {
23+
const { selectedProject } = await prompts(questions);
24+
console.log(selectedProject);
25+
})();

cli-examples/test-utils/icons.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ const win = process.platform === 'win32';
44
module.exports = {
55
FORWARD_ARROW: win ? '»' : '›',
66
CHECK_MARK: win ? '√' : '✔',
7-
THREE_DOTS: win ? '...' : '…'
7+
THREE_DOTS: win ? '...' : '…',
8+
SELECT_ICON: win ? '>' : '❯'
89
};

lib/cli-testing-tool.js

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -38,40 +38,46 @@ const createCommandInterface = (commandString, userOptions = {}) => {
3838
const options = { ...defaultOptions, ...userOptions };
3939

4040
const commandArgs = commandString.split(' ');
41-
const command = spawn(commandArgs[0], commandArgs.slice(1), {
42-
detached: true,
43-
stdio: 'pipe',
44-
cwd: options.cwd,
45-
env: options.env
46-
});
4741

4842
let outputs = '';
4943
let isFinishTypingCalled = false;
5044

51-
command.stdout.on('data', (data) => {
52-
if (options.logData) {
53-
console.log(data.toString());
54-
}
55-
outputs += data.toString();
56-
});
45+
const initCommandListeners = () => {
46+
outputs = '';
47+
isFinishTypingCalled = false;
48+
const commandInterface = spawn(commandArgs[0], commandArgs.slice(1), {
49+
detached: true,
50+
stdio: 'pipe',
51+
cwd: options.cwd,
52+
env: options.env
53+
});
5754

58-
command.stderr.on('data', (error) => {
59-
if (options.logData) {
60-
console.error(error.toString());
61-
}
62-
outputs += error.toString();
63-
});
55+
commandInterface.stdout.on('data', (data) => {
56+
if (options.logData) {
57+
console.log(data.toString());
58+
}
59+
outputs += data.toString();
60+
});
61+
62+
commandInterface.stderr.on('data', (error) => {
63+
if (options.logData) {
64+
console.error(error.toString());
65+
}
66+
outputs += error.toString();
67+
});
68+
69+
commandInterface.on('error', (error) => {
70+
throw error;
71+
});
72+
73+
return commandInterface;
74+
};
6475

65-
command.on('error', (error) => {
66-
throw error;
67-
});
76+
let command = initCommandListeners();
6877

6978
const type = async (text) => {
7079
if (isFinishTypingCalled) {
71-
throw new Error(
72-
// eslint-disable-next-line max-len
73-
'[cli-testing-tool]: `type` cannot be called after `getOutput` or `finishTyping`'
74-
);
80+
command = initCommandListeners();
7581
}
7682

7783
await wait(options.typeDelay);

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cli-testing-tool",
3-
"version": "0.2.0",
3+
"version": "0.3.0",
44
"description": "",
55
"main": "lib/index.js",
66
"directories": {

0 commit comments

Comments
 (0)