like web-driver but for the command line
npm install cli-driver
npm install
requires some tools to be present in the system like Python and C++ compiler. Windows users can easily install them by running the following command in PowerShell as administrator. For more information see https://github.com/felixrieseberg/windows-build-tools:
npm install --global --production windows-build-tools
In the following example we instruct the driver to perform the ls
command and wait until it prints package.json
file that we know it should be in the output:
import {Driver} from 'cli-driver'
const client = new Driver()
client.start()
client.enter('ls')
// now we wait until package.json is printed in stdout
const data = await client.waitForData(data => data.includes('package.json'))
expect(data).toContain('package.json')
expect(data).toContain('tsconfig.json')
client.destroy()
Note you could also require()
it like this: const Driver = require('cli-driver').Driver
cli-driver
focuses on alliviate the "driver" part of automating a task in the command line. There are other tools that complement it for writing text, entering keyboard input, mouse input, etc. These are some:
In the previous example you can notice we used await
before client.waitForData()
which allow us to write clean code to handle asynchrony. But if you can't or don't want to do that you can always use good old promises:
client.waitForData(data => data.includes('package.json'))
.then(data => {
expect(data).toContain('package.json')
expect(data).toContain('tsconfig.json')
client.destroy()
})
The following example will create a folder, and execute npm init command answering all the questions:
import {Driver} from 'cli-driver'
import * as shell from 'shelljs'
const projectPath = 'my-cool-npm-project'
shell.mkdir('-p', projectPath)
const client = new Driver()
await client.start({
cwd: projectPath
})
await client.enter('npm init')
// will wait until stdout prints 'package name:' and then enter the project name 'changed-my-mind-project'
await client.waitForDataAndEnter('package name:', 'changed-my-mind-project')
await client.waitForDataAndEnter('version:', '') // just press enter to use default version (1.0.0)
await client.waitForDataAndEnter('description:', 'cool description')
await client.waitForDataAndEnter('entry point:', 'src/index.js')
await client.waitForDataAndEnter('test command:', 'jasmine')
await client.waitForDataAndEnter('git repository:', '')
await client.waitForDataAndEnter('keywords:', '')
await client.waitForDataAndEnter('author:', '')
await client.waitForDataAndEnter('license:', '')
await client.waitForDataAndEnter('Is this ok?', '')
await client.wait(300) // give npm some time to write the file
const packageJson = JSON.parse(shell.cat(`${projectPath}/package.json`))
expect(packageJson.name).toBe('changed-my-mind-project')
expect(packageJson.version).toBe('1.0.0')
expect(packageJson.description).toBe('cool description')
expect(packageJson.main).toBe('src/index.js')
I'm author of plenty packages that use interactive CLI, like yeoman generators and inquirer-based stuff and I would really like to implement integration tests, not just mocking the CLI, but test them in the real worl in different operating systems.
There is a similar node package, node-suppose that attack the same problem, but IMO the UNIX API and semantics is very limited for today days and I wanted an API more imperative, similar to webdriver.
This code automates an example program based on Inquirer.js. It not only test for validation and entering data but also that the output of the program is correct.
This looks like in Linux bash terminal:
And this looks like in a Windows Power Shell:
And this is in Windows cmd.exe terminal
And this looks like in a Windows MINGW64 terminal:
Generated using TypeDoc