Gathering detailed insights and metrics for @jsdevtools/chai-exec
Gathering detailed insights and metrics for @jsdevtools/chai-exec
Gathering detailed insights and metrics for @jsdevtools/chai-exec
Gathering detailed insights and metrics for @jsdevtools/chai-exec
npm install @jsdevtools/chai-exec
70.7
Supply Chain
98.9
Quality
79.3
Maintenance
100
Vulnerability
100
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
5 Stars
125 Commits
1 Forks
3 Watching
2 Branches
3 Contributors
Updated on 16 Jan 2024
Minified
Minified + Gzipped
JavaScript (89.7%)
TypeScript (10.3%)
Cumulative downloads
Total Downloads
Last day
154.6%
247
Compared to previous day
Last week
45.5%
723
Compared to previous week
Last month
-11.7%
2,828
Compared to previous month
Last year
-3.6%
80,937
Compared to previous year
Easy to use
Pass your CLI and arguments as a single string, an array of strings, or as separate parameters.
Don't repeat yourself
Set your common defaults once. Each test ony needs to specify the arguments that are unique to it.
Fluent assertions
Test your CLI using intuitive fluent syntax, such as myCLI.should.exit.with.code(0)
or myCLI.stdout.should.contain("some string")
.
Async Support
Just use await chaiExecAsync()
instead of chaiExec()
. Everything else is the same.
Windows Support
Excellent Windows support, thanks to cross-spawn.
1const chaiExec = require("@jsdevtools/chai-exec"); 2const chai = require("chai"); 3 4chai.use(chaiExec); 5 6describe("My CLI", () => { 7 it("should exit with a zero exit code", () => { 8 // Run your CLI 9 let myCLI = chaiExec('my-cli --arg1 --arg2 "some other arg"'); 10 11 // Should syntax 12 myCLI.should.exit.with.code(0); 13 myCLI.stdout.should.contain("Success!"); 14 myCLI.stderr.should.be.empty; 15 16 // Expect sytnax 17 expect(myCLI).to.exit.with.code(0); 18 expect(myCLI).stdout.to.contain("Success!"); 19 expect(myCLI).stderr.to.be.empty; 20 21 // Assert syntax 22 assert.exitCode(myCLI, 0); 23 assert.stdout(myCLI, "Success!"); 24 assert.stderr(myCLI, ""); 25 }); 26});
Install using npm:
1npm install @jsdevtools/chai-exec
Then require it in your test file and register it with Chai:
1const chaiExec = require("@jsdevtools/chai-exec"); 2const chai = require("chai"); 3 4chai.use(chaiExec);
chaiExec(cli, [args], [options])
You can pass your CLI and its arguments as a single string, an array of strings, or as separate parameters. The following examples all do the same thing:
1chaiExec(`git commit -am "Fixed a bug"`); // Pass the CLI and args as a single string 2chaiExec("git", "commit", "-am", "Fixed a bug"); // Pass the CLI and args as separate params 3chaiExec(["git", "commit", "-am", "Fixed a bug"]); // Pass the CLI and args as an array 4chaiExec("git", ["commit", "-am", "Fixed a bug"]); // Pass the CLI as a string and args as an array
See ez-spawn options for details about the options
parameter.
chaiExecAsync(cli, [args], [options])
The chaiExecAsync()
function works exactly the same as chaiExec()
, except that it runs your CLI asynchronously and returns a Promise
that resolves when the CLI exits. You'll need to explicitly require the chaiExecAsync
export, like this:
1const { chaiExecAsync } = require("@jsdevtools/chai-exec");
You can then use chaiExecAsync
exactly the same as chaiExec
, but remember to use the async
and await
keywords, since it's asynchronous.
1const { chaiExecAsync } = require("@jsdevtools/chai-exec"); 2const chai = require("chai"); 3 4chai.use(chaiExecAsync); 5 6describe("My CLI", () => { 7 it("should exit with a zero exit code", async () => { 8 // Run your CLI 9 let myCLI = await chaiExecAsync('my-cli --arg1 --arg2 "some other arg"'); 10 11 // Should syntax 12 myCLI.should.exit.with.code(0); 13 myCLI.stdout.should.contain("Success!"); 14 myCLI.stderr.should.be.empty; 15 16 // Expect sytnax 17 expect(myCLI).to.exit.with.code(0); 18 expect(myCLI).stdout.to.contain("Success!"); 19 expect(myCLI).stderr.to.be.empty; 20 21 // Assert syntax 22 assert.exitCode(myCLI, 0); 23 assert.stdout(myCLI, "Success!"); 24 assert.stderr(myCLI, ""); 25 }); 26});
chaiExec.defaults
When writing tests for a CLI, you'll often want to use the same command, args, and/or options for every test. Rather than repeating the same parameters every time you call chaiExec
, you can just set chaiExec.defaults
once. Your default values will be used for every subsequent chaiExec()
call. You can specify additional CLI arguments and/or options for each call, in addition to the defaults.
defaults.command
(string)
The name or path of your CLI. Set this once, and then you only ever need to pass arguments to chaiExec()
defaults.args
(string or array of strings)
Arguments to pass to your CLI every time. If you pass additional arguments when you call chaiExec()
, they will be appended to the default arguments.
defaults.options
(options object)
Default options to use every time. If you pass additional options when you call chaiExec()
, they will be merged with the default arguments.
1const chaiExec = require("@jsdevtools/chai-exec"); 2const chai = require("chai"); 3 4chai.use(chaiExec); 5 6// Set some defaults 7chaiExec.defaults = { 8 command: "my-cli", 9 args: "--arg1 --arg2", 10 options: { 11 cwd: "/usr/bin" 12 } 13}; 14 15describe("My CLI", () => { 16 it("should use defaults", () => { 17 // Run your CLI using defaults + one-time args 18 let myCLI("--arg3 --arg4"); 19 20 myCLI.command.should.equal("my-cli"); 21 myCLI.args.should.deep.equal([ "--arg1", "--arg2", "--arg3", "--arg4" ]); 22 }); 23});
.exitCode(number, [message])
aliases: .exit.code
or .status
Asserts on your CLI's exit code. You can test for a specific code, a list of codes, or a range.
1// Should syntax 2myCLI.exitCode.should.equal(0); 3myCLI.should.have.exitCode(0); 4myCLI.should.exit.with.code(0); 5myCLI.should.exit.with.a.code.that.is.oneOf(0, [0, 1, 2, 3]); 6myCLI.should.have.an.exit.code.of.at.least(0).and.at.most(5); 7 8// Expect sytnax 9expect(myCLI).exitCode.to.equal(0); 10expect(myCLI).to.have.exitCode(0); 11expect(myCLI).to.exit.with.code(0); 12expect(myCLI).to.exit.with.a.code.that.is.oneOf([0, 1, 2, 3]); 13expect(myCLI).to.have.an.exit.code.of.at.least(0).and.at.most(5); 14 15// Assert syntax 16assert.equal(myCLI.exitCode, 0); 17 18assert.exitCode(myCLI, 0); 19assert.exitCode(myCLI, [0, 1, 2, 3]); 20 21assert.notExitCode(myCLI, 1); 22assert.notExitCode(myCLI, [1, 2, 3]); 23 24assert.exitCodeBetween(myCLI, 0, 5); 25assert.exitCodeNotBetween(myCLI, 1, 5);
.stdout(string, [message])
Asserts on your CLI's standard output (non-error, non-warning output). You can test for a specific string, a substring, or a regular expression.
1// Should syntax 2myCLI.stdout.should.equal("Success!"); 3myCLI.should.have.stdout.that.contains("Success!"); 4myCLI.should.have.stdout.that.does.not.contain("Failure!"); 5myCLI.should.have.stdout.that.matches(/^Success!$/); 6myCLI.should.have.stdout.that.does.not.match(/^Failure!$/); 7 8// Expect syntax 9expect(myCLI).stdout.to.equal("Success!"); 10expect(myCLI).to.have.stdout.that.contains("Success!"); 11expect(myCLI).to.have.stdout.that.does.not.contain("Failure!"); 12expect(myCLI).to.have.stdout.that.matches(/^Success!$/); 13expect(myCLI).to.have.stdout.that.does.not.match(/^Failure!$/); 14 15// Assert syntax 16assert.stdout(myCLI, "Success!"); 17assert.stdout(myCLI, /^Success!$/); 18 19assert.include(myCLI.stdout, "Success!"); 20assert.notInclude(myCLI.stdout, "Failure!"); 21 22assert.match(myCLI.stdout, /^Success!$/); 23assert.notMatch(myCLI.stdout, /^Failure!$/);
.stderr(string, [message])
Asserts on your CLI's stderr output (errors and warnings). You can test for a specific string, a substring, or a regular expression.
1// Should syntax 2myCLI.stderr.should.equal("Failure!"); 3myCLI.should.have.stderr.that.contains("Failure!"); 4myCLI.should.have.stderr.that.does.not.contain("Success!"); 5myCLI.should.have.stderr.that.matches(/^Failure!$/); 6myCLI.should.have.stderr.that.does.not.match(/^Success!$/); 7 8// Expect syntax 9expect(myCLI).stderr.to.equal("Failure!"); 10expect(myCLI).to.have.stderr.that.contains("Failure!"); 11expect(myCLI).to.have.stderr.that.does.not.contain("Success!"); 12expect(myCLI).to.have.stderr.that.matches(/^Failure!$/); 13expect(myCLI).to.have.stderr.that.does.not.match(/^Success!$/); 14 15// Assert syntax 16assert.stderr(myCLI, "Failure!"); 17assert.stderr(myCLI, /^Failure!$/); 18 19assert.include(myCLI.stderr, "Failure!"); 20assert.notInclude(myCLI.stderr, "Success!"); 21 22assert.match(myCLI.stderr, /^Failure!$/); 23assert.notMatch(myCLI.stderr, /^Success!$/);
.output(string, [message])
Asserts on all of your CLI's output (stdout + output). You can test for a specific string, a substring, or a regular expression.
1// Should syntax 2myCLI.output.should.equal("Success!"); 3myCLI.should.have.output.that.contains("Failure!"); 4myCLI.should.have.output.that.does.not.contain("Success!"); 5myCLI.should.have.output.that.matches(/^(Success|Failure)!$/); 6myCLI.should.have.output.that.does.not.match(/^(Success|Failure)!$/); 7 8// Expect syntax 9expect(myCLI).output.to.equal("Success!"); 10expect(myCLI).to.have.output.that.contains("Failure!"); 11expect(myCLI).to.have.output.that.does.not.contain("Success!"); 12expect(myCLI).to.have.output.that.matches(/^(Success|Failure)!$/); 13expect(myCLI).to.have.output.that.does.not.match(/^(Success|Failure)!$/); 14 15// Assert syntax 16assert.output(myCLI, "Failure!"); 17assert.output(myCLI, /^(Success|Failure)!$/); 18 19assert.include(myCLI.output, "Failure!"); 20assert.notInclude(myCLI.output, "Success!"); 21 22assert.match(myCLI.output, /^Failure!$/); 23assert.notMatch(myCLI.output, /^Success!$/);
Contributions, enhancements, and bug-fixes are welcome! Open an issue on GitHub and submit a pull request.
To build/test the project locally on your computer:
Clone this repo
git clone hhttps://github.com/JS-DevTools/chai-exec.git
Install dependencies
npm install
Run the tests
npm test
Chai Exec is 100% free and open-source, under the MIT license. Use it however you want.
This package is Treeware. If you use it in production, then we ask that you buy the world a tree to thank us for our work. By contributing to the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats.
Thanks to these awesome companies for their support of Open Source developers ❤
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 3
Details
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no SAST tool detected
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
28 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More