cli-testing-library
CLI Testing Library
是 Testing Library
的配套函式庫,旨在模擬 Testing Library
的 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 Testing Library 的文件以取得完整的 API 清單。