Chai Exec
用于测试 CLI 的 Chai 断言
特性
-
易于使用
将您的 CLI 和参数作为单个字符串、字符串数组或单独参数传递。 -
避免重复
一次性设置您的常见 默认值。每个测试只需要指定对它唯一的参数。 -
流畅的断言
使用直观的流畅语法测试您的 CLI,例如myCLI.should.exit.with.code(0)
或myCLI.stdout.should.contain("some string")
。 -
异步支持
只需使用await chaiExecAsync()
而不是chaiExec()
。其他一切保持不变。 -
Windows 支持
出色的 Windows 支持,得益于 cross-spawn。
相关项目
- 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
然后在您的测试文件中引用它并将其注册到 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()
函数的工作方式与 chaiExec()
完全相同,只是它异步运行您的 CLI 并返回一个 Promise
,该 Promise
在 CLI 退出时解析。您需要显式地引用 chaiExecAsync
导出,如下所示
const { chaiExecAsync } = require("@jsdevtools/chai-exec");
然后,您可以像 chaiExec
一样使用 chaiExecAsync
,但请记住使用 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 的 stderr 输出(错误和警告)。您可以测试特定字符串、子字符串或正则表达式。
// 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 + 输出)。您可以测试特定字符串、子字符串或正则表达式。
// 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 上打开一个问题 并 提交一个拉取请求。
构建/测试
要在您的计算机上本地构建/测试项目
-
克隆此仓库
git clone hhttps://github.com/JS-DevTools/chai-exec.git
-
安装依赖项
npm install
-
运行测试
npm test
许可证
Chai Exec 是 100% 免费开源的,在 MIT 许可证 下。您可以随意使用它。
此软件包是 Treeware。如果您在生产环境中使用它,那么我们要求您 为世界种一棵树 来感谢我们为您的工作付出的努力。通过为 Treeware 森林做出贡献,您将为当地家庭创造就业机会,并恢复野生动物栖息地。
衷心感谢
感谢这些优秀的公司支持开源开发者 ❤