Utilities for testing Ink apps
$ npm install --save-dev ink-testing-library
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
Type: ReactElement
React component to render.
render(<MyApp/>);
This function returns an object, which contains the following methods and properties.
Type: function
Shortcut to stdout.lastFrame
.
Type: array
Shortcut to stdout.frames
.
Type: function
Type: ReactElement
Rerender root component with different props or replace with another component.
const {rerender} = render(<OldApp/>);
rerender(<NewApp/>);
Type: function
Unmount current component.
const {unmount} = render(<Test/>);
unmount();
Type: function
Arguments:
hook
(function
): Hook to render.options
(object
): Options object with the following properties:wrapper
(function
): Wrapper component to wrap the hook with.
Render a hook with an optional wrapper, and return the hook's return value. Works like react-testing-library's renderHook
.
const useCounter = () => {
const [count, setCount] = React.useState(0);
return {count, increment: () => setCount(count + 1)};
};
const {result} = renderHook(() => useCounter());
Type: function
Arguments:
condition
(function
): Function that throws an error if the condition is not met.timeout
(number
): Timeout in milliseconds.interval
(number
): Interval in milliseconds.
Wait for a condition to be met. Useful for waiting for side effects. The condition can be jest's expect
assertion. Works like react-testing-library's waitFor
.
await waitFor(() => expect(result.current.count).toBe(1));
Type: object
Type: function
Write data to current component's stdin stream.
import {useInput, Text} from 'ink';
const Test = () => {
useInput(input => {
console.log(input);
//=> 'hello'
});
return …;
};
const {stdin} = render(<Test/>);
stdin.write('hello');
Type: object
Type: function
Return the last rendered frame (output) from stdout stream.
const Test = () => <Text>Hello</Text>;
const {stdout} = render(<Test/>);
stdout.lastFrame(); //=> 'Hello'
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 {stdout, rerender} = render(<Counter count={0}/>);
rerender(<Counter count={1}/>);
console.log(stdout.frames); //=> ['Count: 0', 'Count: 1']
Type: object
Type: function
Same as lastFrame
in stdout
, but for stderr stream.
Type: array