Skip to content
Utilities for testing Ink apps
Branch: master
Clone or download
Latest commit f77945d Mar 12, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.editorconfig
.gitattributes
.gitignore
.npmrc
.travis.yml Init Mar 6, 2019
index.js
license
package.json
readme.md
test.js

readme.md

ink-testing-library Build Status

Utilities for testing Ink apps

Install

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

Usage

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.

render(<MyApp/>);

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

lastFrame()

Type: function

Return the last rendered frame (output).

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

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

frames

Type: array

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

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

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

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

rerender(tree)

Type: function

tree

Type: ReactElement

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

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

unmount()

Type: function

Unmount current component.

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

stdin

Type: object

write()

Type: function

Write data to current component's stdin stream.

import {StdinContext, Text} from 'ink';

class Test extends React.Component {
	render() {
		return <Text>Hello</Text>;
	}

	componentDidMount() {
		this.props.setRawMode(true);
		this.props.stdin.on('data', data => {
			console.log(data); //=> 'hello'
		});
	}
}

const tree = (
	<StdinContext.Consumer>
		{({stdin, setRawMode}) => (
			<Test stdin={stdin} setRawMode={setRawMode}/>
		)}
	</StdinContext.Consumer>
);

const {stdin} = render(tree);
stdin.write('hello');

License

MIT © Vadim Demedes

You can’t perform that action at this time.