New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

execa_test_double

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

execa_test_double

Test doubles for process execution with execa

latest
Source
npmnpm
Version
4.0.1
Version published
Maintainers
1
Created
Source

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.

Keywords

execa

FAQs

Package last updated on 29 May 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts