Chai Exec

CLIテスト用 Chai アサーション

Cross-Platform Compatibility Build Status

Coverage Status Dependencies

npm License Buy us a tree

機能

  • 使いやすさ
    CLIと引数を単一の文字列、文字列の配列、または個別の引数として渡すことができます。

  • 繰り返しの排除
    共通のデフォルト値一度設定します。各テストでは、それぞれに固有の引数のみを指定する必要があります。

  • 流れるようなアサーション
    myCLI.should.exit.with.code(0)myCLI.stdout.should.contain("some string") のような直感的な流れるような構文を使用して、CLIをテストします。

  • 非同期サポート
    chaiExec() の代わりに await chaiExecAsync() を使用するだけです。それ以外は同じです。

  • Windowsサポート
    cross-spawnのおかげで、優れたWindowsサポートを提供します。

  • ez-spawn - シンプルで一貫性のあるプロセス生成

const chaiExec = require("@jsdevtools/chai-exec");
const chai = require("chai");

chai.use(chaiExec);

describe("My CLI", () => {
  it("should exit with a zero exit code", () => {
    // Run your CLI
    let myCLI = chaiExec('my-cli --arg1 --arg2 "some other arg"');

    // Should syntax
    myCLI.should.exit.with.code(0);
    myCLI.stdout.should.contain("Success!");
    myCLI.stderr.should.be.empty;

    // Expect sytnax
    expect(myCLI).to.exit.with.code(0);
    expect(myCLI).stdout.to.contain("Success!");
    expect(myCLI).stderr.to.be.empty;

    // Assert syntax
    assert.exitCode(myCLI, 0);
    assert.stdout(myCLI, "Success!");
    assert.stderr(myCLI, "");
  });
});

インストール

npmを使用してインストールします

npm install @jsdevtools/chai-exec

次に、テストファイルでそれをrequireし、Chaiに登録します

const chaiExec = require("@jsdevtools/chai-exec");
const chai = require("chai");

chai.use(chaiExec);

使用方法

chaiExec(cli, [args], [options])

CLIとその引数を、単一の文字列、文字列の配列、または個別の引数として渡すことができます。次の例はすべて同じことを行います

chaiExec(`git commit -am "Fixed a bug"`);           // Pass the CLI and args as a single string
chaiExec("git", "commit", "-am", "Fixed a bug");    // Pass the CLI and args as separate params
chaiExec(["git", "commit", "-am", "Fixed a bug"]);  // Pass the CLI and args as an array
chaiExec("git", ["commit", "-am", "Fixed a bug"]);  // Pass the CLI as a string and args as an array

options パラメータの詳細については、ez-spawn オプションを参照してください。

chaiExecAsync(cli, [args], [options])

chaiExecAsync() 関数は、CLIを非同期で実行し、CLIが終了したときに解決される Promise を返すことを除いて、chaiExec() とまったく同じように動作します。次のように、chaiExecAsync エクスポートを明示的にrequireする必要があります

const { chaiExecAsync } = require("@jsdevtools/chai-exec");

その後、chaiExecAsyncchaiExec とまったく同じように使用できますが、非同期であるため、async および await キーワードを使用することを忘れないでください。

const { chaiExecAsync } = require("@jsdevtools/chai-exec");
const chai = require("chai");

chai.use(chaiExecAsync);

describe("My CLI", () => {
  it("should exit with a zero exit code", async () => {
    // Run your CLI
    let myCLI = await chaiExecAsync('my-cli --arg1 --arg2 "some other arg"');

    // Should syntax
    myCLI.should.exit.with.code(0);
    myCLI.stdout.should.contain("Success!");
    myCLI.stderr.should.be.empty;

    // Expect sytnax
    expect(myCLI).to.exit.with.code(0);
    expect(myCLI).stdout.to.contain("Success!");
    expect(myCLI).stderr.to.be.empty;

    // Assert syntax
    assert.exitCode(myCLI, 0);
    assert.stdout(myCLI, "Success!");
    assert.stderr(myCLI, "");
  });
});

chaiExec.defaults

CLIのテストを作成する場合、すべてのテストで同じコマンド、引数、および/またはオプションを使用することがよくあります。chaiExec を呼び出すたびに同じパラメータを繰り返すのではなく、chaiExec.defaults を一度設定するだけです。デフォルト値は、後続のすべての chaiExec() 呼び出しに使用されます。デフォルトに加えて、呼び出しごとに追加のCLI引数および/またはオプションを指定できます。

  • defaults.command (文字列)
    CLIの名前またはパス。これを一度設定すれば、chaiExec() に引数を渡すだけで済みます

  • defaults.args (文字列または文字列の配列)
    CLIに毎回渡す引数。chaiExec() を呼び出すときに追加の引数を渡すと、それらはデフォルトの引数に追加されます。

  • defaults.options (オプションオブジェクト)
    毎回使用するデフォルトオプション。chaiExec() を呼び出すときに追加のオプションを渡すと、それらはデフォルトの引数とマージされます。

const chaiExec = require("@jsdevtools/chai-exec");
const chai = require("chai");

chai.use(chaiExec);

// Set some defaults
chaiExec.defaults = {
  command: "my-cli",
  args: "--arg1 --arg2",
  options: {
    cwd: "/usr/bin"
  }
};

describe("My CLI", () => {
  it("should use defaults", () => {
    // Run your CLI using defaults + one-time args
    let myCLI("--arg3 --arg4");

    myCLI.command.should.equal("my-cli");
    myCLI.args.should.deep.equal([ "--arg1", "--arg2", "--arg3", "--arg4" ]);
  });
});

アサーション

.exitCode(number, [message])

**エイリアス:** .exit.code または .status

