Giter Club home page Giter Club logo

execa_test_double's Introduction

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.