-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# `@inquirer/testing` | ||
|
||
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). | ||
|
||
# Installation | ||
|
||
```sh | ||
npm install @inquirer/testing | ||
|
||
yarn add @inquirer/testing | ||
``` | ||
|
||
# Example | ||
|
||
Here's an example of a test running with Jest (though `@inquirer/testing` will work with any runners). | ||
|
||
```ts | ||
import { render } from '@inquirer/testing'; | ||
import input from './src/index.mjs'; | ||
|
||
describe('input prompt', () => { | ||
it('handle simple use case', async () => { | ||
const { answer, events, getScreen } = await render(input, { | ||
message: 'What is your name', | ||
}); | ||
|
||
expect(getScreen()).toMatchInlineSnapshot(`"? What is your name"`); | ||
|
||
events.type('J'); | ||
expect(getScreen()).toMatchInlineSnapshot(`"? What is your name J"`); | ||
|
||
events.type('ohn'); | ||
events.keypress('enter'); | ||
|
||
await expect(answer).resolves.toEqual('John'); | ||
expect(getScreen()).toMatchInlineSnapshot(`"? What is your name John"`); | ||
}); | ||
}); | ||
``` | ||
|
||
# Usage | ||
|
||
The core utility of `@inquirer/testing` is the `render()` function. This `render` function will create and instrument a command line like interface. | ||
|
||
`render` takes 2 arguments: | ||
|
||
1. The Inquirer prompt to test (the return value of `createPrompt()`) | ||
2. The prompt configuration (the first prompt argument) | ||
|
||
`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: | ||
|
||
1. `answer` (`Promise`) This is the promise that'll be resolved once an answer is provided and valid. | ||
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.) | ||
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. | ||
|
||
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. | ||
|
||
# License | ||
|
||
Copyright (c) 2022 Simon Boudrias (twitter: [@vaxilart](https://twitter.com/Vaxilart)) | ||
Licensed under the MIT license. |