Skip to content

wise-team/stdio-mock

 
 

Repository files navigation

🍴 'Standard I/O Mock' forked by Wise team

This fork resolves the following issue:

Class 'MockReadable' incorrectly extends base class 'Readable'. Property '_read' is protected in type 'MockReadable' but public in type 'Readable'.

To install it, please sh:

$ npm install --save stdio-mock--fork-by-wiseteam

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! 😄

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');

About

Fork of Standard I/O mocks for Node.js that fixes TypeError in Node v10.x

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 100.0%