jest-shell-matchers
Test shell scripts while mocking specific commands
Run shell scripts and make assertions about the exit code, stdout, stderr, and termination signal that are generated. It uses the spawn-with-mocks library, so mocks can be written for specific shell commands.
Usage
The library exposes asynchronous matchers, so it requires Jest 23 or higher (to run synchronous tests, use spawn-with-mocks directly). Mocks are created by writing temporary files to disk, so they do not work if fs.writeFileSync
is being mocked.
Initialization
const shellMatchers =
Example Without Mocks
Example With Mocks
Mocks are created by spawn-with-mocks, which documents the mocking API. In this example, we mock the date
and mkdir
commands:
const fs =
Mocks can also return a Number
or String
to shorten the code:
// The string is shorthand for stdout;// stderr will be '' and the exit code will be 0const date = '01-06-2019' // The number is shorthand for the exit code// stdout and stderr will be ''const mkdir = 0
API
expect([command[, args][, options]])
- To use the matchers, call
expect
with the input for spawn-with-mocks#spawn, which the matchers run internally. It can execute a script, create mocks, set enviroment variables, etc. When passingargs
oroptions
, the input must be wrapped with an array:
await await
.toHaveMatchingSpawnOutput (expected)
- The expected value can be a
Number
,String
,RegExp
, orObject
.
const input = 'sh' './test.sh' await // Number: test the exit code await // String: test the stdout for an exact match await // RegExp: test the stdout await // Object: the values can be Numbers, Strings, or RegExps
LICENSE
MIT