The easy way to test commands.
npm i command-test --save
This function runs the given command.
Returns a <Promise>
that resolves to a Command object.
const command = require('command-test')
async function example() {
const cmd = await command('node -v')
cmd.code // 0
cmd.out // "v8.1.4"
cmd.err // ""
}
example()
This function runs the given command. If the command's exit code is not 0, an error gets thrown.
Returns a <Promise>
that resolves to a Command object.
const command = require('command-test')
async function example() {
const cmd = await command.works('node -v')
command.code // 0
command.out // "v8.1.4"
command.err // ""
await command.fails('non-existent-command')
// Throws an error because the command has an exit code 1
}
example()
This function runs the given command. If the command's exit code is 0, an error gets thrown.
Returns a <Promise>
that resolves to a Command object.
const command = require('command-test')
async function example() {
const cmd = await command.fails('non-existent-command')
cmd.code // 1
cmd.err // "/bin/sh: 1: huhn: not found\n"
cmd.out // ""
await command.fails('node -v')
// Throws an error because the command has an exit code 0
}
example()
Represents a command.
This class extends BetterEvents.
Therefore you can await events if you don't pass a listener to the .once()
method.
const { Command } = require('command-test')
async function example() {
const command = new Command('node -v')
await command.once('done')
command.code // 0
command.out // "v8.1.4"
command.err // ""
}
example()
This event is emitted as soon as the command exits.
await command.once('done')
// or
command.once('done', () => {
})
// or
command.on('done', () => {
})
This property is a <string>
that stores everything that the command has written to the stdout stream.
This property is a <string>
that stores everything that the command has written to the stderr stream.
This property stores the exit code <number>
of the command.
This property is a reference to the stdout of the command's child_process
This property is a reference to the stderr of the command's child_process