npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

spawk

v1.8.2

Published

child_process.spawn mocking library

Downloads

14,878

Readme

spawk

node.js child_process.spawn mocking library

Spawk can be used to test modules that call spawn in isolation.

Note: This module does its best to implement platform specific behaviors of child_process.spawn(), anything that behaves differently is a bug.

Example

const spawk = require('spawk')
spawk.spawn('ls').exit(1).stdout('custom response')

const child = require('child_process').spawn('ls')

child here will be a mocked ChildProcess object that will exit with a status code of 1, and will receive the string custom response on its stdout interface.

By default, any calls to spawn that do not match an existing mock will pass through to the original spawn. See preventUnmatched below for more info on how to change this.

Each intercepted call will only be used once, so if you want to intercept multiple calls to the same command you need to call spawk.spawn for each call you want to be intercepted. They will be used in the order that they were created.

API

spawk

npm install spawk
const spawk = require('spawk')

spawk.spawn(command, arguments, options)

Intercept and mock a call to child_process.spawn. Returns a Interceptor object, see below for more info. Parameters are defined as follows:

  • command

Command to intercept and mock. Can either be a string, a RegExp, or a function. The interceptor will mock a given call if the string matches exactly, or the RegExp matches, or the function returns true. The function will be passed three parameters: the command, args, and options passed to child_process.spawn. The function will be called under the context of the Interceptor, so you will have access to the methods and attributes described below.

  • arguments

Optional arguments that must accompany the given command in order to be mocked. Can either be an array or a function. The interceptor will mock a given call if the array matches exactly, or if the function returns true. The function will be passed one parameter: the args passed to child_process.spawn. The function will be called under the context of the Interceptor, so you will have access to the methods and attributes described below.

  • options

Optional options that must accompany the given command in order to be mocked. Can either be an object or a function. The interceptor will mock a given call if all of the attributes in these options match, or if the function returns true. If an object, only the attributes you give are matched, others do not affect whether or not it matches. If a function, it will be passed one parameter: the options passed to child_process.spawn. The function will be called under the context of the Interceptor, so you will have access to the methods and attributes described below.

When generating stdin/stdin/stdout streams for the interceptor, if the call to spawn specifies inherit for their modes they will not be created.

spawk.allowUnmatched()

Allow calls to child_process.spawn that do not match any interceptor to pass through to node's implementation. This is the default state.

spawk.preventUnmatched()

Allow calls to child_process.spawn that do not match any interceptor from passing through to node's implementation. An unmatched call will cause an exception to be thrown.

spawk.done()

Ensure that all configured interceptors have been called. If they have this will return true. If they have not this will throw an exception.

spawk.clean()

Remove any currently configured interceptors.

spawk.unload()

Unloads spawk from intercepting child_process.spawn calls completely. This also removes any currently configured interceptors.

spawk.load()

Loads spawk for intercepting child_process.spawn calls. This is called by default, you should only need to call this if you have previously called spawk.unload() for some reason.

Interceptor

const interceptor = spawk.spawn('ls')

All of the following methods can be chained together.

By default a interceptor will exit with a code of 0 with no signal, and nothing written to either stdout or stderr.

interceptor.called

Boolean that denotes whether or not this interceptor has been called yet

interceptor.description

Helpful string representation of the interceptor.

interceptor.calledWith

When the interceptor has been called, this will be an object that contains the command, args, and options that were actually called.

It will also contain an array with the stdio objects that the spawned process got, provided it was not called with inherit (aka if pipe was used). You can use this to assert for instance that certain things were written to the stdin of your spawned process.

stdio will be an array that contains [stdin, stdout, stderr].

interceptor.signals

Array containing any signals that the interceptor received via .kill()

interceptor.exit(code)

Tells the interceptor what status code to exit with. Defaults to 0.

This can be either a number or a function that returns a number. The function can also be async or return a Promise. The function will be called with this set to the interceptor.

Calling this will clear out any signal previously set with interceptor.signal

interceptor.signal(signal)

Tells the interceptor what signal to exit with. Defaults to null (exit with no signal).

This can be either a string or a function that returns a string. The function can also be async or return a Promise. The function will be called with this set to the interceptor.

Calling this will clear out any code previously set with interceptor.exit

interceptor.stdout(data)

Tells the interceptor what to write to stdout before exit.

This can be either a string, buffer, or a function that returns a string or buffer. The function can also be async or return a Promise. The function will be called with this set to the interceptor.

interceptor.stderr(data)

Tells the interceptor what to write to stderr before exit.

This can be either a string, buffer, or a function that returns a string or buffer. The function can also be async or return a Promise. The function will be called with this set to the interceptor.

interceptor.delay(ms)

Tells the interceptor to delay exiting for a given number of milliseconds.

interceptor.exitOnSignal(signal)

Tells the interceptor to delay exiting until it receives the given signal. Setting this will ignore any delay you have set. This will also be the signal that the interceptor exits with, unless you otherwise change it via interceptor.signal.

interceptor.spawnError(error)

Tells the interceptor to emit the given error object via the error event, instead of the normal spawn event. The interceptor will not emit the exit or close events in this case, nor will it set up any stdio objects. This can be combined with interceptor.delay to delay this error, or with interceptor.exitOnSignal to error when the given signal is received (replicating a process that can not be killed).

interceptor.stdoutEmit(eventName, [...args])

Tells the interceptor to emit a given event from stdout with any number of arguments.

interceptor.stderrEmit(eventName, [...args])

Tells the interceptor to emit a given event from stderr with any number of arguments.