@inquirer/testing 0.0.5-alpha.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/README.md +61 -0
- package/dist/cjs/index.js +88 -0
- package/dist/{index.d.ts → cjs/types/index.d.mts} +4 -1
- package/dist/esm/index.mjs +59 -0
- package/dist/esm/types/index.d.mts +14 -0
- package/package.json +37 -16
- package/dist/index.js +0 -50
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Copyright (c) 2022 Simon Boudrias
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person
|
|
4
|
+
obtaining a copy of this software and associated documentation
|
|
5
|
+
files (the "Software"), to deal in the Software without
|
|
6
|
+
restriction, including without limitation the rights to use,
|
|
7
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
copies of the Software, and to permit persons to whom the
|
|
9
|
+
Software is furnished to do so, subject to the following
|
|
10
|
+
conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be
|
|
13
|
+
included in all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
16
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
17
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
18
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
19
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
20
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
21
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# `@inquirer/testing`
|
|
2
|
+
|
|
3
|
+
The `@inquirer/testing` package is Inquirer's answer to testing prompts [built with `@inquirer/core`](https://github.com/SBoudrias/Inquirer.js/tree/master/packages/core).
|
|
4
|
+
|
|
5
|
+
# Installation
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install @inquirer/testing
|
|
9
|
+
|
|
10
|
+
yarn add @inquirer/testing
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
# Example
|
|
14
|
+
|
|
15
|
+
Here's an example of a test running with Jest (though `@inquirer/testing` will work with any runners).
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { render } from '@inquirer/testing';
|
|
19
|
+
import input from './src/index.mjs';
|
|
20
|
+
|
|
21
|
+
describe('input prompt', () => {
|
|
22
|
+
it('handle simple use case', async () => {
|
|
23
|
+
const { answer, events, getScreen } = await render(input, {
|
|
24
|
+
message: 'What is your name',
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
expect(getScreen()).toMatchInlineSnapshot(`"? What is your name"`);
|
|
28
|
+
|
|
29
|
+
events.type('J');
|
|
30
|
+
expect(getScreen()).toMatchInlineSnapshot(`"? What is your name J"`);
|
|
31
|
+
|
|
32
|
+
events.type('ohn');
|
|
33
|
+
events.keypress('enter');
|
|
34
|
+
|
|
35
|
+
await expect(answer).resolves.toEqual('John');
|
|
36
|
+
expect(getScreen()).toMatchInlineSnapshot(`"? What is your name John"`);
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
# Usage
|
|
42
|
+
|
|
43
|
+
The core utility of `@inquirer/testing` is the `render()` function. This `render` function will create and instrument a command line like interface.
|
|
44
|
+
|
|
45
|
+
`render` takes 2 arguments:
|
|
46
|
+
|
|
47
|
+
1. The Inquirer prompt to test (the return value of `createPrompt()`)
|
|
48
|
+
2. The prompt configuration (the first prompt argument)
|
|
49
|
+
|
|
50
|
+
`render` then returns a promise that will resolve once the prompt is rendered and the test environment up and running. This promise returns the utilities we'll use to interact with our tests:
|
|
51
|
+
|
|
52
|
+
1. `answer` (`Promise`) This is the promise that'll be resolved once an answer is provided and valid.
|
|
53
|
+
2. `getScreen` (`({ raw: boolean }) => string`) This function returns the state of what is printed on the command line screen at any given time. You can use its return value to validate your prompt is properly rendered. By default this function will strip the ANSI codes (used for colors.)
|
|
54
|
+
3. `events` (`{ keypress: (name) => void, type: (string) => void }`) Is the utilities allowing you to interact with the prompt. Use it to trigger keypress events, or typing any input.
|
|
55
|
+
|
|
56
|
+
You can refer to [the `@inquirer/input` prompt test suite](https://github.com/SBoudrias/Inquirer.js/blob/master/packages/input/test.mts) as a practical example.
|
|
57
|
+
|
|
58
|
+
# License
|
|
59
|
+
|
|
60
|
+
Copyright (c) 2022 Simon Boudrias (twitter: [@vaxilart](https://twitter.com/Vaxilart))
|
|
61
|
+
Licensed under the MIT license.
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
12
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
13
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
14
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
15
|
+
};
|
|
16
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
17
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
18
|
+
};
|
|
19
|
+
var _BufferedStream__chunks;
|
|
20
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
21
|
+
exports.render = exports.log = void 0;
|
|
22
|
+
const mute_stream_1 = __importDefault(require("mute-stream"));
|
|
23
|
+
const strip_ansi_1 = __importDefault(require("strip-ansi"));
|
|
24
|
+
const node_stream_1 = require("node:stream");
|
|
25
|
+
const logStore = [];
|
|
26
|
+
class BufferedStream extends node_stream_1.Stream.Writable {
|
|
27
|
+
constructor() {
|
|
28
|
+
super(...arguments);
|
|
29
|
+
_BufferedStream__chunks.set(this, []);
|
|
30
|
+
}
|
|
31
|
+
_write(chunk, _encoding, callback) {
|
|
32
|
+
const str = chunk.toString();
|
|
33
|
+
// Stripping the ANSI codes here because Inquirer will push commands ANSI (like cursor move.)
|
|
34
|
+
// This is probably fine since we don't care about those for testing; but this could become
|
|
35
|
+
// an issue if we ever want to test for those.
|
|
36
|
+
if ((0, strip_ansi_1.default)(str).trim().length > 0) {
|
|
37
|
+
__classPrivateFieldGet(this, _BufferedStream__chunks, "f").push(chunk.toString());
|
|
38
|
+
}
|
|
39
|
+
callback();
|
|
40
|
+
}
|
|
41
|
+
getLastChunk() {
|
|
42
|
+
const lastChunk = __classPrivateFieldGet(this, _BufferedStream__chunks, "f")[__classPrivateFieldGet(this, _BufferedStream__chunks, "f").length - 1];
|
|
43
|
+
return lastChunk !== null && lastChunk !== void 0 ? lastChunk : '';
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
_BufferedStream__chunks = new WeakMap();
|
|
47
|
+
beforeEach(() => {
|
|
48
|
+
logStore.length = 0;
|
|
49
|
+
});
|
|
50
|
+
afterEach(() => {
|
|
51
|
+
logStore.forEach((...line) => console.log(...line));
|
|
52
|
+
});
|
|
53
|
+
function log(...line) {
|
|
54
|
+
logStore.push(line);
|
|
55
|
+
}
|
|
56
|
+
exports.log = log;
|
|
57
|
+
function render(prompt, props, options) {
|
|
58
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
59
|
+
const input = new mute_stream_1.default();
|
|
60
|
+
input.unmute();
|
|
61
|
+
const output = new BufferedStream();
|
|
62
|
+
const answer = prompt(props, Object.assign({ input, output }, options));
|
|
63
|
+
// Wait for event listeners to be ready
|
|
64
|
+
yield Promise.resolve();
|
|
65
|
+
yield Promise.resolve();
|
|
66
|
+
const events = {
|
|
67
|
+
keypress(name) {
|
|
68
|
+
input.emit('keypress', null, { name });
|
|
69
|
+
},
|
|
70
|
+
type(text) {
|
|
71
|
+
input.write(text);
|
|
72
|
+
for (const char of text) {
|
|
73
|
+
input.emit('keypress', null, { name: char });
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
return {
|
|
78
|
+
answer,
|
|
79
|
+
input,
|
|
80
|
+
events,
|
|
81
|
+
getScreen({ raw } = {}) {
|
|
82
|
+
const lastScreen = output.getLastChunk();
|
|
83
|
+
return raw ? lastScreen : (0, strip_ansi_1.default)(lastScreen).trim();
|
|
84
|
+
},
|
|
85
|
+
};
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
exports.render = render;
|
|
@@ -6,6 +6,9 @@ export declare function render<TestedPrompt extends Prompt<any, any>>(prompt: Te
|
|
|
6
6
|
input: MuteStream;
|
|
7
7
|
events: {
|
|
8
8
|
keypress(name: string): void;
|
|
9
|
+
type(text: string): void;
|
|
9
10
|
};
|
|
10
|
-
getScreen(
|
|
11
|
+
getScreen({ raw }?: {
|
|
12
|
+
raw?: boolean | undefined;
|
|
13
|
+
}): string;
|
|
11
14
|
}>;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import MuteStream from 'mute-stream';
|
|
2
|
+
import stripAnsi from 'strip-ansi';
|
|
3
|
+
import { Stream } from 'node:stream';
|
|
4
|
+
const logStore = [];
|
|
5
|
+
class BufferedStream extends Stream.Writable {
|
|
6
|
+
#_chunks = [];
|
|
7
|
+
_write(chunk, _encoding, callback) {
|
|
8
|
+
const str = chunk.toString();
|
|
9
|
+
// Stripping the ANSI codes here because Inquirer will push commands ANSI (like cursor move.)
|
|
10
|
+
// This is probably fine since we don't care about those for testing; but this could become
|
|
11
|
+
// an issue if we ever want to test for those.
|
|
12
|
+
if (stripAnsi(str).trim().length > 0) {
|
|
13
|
+
this.#_chunks.push(chunk.toString());
|
|
14
|
+
}
|
|
15
|
+
callback();
|
|
16
|
+
}
|
|
17
|
+
getLastChunk() {
|
|
18
|
+
const lastChunk = this.#_chunks[this.#_chunks.length - 1];
|
|
19
|
+
return lastChunk ?? '';
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
beforeEach(() => {
|
|
23
|
+
logStore.length = 0;
|
|
24
|
+
});
|
|
25
|
+
afterEach(() => {
|
|
26
|
+
logStore.forEach((...line) => console.log(...line));
|
|
27
|
+
});
|
|
28
|
+
export function log(...line) {
|
|
29
|
+
logStore.push(line);
|
|
30
|
+
}
|
|
31
|
+
export async function render(prompt, props, options) {
|
|
32
|
+
const input = new MuteStream();
|
|
33
|
+
input.unmute();
|
|
34
|
+
const output = new BufferedStream();
|
|
35
|
+
const answer = prompt(props, { input, output, ...options });
|
|
36
|
+
// Wait for event listeners to be ready
|
|
37
|
+
await Promise.resolve();
|
|
38
|
+
await Promise.resolve();
|
|
39
|
+
const events = {
|
|
40
|
+
keypress(name) {
|
|
41
|
+
input.emit('keypress', null, { name });
|
|
42
|
+
},
|
|
43
|
+
type(text) {
|
|
44
|
+
input.write(text);
|
|
45
|
+
for (const char of text) {
|
|
46
|
+
input.emit('keypress', null, { name: char });
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
return {
|
|
51
|
+
answer,
|
|
52
|
+
input,
|
|
53
|
+
events,
|
|
54
|
+
getScreen({ raw } = {}) {
|
|
55
|
+
const lastScreen = output.getLastChunk();
|
|
56
|
+
return raw ? lastScreen : stripAnsi(lastScreen).trim();
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import MuteStream from 'mute-stream';
|
|
2
|
+
import type { Prompt } from '@inquirer/type';
|
|
3
|
+
export declare function log(...line: Parameters<typeof console.log>): void;
|
|
4
|
+
export declare function render<TestedPrompt extends Prompt<any, any>>(prompt: TestedPrompt, props: Parameters<TestedPrompt>[0], options?: Parameters<TestedPrompt>[1]): Promise<{
|
|
5
|
+
answer: Promise<any>;
|
|
6
|
+
input: MuteStream;
|
|
7
|
+
events: {
|
|
8
|
+
keypress(name: string): void;
|
|
9
|
+
type(text: string): void;
|
|
10
|
+
};
|
|
11
|
+
getScreen({ raw }?: {
|
|
12
|
+
raw?: boolean | undefined;
|
|
13
|
+
}): string;
|
|
14
|
+
}>;
|
package/package.json
CHANGED
|
@@ -1,21 +1,25 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inquirer/testing",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"engines": {
|
|
5
|
+
"node": ">=14.18.0"
|
|
6
|
+
},
|
|
5
7
|
"description": "Inquirer testing utilities",
|
|
6
|
-
"main": "dist/index.js",
|
|
7
|
-
"typings": "dist/index.d.
|
|
8
|
+
"main": "./dist/cjs/index.js",
|
|
9
|
+
"typings": "./dist/cjs/types/index.d.mts",
|
|
8
10
|
"files": [
|
|
9
|
-
"dist
|
|
11
|
+
"dist/**/*"
|
|
10
12
|
],
|
|
11
|
-
"repository":
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "https://github.com/SBoudrias/Inquirer.js.git"
|
|
16
|
+
},
|
|
12
17
|
"keywords": [
|
|
13
18
|
"answer",
|
|
14
19
|
"answers",
|
|
15
20
|
"ask",
|
|
16
21
|
"base",
|
|
17
22
|
"cli",
|
|
18
|
-
"cli",
|
|
19
23
|
"command",
|
|
20
24
|
"command-line",
|
|
21
25
|
"confirm",
|
|
@@ -43,24 +47,41 @@
|
|
|
43
47
|
"stdin",
|
|
44
48
|
"stdout",
|
|
45
49
|
"terminal",
|
|
46
|
-
"test",
|
|
47
|
-
"testing",
|
|
48
50
|
"tty",
|
|
49
51
|
"ui",
|
|
50
|
-
"unit-test",
|
|
51
52
|
"yeoman",
|
|
52
53
|
"yo",
|
|
53
|
-
"zsh"
|
|
54
|
+
"zsh",
|
|
55
|
+
"test",
|
|
56
|
+
"testing",
|
|
57
|
+
"unit-test"
|
|
54
58
|
],
|
|
55
|
-
"author": "Simon Boudrias",
|
|
59
|
+
"author": "Simon Boudrias <admin@simonboudrias.com>",
|
|
56
60
|
"license": "MIT",
|
|
57
61
|
"homepage": "https://github.com/SBoudrias/Inquirer.js/blob/master/packages/testing/README.md",
|
|
58
62
|
"dependencies": {
|
|
59
|
-
"@inquirer/type": "^
|
|
60
|
-
"mute-stream": "^0.0
|
|
63
|
+
"@inquirer/type": "^1.0.0",
|
|
64
|
+
"mute-stream": "^1.0.0",
|
|
61
65
|
"strip-ansi": "^7.0.1"
|
|
62
66
|
},
|
|
63
67
|
"scripts": {
|
|
64
|
-
"tsc": "tsc"
|
|
65
|
-
|
|
68
|
+
"tsc": "yarn run clean && yarn run tsc:esm && yarn run tsc:cjs",
|
|
69
|
+
"clean": "rm -rf dist",
|
|
70
|
+
"tsc:esm": "tsc -p ./tsconfig.esm.json",
|
|
71
|
+
"tsc:cjs": "tsc -p ./tsconfig.cjs.json && yarn run fix-ext",
|
|
72
|
+
"fix-ext": "ts-node ../../tools/rename-ext.ts"
|
|
73
|
+
},
|
|
74
|
+
"exports": {
|
|
75
|
+
".": {
|
|
76
|
+
"import": {
|
|
77
|
+
"types": "./dist/esm/types/index.d.mts",
|
|
78
|
+
"default": "./dist/esm/index.mjs"
|
|
79
|
+
},
|
|
80
|
+
"require": {
|
|
81
|
+
"types": "./dist/cjs/types/index.d.mts",
|
|
82
|
+
"default": "./dist/cjs/index.js"
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
},
|
|
86
|
+
"gitHead": "e93c4cb14f0c35ebd1efb065776b90f48899b075"
|
|
66
87
|
}
|
package/dist/index.js
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import MuteStream from 'mute-stream';
|
|
2
|
-
import stripAnsi from 'strip-ansi';
|
|
3
|
-
import { Stream } from 'node:stream';
|
|
4
|
-
const logStore = [];
|
|
5
|
-
beforeEach(() => {
|
|
6
|
-
logStore.length = 0;
|
|
7
|
-
});
|
|
8
|
-
afterEach(() => {
|
|
9
|
-
logStore.forEach((...line) => console.log(...line));
|
|
10
|
-
});
|
|
11
|
-
export function log(...line) {
|
|
12
|
-
logStore.push(line);
|
|
13
|
-
}
|
|
14
|
-
export async function render(prompt, props, options) {
|
|
15
|
-
const input = new MuteStream();
|
|
16
|
-
const buffer = [];
|
|
17
|
-
const data = [];
|
|
18
|
-
const output = new Stream.Writable({
|
|
19
|
-
write(chunk, _encoding, next) {
|
|
20
|
-
buffer.push(chunk.toString());
|
|
21
|
-
next();
|
|
22
|
-
},
|
|
23
|
-
});
|
|
24
|
-
const processScreen = () => {
|
|
25
|
-
if (buffer.length > 0) {
|
|
26
|
-
const prevScreen = buffer.join('');
|
|
27
|
-
data.push(stripAnsi(prevScreen));
|
|
28
|
-
buffer.length = 0;
|
|
29
|
-
}
|
|
30
|
-
};
|
|
31
|
-
const answer = prompt(props, { input, output, ...options });
|
|
32
|
-
// Wait for event listeners to be ready
|
|
33
|
-
await Promise.resolve();
|
|
34
|
-
await Promise.resolve();
|
|
35
|
-
const events = {
|
|
36
|
-
keypress(name) {
|
|
37
|
-
processScreen();
|
|
38
|
-
input.emit('keypress', null, { name });
|
|
39
|
-
},
|
|
40
|
-
};
|
|
41
|
-
return {
|
|
42
|
-
answer,
|
|
43
|
-
input,
|
|
44
|
-
events,
|
|
45
|
-
getScreen() {
|
|
46
|
-
processScreen();
|
|
47
|
-
return data[data.length - 1] ?? '';
|
|
48
|
-
},
|
|
49
|
-
};
|
|
50
|
-
}
|