Socket
Socket
Sign inDemoInstall

stdio-mock

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

stdio-mock

Mock Command Line Applications


Version published
Weekly downloads
837
increased by32.44%
Maintainers
1
Weekly downloads
 
Created
Source

Standard I/O Mock

A basic implementation of Readable and Writable streams to mock process.stdout, process.stderr, process.stdin, or any readable and writable streams.

Get it

npm install --save stdio-mock

TypeScript

This project is proudly written in TypeScript so you can enjoy using beautiful typings! :smile:

Basic Usage

for standard i/o

import { stdio } from 'stdio-mock';

const { stdout, stdin } = stdio();

stdin.pipe(stdout);

stdout.on('data', data => {
  // do stuff
})

stdin.write('test data');
stdin.end();

for more general purpose use cases

import { MockReadable, MockWriteable } from 'stdio-mock';

const stdin = new MockReadable();
const stdout = new MockWriteable();

API

stdio(): StdIO

This is the main function exposed by the library as a convenience to mock your standard input and output streams.

type StdIO = {
  stdin: MockReadable,
  stdout: MockWriteable,
  stderr: MockWriteable,
} 
import { stdio } from 'stdio-mock';

const { stdout, stdin } = stdio();

stdin.pipe(stdout);

stdout.on('data', data => {
  // do stuff
})

stdin.write('test data');
stdin.end();
MockReadable :: stream.Readable

This is an implementation of Node.js' stream.Readable with additional methods for use in testing.

MockReadable.write(...data: Array): MockWriteable

Pushes data into the Readable stream. This will throw and error if the stream has ended.

import { MockReadable } from 'stdio-mock';

const readable = new MockReadable();

readable.on('data', (data: string) => {
  assert.stictEqual(data, 'test');
});

readable.write('test');

MockReadable.data(): Array

Returns an array containing all data that has been passed into the stream.

import { MockReadable } from 'stdio-mock';

const readable = new MockReadable();

readable.write('test');

const data = readable.data();

assert.strictEqual(data[0], 'test');

MockReadable.end(): void

Ends the stream asynchronously.

import { MockReadable } from 'stdio-mock';

const readable = new MockReadable();

readable.write('test');
readable.end();
MockWriteable :: stream.Writable

This is an implementation of Node.js' stream.Writable with an additional method to query all the data that has been pushed to it.

MockWriteable.data(): Array

Returns an array containing all data that has been passed into the stream.

import { MockWritable } from 'stdio-mock';

const writable = new MockWritable();

writable.write('test');

const data = writable.data();

assert.strictEqual(data[0], 'test');

Keywords

FAQs

Package last updated on 30 Mar 2022

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc