Skip to main content

cli-testing-library

CLI Testing Library is a companion library to Testing Library that aims to mimic the API of Testing Library for testing CLI applications.

npm install --save-dev cli-testing-library
import {resolve} from 'path'
import {render} from 'cli-testing-library'

test('Is able to make terminal input and view in-progress stdout', async () => {
const {clear, findByText, queryByText, userEvent} = await render('node', [
resolve(__dirname, './execute-scripts/stdio-inquirer.js'),
])

const instance = await findByText('First option')

expect(instance).toBeInTheConsole()

expect(await findByText('❯ One')).toBeInTheConsole()

clear()

userEvent('[ArrowDown]')

expect(await findByText('❯ Two')).toBeInTheConsole()

clear()

userEvent.keyboard('[Enter]')

expect(await findByText('First option: Two')).toBeInTheConsole()
expect(await queryByText('First option: Three')).not.toBeInTheConsole()
})

Check out CLI Testing Library's documentation for a full list of its API.