cli-testing-library
CLI 测试库
是 测试库
的一个配套库,旨在模拟 测试库
的 API 来测试 CLI 应用程序。
- npm
- Yarn
npm install --save-dev cli-testing-library
yarn add --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()
})
查看 CLI 测试库的文档,获取其 API 的完整列表。