├── .editorconfig ├── .gitattributes ├── .github └── workflows │ └── test.yml ├── .gitignore ├── .npmrc ├── license ├── package.json ├── readme.md ├── source └── index.ts ├── test └── test.tsx └── tsconfig.json /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: test 2 | on: [push, pull_request] 3 | 4 | jobs: 5 | test: 6 | name: Node.js ${{ matrix.node_version }} 7 | runs-on: ubuntu-latest 8 | strategy: 9 | matrix: 10 | node_version: [20, 18] 11 | steps: 12 | - uses: actions/checkout@v4 13 | - name: Use Node.js ${{ matrix.node_version }} 14 | uses: actions/setup-node@v4 15 | with: 16 | node-version: ${{ matrix.node_version }} 17 | - run: npm install 18 | - run: npm test 19 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | build 2 | node_modules 3 | yarn.lock 4 | .tsimp 5 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Vadym Demedes (github.com/vadimdemedes/ink-testing-library) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ink-testing-library", 3 | "version": "4.0.0", 4 | "description": "Utilities for testing Ink apps", 5 | "license": "MIT", 6 | "repository": "vadimdemedes/ink-testing-library", 7 | "author": { 8 | "name": "Vadim Demedes", 9 | "email": "vadimdemedes@hey.com", 10 | "url": "https://github.com/vadimdemedes" 11 | }, 12 | "type": "module", 13 | "exports": { 14 | "types": "./build/index.d.ts", 15 | "default": "./build/index.js" 16 | }, 17 | "engines": { 18 | "node": ">=18" 19 | }, 20 | "scripts": { 21 | "prepare": "tsc", 22 | "build": "tsc", 23 | "test": "tsc && xo && ava" 24 | }, 25 | "files": [ 26 | "build" 27 | ], 28 | "keywords": [ 29 | "ink", 30 | "cli", 31 | "test" 32 | ], 33 | "devDependencies": { 34 | "@sindresorhus/tsconfig": "^5.0.0", 35 | "@types/react": "^18.3.2", 36 | "@vdemedes/prettier-config": "^2.0.1", 37 | "ava": "^6.1.3", 38 | "delay": "^6.0.0", 39 | "eslint-config-xo-react": "^0.27.0", 40 | "eslint-plugin-react": "^7.34.1", 41 | "eslint-plugin-react-hooks": "^4.6.2", 42 | "ink": "^5.0.0", 43 | "prettier": "^3.2.5", 44 | "react": "^18.3.1", 45 | "tsimp": "^2.0.11", 46 | "typescript": "^5.4.5", 47 | "xo": "^0.58.0" 48 | }, 49 | "peerDependencies": { 50 | "@types/react": ">=18.0.0" 51 | }, 52 | "peerDependenciesMeta": { 53 | "@types/react": { 54 | "optional": true 55 | } 56 | }, 57 | "ava": { 58 | "extensions": { 59 | "ts": "module", 60 | "tsx": "module" 61 | }, 62 | "nodeArguments": [ 63 | "--import=tsimp" 64 | ] 65 | }, 66 | "xo": { 67 | "extends": "xo-react", 68 | "prettier": true, 69 | "rules": { 70 | "unicorn/prefer-event-target": "off" 71 | } 72 | }, 73 | "prettier": "@vdemedes/prettier-config" 74 | } 75 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # ink-testing-library ![test](https://github.com/vadimdemedes/ink-testing-library/workflows/test/badge.svg) 2 | 3 | > Utilities for testing [Ink](https://github.com/vadimdemedes/ink) apps 4 | 5 | ## Install 6 | 7 | ``` 8 | $ npm install --save-dev ink-testing-library 9 | ``` 10 | 11 | ## Usage 12 | 13 | ```jsx 14 | import React from 'react'; 15 | import {Text} from 'ink'; 16 | import {render} from 'ink-testing-library'; 17 | 18 | const Counter = ({count}) => Count: {count}; 19 | 20 | const {lastFrame, rerender} = render(); 21 | lastFrame() === 'Count: 0'; //=> true 22 | 23 | rerender(); 24 | lastFrame() === 'Count: 1'; //=> true 25 | ``` 26 | 27 | ## API 28 | 29 | ### render(tree) 30 | 31 | #### tree 32 | 33 | Type: `ReactElement` 34 | 35 | React component to render. 36 | 37 | ```jsx 38 | render(); 39 | ``` 40 | 41 | This function returns an object, which contains the following methods and properties. 42 | 43 | #### lastFrame() 44 | 45 | Type: `function` 46 | 47 | Shortcut to [`stdout.lastFrame`](#lastframe-1). 48 | 49 | #### frames 50 | 51 | Type: `array` 52 | 53 | Shortcut to [`stdout.frames`](#frames-1). 54 | 55 | #### rerender(tree) 56 | 57 | Type: `function` 58 | 59 | ##### tree 60 | 61 | Type: `ReactElement` 62 | 63 | Rerender root component with different props or replace with another component. 64 | 65 | ```jsx 66 | const {rerender} = render(); 67 | rerender(); 68 | ``` 69 | 70 | #### unmount() 71 | 72 | Type: `function` 73 | 74 | Unmount current component. 75 | 76 | ```jsx 77 | const {unmount} = render(); 78 | unmount(); 79 | ``` 80 | 81 | #### stdin 82 | 83 | Type: `object` 84 | 85 | ##### write() 86 | 87 | Type: `function` 88 | 89 | Write data to current component's stdin stream. 90 | 91 | ```jsx 92 | import {useInput, Text} from 'ink'; 93 | 94 | const Test = () => { 95 | useInput(input => { 96 | console.log(input); 97 | //=> 'hello' 98 | }); 99 | 100 | return …; 101 | }; 102 | 103 | const {stdin} = render(); 104 | stdin.write('hello'); 105 | ``` 106 | 107 | #### stdout 108 | 109 | Type: `object` 110 | 111 | ##### lastFrame() 112 | 113 | Type: `function` 114 | 115 | Return the last rendered frame (output) from stdout stream. 116 | 117 | ```jsx 118 | const Test = () => Hello; 119 | 120 | const {stdout} = render(); 121 | stdout.lastFrame(); //=> 'Hello' 122 | ``` 123 | 124 | ##### frames 125 | 126 | Type: `array` 127 | 128 | Array of all rendered frames, where the last frame is also the last item in that array. 129 | 130 | ```jsx 131 | const Counter = ({count}) => Count: {count}; 132 | 133 | const {stdout, rerender} = render(); 134 | rerender(); 135 | 136 | console.log(stdout.frames); //=> ['Count: 0', 'Count: 1'] 137 | ``` 138 | 139 | #### stderr 140 | 141 | Type: `object` 142 | 143 | ##### lastFrame() 144 | 145 | Type: `function` 146 | 147 | Same as [`lastFrame`](#lastframe-1) in [`stdout`](#stdout), but for stderr stream. 148 | 149 | ##### frames 150 | 151 | Type: `array` 152 | 153 | Same as [`frames`](#frames-1) in [`stdout`](#stdout), but for stderr stream. 154 | -------------------------------------------------------------------------------- /source/index.ts: -------------------------------------------------------------------------------- 1 | import {EventEmitter} from 'node:events'; 2 | import {render as inkRender, type Instance as InkInstance} from 'ink'; 3 | import type {ReactElement} from 'react'; 4 | 5 | class Stdout extends EventEmitter { 6 | get columns() { 7 | return 100; 8 | } 9 | 10 | readonly frames: string[] = []; 11 | private _lastFrame?: string; 12 | 13 | write = (frame: string) => { 14 | this.frames.push(frame); 15 | this._lastFrame = frame; 16 | }; 17 | 18 | lastFrame = () => this._lastFrame; 19 | } 20 | 21 | class Stderr extends EventEmitter { 22 | readonly frames: string[] = []; 23 | private _lastFrame?: string; 24 | 25 | write = (frame: string) => { 26 | this.frames.push(frame); 27 | this._lastFrame = frame; 28 | }; 29 | 30 | lastFrame = () => this._lastFrame; 31 | } 32 | 33 | class Stdin extends EventEmitter { 34 | // eslint-disable-next-line @typescript-eslint/naming-convention 35 | isTTY = true; 36 | data: string | null = null; // eslint-disable-line @typescript-eslint/ban-types 37 | constructor(options: {isTTY?: boolean} = {}) { 38 | super(); 39 | this.isTTY = options.isTTY ?? true; 40 | } 41 | 42 | write = (data: string) => { 43 | this.data = data; 44 | this.emit('readable'); 45 | this.emit('data', data); 46 | }; 47 | 48 | setEncoding() { 49 | // Do nothing 50 | } 51 | 52 | setRawMode() { 53 | // Do nothing 54 | } 55 | 56 | resume() { 57 | // Do nothing 58 | } 59 | 60 | pause() { 61 | // Do nothing 62 | } 63 | 64 | ref() { 65 | // Do nothing 66 | } 67 | 68 | unref() { 69 | // Do nothing 70 | } 71 | 72 | // eslint-disable-next-line @typescript-eslint/ban-types 73 | read: () => string | null = () => { 74 | const {data} = this; 75 | this.data = null; 76 | return data; 77 | }; 78 | } 79 | 80 | type Instance = { 81 | rerender: (tree: ReactElement) => void; 82 | unmount: () => void; 83 | cleanup: () => void; 84 | stdout: Stdout; 85 | stderr: Stderr; 86 | stdin: Stdin; 87 | frames: string[]; 88 | lastFrame: () => string | undefined; 89 | }; 90 | 91 | const instances: InkInstance[] = []; 92 | 93 | export const render = (tree: ReactElement): Instance => { 94 | const stdout = new Stdout(); 95 | const stderr = new Stderr(); 96 | const stdin = new Stdin(); 97 | 98 | const instance = inkRender(tree, { 99 | // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment 100 | stdout: stdout as any, 101 | // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment 102 | stderr: stderr as any, 103 | // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment 104 | stdin: stdin as any, 105 | debug: true, 106 | exitOnCtrlC: false, 107 | patchConsole: false, 108 | }); 109 | 110 | instances.push(instance); 111 | 112 | return { 113 | rerender: instance.rerender, 114 | unmount: instance.unmount, 115 | cleanup: instance.cleanup, 116 | stdout, 117 | stderr, 118 | stdin, 119 | frames: stdout.frames, 120 | lastFrame: stdout.lastFrame, 121 | }; 122 | }; 123 | 124 | export const cleanup = () => { 125 | for (const instance of instances) { 126 | instance.unmount(); 127 | instance.cleanup(); 128 | } 129 | }; 130 | -------------------------------------------------------------------------------- /test/test.tsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import test from 'ava'; 3 | import {Text, useStdin, useStderr} from 'ink'; 4 | import delay from 'delay'; 5 | import {render} from '../source/index.js'; 6 | 7 | test('render a single frame', t => { 8 | function Test() { 9 | return Hello World; 10 | } 11 | 12 | const {frames, lastFrame} = render(); 13 | 14 | t.is(lastFrame(), 'Hello World'); 15 | t.deepEqual(frames, ['Hello World']); 16 | }); 17 | 18 | test('render multiple frames', t => { 19 | function Counter({count}: {readonly count: number}) { 20 | return Count: {count}; 21 | } 22 | 23 | const {frames, lastFrame, rerender} = render(); 24 | 25 | t.is(lastFrame(), 'Count: 0'); 26 | t.deepEqual(frames, ['Count: 0']); 27 | 28 | rerender(); 29 | 30 | t.is(lastFrame(), 'Count: 1'); 31 | t.deepEqual(frames, ['Count: 0', 'Count: 1']); 32 | }); 33 | 34 | test('unmount class component', t => { 35 | let didMount = false; 36 | let didUnmount = false; 37 | 38 | class Test extends React.Component { 39 | override render() { 40 | return Hello World; 41 | } 42 | 43 | override componentDidMount() { 44 | didMount = true; 45 | } 46 | 47 | override componentWillUnmount() { 48 | didUnmount = true; 49 | } 50 | } 51 | 52 | const {lastFrame, unmount} = render(); 53 | 54 | t.is(lastFrame(), 'Hello World'); 55 | t.true(didMount); 56 | t.false(didUnmount); 57 | 58 | unmount(); 59 | 60 | t.true(didUnmount); 61 | }); 62 | 63 | test('unmount function component', t => { 64 | let didMount = false; 65 | let didUnmount = false; 66 | 67 | function Test() { 68 | React.useLayoutEffect(() => { 69 | didMount = true; 70 | 71 | return () => { 72 | didUnmount = true; 73 | }; 74 | }, []); 75 | 76 | return Hello World; 77 | } 78 | 79 | const {lastFrame, unmount} = render(); 80 | 81 | t.is(lastFrame(), 'Hello World'); 82 | t.true(didMount); 83 | t.false(didUnmount); 84 | 85 | unmount(); 86 | t.true(didUnmount); 87 | }); 88 | 89 | test('write to stdin', async t => { 90 | function Test() { 91 | const [input, setInput] = React.useState(''); 92 | const {stdin, setRawMode} = useStdin(); 93 | 94 | React.useEffect(() => { 95 | const handleData = (data: string) => { 96 | setInput(data); 97 | }; 98 | 99 | setRawMode(true); 100 | stdin.on('data', handleData); 101 | 102 | return () => { 103 | setRawMode(false); 104 | stdin.off('data', handleData); 105 | }; 106 | }, [stdin, setRawMode]); 107 | 108 | return {input}; 109 | } 110 | 111 | const {stdin, lastFrame} = render(); 112 | t.is(lastFrame(), ''); 113 | await delay(100); 114 | stdin.write('Hello World'); 115 | await delay(100); 116 | t.is(lastFrame(), 'Hello World'); 117 | }); 118 | 119 | test('write to stderr', async t => { 120 | function Test() { 121 | const {write} = useStderr(); 122 | 123 | React.useEffect(() => { 124 | write('Hello World'); 125 | }, [write]); 126 | 127 | return Output; 128 | } 129 | 130 | const {stderr, lastFrame} = render(); 131 | t.is(lastFrame(), 'Output'); 132 | await delay(100); 133 | t.is(stderr.lastFrame(), 'Hello World'); 134 | }); 135 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "@sindresorhus/tsconfig", 3 | "compilerOptions": { 4 | "outDir": "build", 5 | "sourceMap": true, 6 | "jsx": "react" 7 | }, 8 | "include": [ 9 | "source" 10 | ] 11 | } 12 | --------------------------------------------------------------------------------