# ink-testing-library

> Utilities for testing Ink apps

Latest version **4.0.0** (published 2024-05-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install ink-testing-library
pnpm add ink-testing-library
yarn add ink-testing-library
bun add ink-testing-library
```

## Health

**Score 28/100 (F)** — status: abandoned.

Positive: has types package; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 4.0.0 |
| Published | 2024-05-22 |
| First published | 2019-03-06 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/ink-testing-library) |
| Module format | ESM |
| Node | >=18 |
| Dependencies | 0 |
| Unpacked size | 10.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Vadim Demedes |
| Maintainers | vdemedes, sindresorhus |
| Keywords | ink, cli, test |

## Links

- npm: https://www.npmjs.com/package/ink-testing-library
- Repository: https://github.com/vadimdemedes/ink-testing-library
- Homepage: https://github.com/vadimdemedes/ink-testing-library#readme
- Issues: https://github.com/vadimdemedes/ink-testing-library/issues
- npm.io page: https://npm.io/package/ink-testing-library

## Alternatives

- [duck](https://npm.io/package/duck.md) — 4.2M weekly downloads
- [ava](https://npm.io/package/ava.md) — 560.2K weekly downloads
- [storybook-addon-module-mock](https://npm.io/package/storybook-addon-module-mock.md) — 71.7K weekly downloads
- [@ethereum-waffle/mock-contract](https://npm.io/package/@ethereum-waffle/mock-contract.md) — 40.0K weekly downloads
- [aws-elasticsearch-connector](https://npm.io/package/aws-elasticsearch-connector.md) — 37.4K weekly downloads

## Recent versions

- 4.0.0 (latest) — 2024-05-22
- 3.0.0 — 2023-03-01
- 2.1.0 — 2020-10-03
- 2.0.1 — 2020-07-19
- 2.0.0 — 2020-06-21
- 1.0.3 — 2020-01-12
- 1.0.2 — 2019-06-20
- 1.0.1 — 2019-03-13
- 1.0.0 — 2019-03-06
- 0.0.1 — 2019-03-06

## README

# ink-testing-library ![test](https://github.com/vadimdemedes/ink-testing-library/workflows/test/badge.svg)

> Utilities for testing [Ink](https://github.com/vadimdemedes/ink) apps

## Install

```
$ npm install --save-dev ink-testing-library
```

## Usage

```jsx
import React from 'react';
import {Text} from 'ink';
import {render} from 'ink-testing-library';

const Counter = ({count}) => <Text>Count: {count}</Text>;

const {lastFrame, rerender} = render(<Counter count={0}/>);
lastFrame() === 'Count: 0'; //=> true

rerender(<Counter count={1}/>);
lastFrame() === 'Count: 1'; //=> true
```

## API

### render(tree)

#### tree

Type: `ReactElement`

React component to render.

```jsx
render(<MyApp/>);
```

This function returns an object, which contains the following methods and properties.

#### lastFrame()

Type: `function`

Shortcut to [`stdout.lastFrame`](#lastframe-1).

#### frames

Type: `array`

Shortcut to [`stdout.frames`](#frames-1).

#### rerender(tree)

Type: `function`

##### tree

Type: `ReactElement`

Rerender root component with different props or replace with another component.

```jsx
const {rerender} = render(<OldApp/>);
rerender(<NewApp/>);
```

#### unmount()

Type: `function`

Unmount current component.

```jsx
const {unmount} = render(<Test/>);
unmount();
```

#### stdin

Type: `object`

##### write()

Type: `function`

Write data to current component's stdin stream.

```jsx
import {useInput, Text} from 'ink';

const Test = () => {
	useInput(input => {
		console.log(input);
		//=> 'hello'
	});

	return …;
};

const {stdin} = render(<Test/>);
stdin.write('hello');
```

#### stdout

Type: `object`

##### lastFrame()

Type: `function`

Return the last rendered frame (output) from stdout stream.

```jsx
const Test = () => <Text>Hello</Text>;

const {stdout} = render(<Test/>);
stdout.lastFrame(); //=> 'Hello'
```

##### frames

Type: `array`

Array of all rendered frames, where the last frame is also the last item in that array.

```jsx
const Counter = ({count}) => <Text>Count: {count}</Text>;

const {stdout, rerender} = render(<Counter count={0}/>);
rerender(<Counter count={1}/>);

console.log(stdout.frames); //=> ['Count: 0', 'Count: 1']
```

#### stderr

Type: `object`

##### lastFrame()

Type: `function`

Same as [`lastFrame`](#lastframe-1) in [`stdout`](#stdout), but for stderr stream.

##### frames

Type: `array`

Same as [`frames`](#frames-1) in [`stdout`](#stdout), but for stderr stream.

---
_Source: https://npm.io/package/ink-testing-library · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