CLIの終了コードをアサートします。特定のコード、コードのリスト、または範囲をテストできます。

// Should syntax
myCLI.exitCode.should.equal(0);
myCLI.should.have.exitCode(0);
myCLI.should.exit.with.code(0);
myCLI.should.exit.with.a.code.that.is.oneOf(0, [0, 1, 2, 3]);
myCLI.should.have.an.exit.code.of.at.least(0).and.at.most(5);

// Expect sytnax
expect(myCLI).exitCode.to.equal(0);
expect(myCLI).to.have.exitCode(0);
expect(myCLI).to.exit.with.code(0);
expect(myCLI).to.exit.with.a.code.that.is.oneOf([0, 1, 2, 3]);
expect(myCLI).to.have.an.exit.code.of.at.least(0).and.at.most(5);

// Assert syntax
assert.equal(myCLI.exitCode, 0);

assert.exitCode(myCLI, 0);
assert.exitCode(myCLI, [0, 1, 2, 3]);

assert.notExitCode(myCLI, 1);
assert.notExitCode(myCLI, [1, 2, 3]);

assert.exitCodeBetween(myCLI, 0, 5);
assert.exitCodeNotBetween(myCLI, 1, 5);

.stdout(string, [message])

CLIの標準出力(エラーや警告以外の出力)をアサートします。特定の文字列、部分文字列、または正規表現をテストできます。

// Should syntax
myCLI.stdout.should.equal("Success!");
myCLI.should.have.stdout.that.contains("Success!");
myCLI.should.have.stdout.that.does.not.contain("Failure!");
myCLI.should.have.stdout.that.matches(/^Success!$/);
myCLI.should.have.stdout.that.does.not.match(/^Failure!$/);

// Expect syntax
expect(myCLI).stdout.to.equal("Success!");
expect(myCLI).to.have.stdout.that.contains("Success!");
expect(myCLI).to.have.stdout.that.does.not.contain("Failure!");
expect(myCLI).to.have.stdout.that.matches(/^Success!$/);
expect(myCLI).to.have.stdout.that.does.not.match(/^Failure!$/);

// Assert syntax
assert.stdout(myCLI, "Success!");
assert.stdout(myCLI, /^Success!$/);

assert.include(myCLI.stdout, "Success!");
assert.notInclude(myCLI.stdout, "Failure!");

assert.match(myCLI.stdout, /^Success!$/);
assert.notMatch(myCLI.stdout, /^Failure!$/);

.stderr(string, [message])

CLIの標準エラー出力(エラーと警告)をアサートします。特定の文字列、部分文字列、または正規表現をテストできます。

// Should syntax
myCLI.stderr.should.equal("Failure!");
myCLI.should.have.stderr.that.contains("Failure!");
myCLI.should.have.stderr.that.does.not.contain("Success!");
myCLI.should.have.stderr.that.matches(/^Failure!$/);
myCLI.should.have.stderr.that.does.not.match(/^Success!$/);

// Expect syntax
expect(myCLI).stderr.to.equal("Failure!");
expect(myCLI).to.have.stderr.that.contains("Failure!");
expect(myCLI).to.have.stderr.that.does.not.contain("Success!");
expect(myCLI).to.have.stderr.that.matches(/^Failure!$/);
expect(myCLI).to.have.stderr.that.does.not.match(/^Success!$/);

// Assert syntax
assert.stderr(myCLI, "Failure!");
assert.stderr(myCLI, /^Failure!$/);

assert.include(myCLI.stderr, "Failure!");
assert.notInclude(myCLI.stderr, "Success!");

assert.match(myCLI.stderr, /^Failure!$/);
assert.notMatch(myCLI.stderr, /^Success!$/);

.output(string, [message])

CLIの**すべて**の出力 (stdout + stderr) をアサートします。特定の文字列、部分文字列、または正規表現をテストできます。

// Should syntax
myCLI.output.should.equal("Success!");
myCLI.should.have.output.that.contains("Failure!");
myCLI.should.have.output.that.does.not.contain("Success!");
myCLI.should.have.output.that.matches(/^(Success|Failure)!$/);
myCLI.should.have.output.that.does.not.match(/^(Success|Failure)!$/);

// Expect syntax
expect(myCLI).output.to.equal("Success!");
expect(myCLI).to.have.output.that.contains("Failure!");
expect(myCLI).to.have.output.that.does.not.contain("Success!");
expect(myCLI).to.have.output.that.matches(/^(Success|Failure)!$/);
expect(myCLI).to.have.output.that.does.not.match(/^(Success|Failure)!$/);

// Assert syntax
assert.output(myCLI, "Failure!");
assert.output(myCLI, /^(Success|Failure)!$/);

assert.include(myCLI.output, "Failure!");
assert.notInclude(myCLI.output, "Success!");

assert.match(myCLI.output, /^Failure!$/);
assert.notMatch(myCLI.output, /^Success!$/);

貢献

貢献、機能強化、バグ修正は大歓迎です!GitHubでissueを開きプルリクエストを送信してください

ビルド/テスト

コンピューター上でローカルにプロジェクトをビルド/テストするには

  1. このリポジトリをクローンします
    git clone https://github.com/JS-DevTools/chai-exec.git

  2. 依存関係をインストールします
    npm install

  3. テストを実行します
    npm test

ライセンス

Chai Execは、MITライセンスの下で、100%無料でオープンソースです。好きなように使ってください。

このパッケージはTreewareです。本番環境で使用する場合、私たちの仕事に感謝するために世界に木を買ってください。Treewareの森に貢献することで、地元の家族の雇用を創出し、野生生物の生息地を回復することができます。

大きな感謝を込めて

オープンソース開発者へのサポートに感謝します ❤

Travis CI SauceLabs Coveralls