Back to Testing Library

cli-testing-library

docs/ecosystem-cli-testing-library.mdx

latest1.2 KB
Original Source

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

bash
npm install --save-dev cli-testing-library
js
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.