Skip to content

bunysae/execa_test_double

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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:

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();
})();

Verify expectations

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.

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

License

Stars

Watchers

Forks

Packages

No packages published