With this library you can build test doubles for
process execution with execa
.
npm install execa_test_double
Inline stubbing with mockery:
const execaTestDouble = require('execa_test_double');
const mockery = require('mockery');
(async () => {
execaTestDouble.createStub([{
command: "echo hello world",
exitCode: 0,
stdout: "hello world",
stderr: ""
}]);
mockery.registerMock('execa', execaTestDouble.execa);
mockery.enable({useCleanCache: true});
//expected output `hello world`
const execa = require('execa');
console.log(await execa('echo', ['hello', 'world']));
mockery.disable();
mockery.deregisterAll();
})();
Stubbed invocations can be verified with the getStub()
function:
const execaTestDouble = require('execa_test_double');
const mockery = require('mockery');
(async () => {
execaTestDouble.createStub([{
command: "echo hello world",
exitCode: 0,
stdout: "hello world",
stderr: ""
}]);
mockery.registerMock('execa', execaTestDouble.execa);
mockery.enable({useCleanCache: true});
const execa = require('execa');
console.log(await execa('echo', ['hello', 'world']));
assert.true(execaTestDouble.getStub().withArgs('echo hello world').calledOnce);
mockery.disable();
mockery.deregisterAll();
})();
The verification methods of sinon spies can be used.
Creates stubs for processes. For processes
a array with
the type childProcessResult
is expected.
For an successful execution please supply the following properties:
{
// command (file and arguments)
command: "echo hello world",
exitCode: 0,
stdout: "hello world",
stderr: ""
}
For an command, which terminates with exitCode !== 0
:
{
command: "terminate",
exitCode: 1,
exitCodeName: "EPERM",
stdout: "",
stderr: "terminated"
}
For an unknown file:
{
command: "unknown",
exitCodeName: "ENOENT"
}
The stub behavior is reseted at every invocation of this method. The real command is executed, if it isn't stubbed.
Reads the stubs from JSON-Files (JSON-Array or JSON-Object).
Globby is used for path
filtering. The parameter globs
and options
are
directly passed to globby.
The stub behavior is reseted at every invocation of this method. The real command is executed, if it isn't stubbed.
Resets the history and the behavior of the stub.
Returns the sinon stub.
The major and minor component of the version number
will be keeped in sync with execa
.