# command-test

> The easy way to test commands.

Latest version **1.1.1** (published 2017-10-04) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install command-test
pnpm add command-test
yarn add command-test
bun add command-test
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.1.1 |
| Published | 2017-10-04 |
| First published | 2017-08-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=6.0.0 |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | robojones |
| Maintainers | robojones |
| Keywords | command, testing, test, promises, promise, commands |

## Links

- npm: https://www.npmjs.com/package/command-test
- Repository: https://github.com/robojones/command-test
- Homepage: https://github.com/robojones/command-test#readme
- Issues: https://github.com/robojones/command-test/issues
- npm.io page: https://npm.io/package/command-test

## Dependencies (1)

- [better-events](https://npm.io/package/better-events.md) ^3.0.5

## Alternatives

- [duck](https://npm.io/package/duck.md) — 4.2M weekly downloads
- [ava](https://npm.io/package/ava.md) — 560.2K weekly downloads
- [storybook-addon-module-mock](https://npm.io/package/storybook-addon-module-mock.md) — 71.7K weekly downloads
- [@ethereum-waffle/mock-contract](https://npm.io/package/@ethereum-waffle/mock-contract.md) — 40.0K weekly downloads
- [aws-elasticsearch-connector](https://npm.io/package/aws-elasticsearch-connector.md) — 37.4K weekly downloads

## Recent versions

- 1.1.1 (latest) — 2017-10-04
- 1.1.0 — 2017-09-24
- 1.0.0 — 2017-08-31
- 0.2.1 — 2017-08-31
- 0.2.0 — 2017-08-28
- 0.1.0 — 2017-08-28
- 0.0.1 — 2017-08-28
- 0.0.0 — 2017-08-28

## README

# command-test
The easy way to test commands.

[![Build Status](https://travis-ci.org/robojones/command-test.svg?branch=master)](https://travis-ci.org/robojones/command-test)
[![Test Coverage](https://codeclimate.com/github/robojones/command-test/badges/coverage.svg)](https://codeclimate.com/github/robojones/command-test/coverage)

[![bitHound Code](https://www.bithound.io/github/robojones/command-test/badges/code.svg)](https://www.bithound.io/github/robojones/command-test)
[![bitHound Overall Score](https://www.bithound.io/github/robojones/command-test/badges/score.svg)](https://www.bithound.io/github/robojones/command-test)
[![bitHound Dependencies](https://www.bithound.io/github/robojones/command-test/badges/dependencies.svg)](https://www.bithound.io/github/robojones/command-test/master/dependencies/npm)
[![bitHound Dev Dependencies](https://www.bithound.io/github/robojones/command-test/badges/devDependencies.svg)](https://www.bithound.io/github/robojones/command-test/master/dependencies/npm)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

## Install

```
npm i command-test --save
```

## Docs

### command(cmd)

This function runs the given command.

__Returns__ a `<Promise>` that resolves to a [Command](#class-command) object.

```javascript
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()
```

### command.works(cmd)
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](#class-command) object.

```javascript
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()
```

### command.fails(cmd)
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](#class-command) object.

```javascript
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()
```

### Class: Command

Represents a command.

This class extends [BetterEvents](https://npmjs.com/package/better-events).
Therefore you can await events if you don't pass a listener to the `.once()` method.

#### Example
```javascript
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()
```

#### Event: "done"
This event is emitted as soon as the command exits.

```javascript
await command.once('done')

// or
command.once('done', () => {

})

// or
command.on('done', () => {

})
```

#### Property: out
This property is a `<string>` that stores everything that the command has written to the stdout stream.

#### Property: err

This property is a `<string>` that stores everything that the command has written to the __stderr__ stream.

#### Property: code
This property stores the __exit code__ `<number>` of the command.

#### Property: stdout
This property is a reference to the stdout of the command's child_process

#### Property: stderr
This property is a reference to the stderr of the command's child_process

---
_Source: https://npm.io/package/command-test · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
