@inquirer/testing 0.1.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/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.
package/dist/cjs/index.js CHANGED
@@ -8,15 +8,42 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
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
+ };
11
16
  var __importDefault = (this && this.__importDefault) || function (mod) {
12
17
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
18
  };
19
+ var _BufferedStream__chunks;
14
20
  Object.defineProperty(exports, "__esModule", { value: true });
15
21
  exports.render = exports.log = void 0;
16
22
  const mute_stream_1 = __importDefault(require("mute-stream"));
17
23
  const strip_ansi_1 = __importDefault(require("strip-ansi"));
18
24
  const node_stream_1 = require("node:stream");
19
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();
20
47
  beforeEach(() => {
21
48
  logStore.length = 0;
22
49
  });
@@ -30,39 +57,30 @@ exports.log = log;
30
57
  function render(prompt, props, options) {
31
58
  return __awaiter(this, void 0, void 0, function* () {
32
59
  const input = new mute_stream_1.default();
33
- const buffer = [];
34
- const data = [];
35
- const output = new node_stream_1.Stream.Writable({
36
- write(chunk, _encoding, next) {
37
- buffer.push(chunk.toString());
38
- next();
39
- },
40
- });
41
- const processScreen = () => {
42
- if (buffer.length > 0) {
43
- const prevScreen = buffer.join('');
44
- data.push((0, strip_ansi_1.default)(prevScreen));
45
- buffer.length = 0;
46
- }
47
- };
60
+ input.unmute();
61
+ const output = new BufferedStream();
48
62
  const answer = prompt(props, Object.assign({ input, output }, options));
49
63
  // Wait for event listeners to be ready
50
64
  yield Promise.resolve();
51
65
  yield Promise.resolve();
52
66
  const events = {
53
67
  keypress(name) {
54
- processScreen();
55
68
  input.emit('keypress', null, { name });
56
69
  },
70
+ type(text) {
71
+ input.write(text);
72
+ for (const char of text) {
73
+ input.emit('keypress', null, { name: char });
74
+ }
75
+ },
57
76
  };
58
77
  return {
59
78
  answer,
60
79
  input,
61
80
  events,
62
- getScreen() {
63
- var _a;
64
- processScreen();
65
- return (_a = data[data.length - 1]) !== null && _a !== void 0 ? _a : '';
81
+ getScreen({ raw } = {}) {
82
+ const lastScreen = output.getLastChunk();
83
+ return raw ? lastScreen : (0, strip_ansi_1.default)(lastScreen).trim();
66
84
  },
67
85
  };
68
86
  });
@@ -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(): string;
11
+ getScreen({ raw }?: {
12
+ raw?: boolean | undefined;
13
+ }): string;
11
14
  }>;
@@ -2,6 +2,23 @@ import MuteStream from 'mute-stream';
2
2
  import stripAnsi from 'strip-ansi';
3
3
  import { Stream } from 'node:stream';
4
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
+ }
5
22
  beforeEach(() => {
6
23
  logStore.length = 0;
7
24
  });
@@ -13,38 +30,30 @@ export function log(...line) {
13
30
  }
14
31
  export async function render(prompt, props, options) {
15
32
  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
- };
33
+ input.unmute();
34
+ const output = new BufferedStream();
31
35
  const answer = prompt(props, { input, output, ...options });
32
36
  // Wait for event listeners to be ready
33
37
  await Promise.resolve();
34
38
  await Promise.resolve();
35
39
  const events = {
36
40
  keypress(name) {
37
- processScreen();
38
41
  input.emit('keypress', null, { name });
39
42
  },
43
+ type(text) {
44
+ input.write(text);
45
+ for (const char of text) {
46
+ input.emit('keypress', null, { name: char });
47
+ }
48
+ },
40
49
  };
41
50
  return {
42
51
  answer,
43
52
  input,
44
53
  events,
45
- getScreen() {
46
- processScreen();
47
- return data[data.length - 1] ?? '';
54
+ getScreen({ raw } = {}) {
55
+ const lastScreen = output.getLastChunk();
56
+ return raw ? lastScreen : stripAnsi(lastScreen).trim();
48
57
  },
49
58
  };
50
59
  }
@@ -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(): string;
11
+ getScreen({ raw }?: {
12
+ raw?: boolean | undefined;
13
+ }): string;
11
14
  }>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inquirer/testing",
3
- "version": "0.1.0",
3
+ "version": "1.0.0",
4
4
  "engines": {
5
5
  "node": ">=14.18.0"
6
6
  },
@@ -60,7 +60,7 @@
60
60
  "license": "MIT",
61
61
  "homepage": "https://github.com/SBoudrias/Inquirer.js/blob/master/packages/testing/README.md",
62
62
  "dependencies": {
63
- "@inquirer/type": "^0.1.0",
63
+ "@inquirer/type": "^1.0.0",
64
64
  "mute-stream": "^1.0.0",
65
65
  "strip-ansi": "^7.0.1"
66
66
  },
@@ -83,5 +83,5 @@
83
83
  }
84
84
  }
85
85
  },
86
- "gitHead": "bd58130dd8204945a31b062070b829003b8385fc"
86
+ "gitHead": "e93c4cb14f0c35ebd1efb065776b90f48899b075"
87
87
  }