cltester

Automate command-line testing

Usage no npm install needed!

<script type="module">
  import cltester from 'https://cdn.skypack.dev/cltester';
</script>

README

cltester

Simple end-to-end command-line testing

  • Give the test a name.
  • Set the command line.
  • Set what you expect to see in standard out and standard error.
  • Run it.

new CLTester('order two toppings', 'orderPizza large [mushrooms,peppers]')
      .standardOutMustMatch('Ordered large pizza with mushrooms and peppers!')
      .run();

This creates a test named order two toppings.
The command line is orderPizza large [mushrooms,peppers].
The test expects standard out to exactly match Ordered large pizza with mushrooms and peppers!
The test is executed.

If the expectation is met, CLTester will write

Pass [order two toppings]

to standard out.

If the expectation is not met, CLTester will write

Failure [order two toppings]
   PROBLEM: stdout does not contain expected value
   EXPECTED: Ordered large pizza with mushrooms and peppers!
   ACTUAL: Ordered large pizza with mushrooms!

to standard out.

If you prefer to see only failures, silence successful tests with the setTerse() function.

new CLTester('order small', 'orderPizza small')
      .standardOutMustMatch('Ordered small pizza with no toppings!')
      .setTerse()
      .run();

If the expectation is met for this test, CLTester will not write anything to standard out.

Expectations

Expectation Description
standardOutMustMatch standard output must match this string exactly
standardOutMustNotMatch standard output must not match this string
standardOutMustContain standard output must contain this string
standardOutMustNotContain standard output must not contain this string
standardErrMustMatch standard error must match this string exactly
standardErrMustNotMatch standard error must not match this string
standardErrMustContain standard error must contain this string
standardErrMustNotContain standard error must not contain this string



CLTester Tests

See CLTesterTest.js for usage examples.

Note that this tests a testing utility, so uses a bit of trickery. We want to verify that failing tests are reported correctly. In this case the failures are expected, so they are considered passing tests. The convert_good_failures.sh script handles this. If you run

node test/CLTesterTest.js

you will see a number of test faiures. These are tests whose names end with "_ShouldFail". convert_good_failures.sh script changes these to say "Pass".

The test in package.json pipes the test output through convert_good_failures.sh (and through sed to remove blank lines). If you run

npm test

you should see all tests pass.

For Windows

convert_good_failures.sh will of course not work in Windows. If you want to test CLTester, you can do either of the following.

  1. Use node to run the tests directly

node test/CLTesterTest.js

  1. If you want to run the tests with the npm test command, change the test script in package.json from

"node test/CLTesterTest.js | ./test/convert_good_failures.sh | sed /^$/d"

        to

"node test/CLTesterTest.js"

In either case, on Windows, you will see the expected failures appear as failing tests. That's okay. They're supposed to fail.

To Do

Regular expressions - Allow callers to pass regular expressions to expectation functions and evaluate them accordingly. It makes sense to use the familiar /regex/ syntax for regexes.