Skip to content

Repository files navigation

Create test doubles of execa

Build Status

With this library you can build test doubles for process execution with execa.

Install

npm install execa_test_double

Usage

Stubbing

Inline stubbing with mockery:

constexecaTestDouble=require('execa_test_double');constmockery=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`constexeca=require('execa');console.log(awaitexeca('echo',['hello','world']));mockery.disable();mockery.deregisterAll();})();

Verify expectations

Stubbed invocations can be verified with the getStub() function:

constexecaTestDouble=require('execa_test_double');constmockery=require('mockery');(async()=>{execaTestDouble.createStub([{command: "echo hello world",exitCode: 0,stdout: "hello world",stderr: ""}]);mockery.registerMock('execa',execaTestDouble.execa);mockery.enable({useCleanCache: true});constexeca=require('execa');console.log(awaitexeca('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.

API

Stubbing

createStub(processes)

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.

async createStubFromFixtures(globs, options)

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.

resetStub

Resets the history and the behavior of the stub.

getStub

Returns the sinon stub.

Versioning

The major and minor component of the version number will be keeped in sync with execa.

About

Test double creation for sindresorhus/execa

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages