UNPKG

5.03 kBMarkdownView Raw
1# node-mock-stdin
2
3[![Build Status](https://travis-ci.org/caitp/node-mock-stdin.svg?branch=master)](https://travis-ci.org/caitp/node-mock-stdin) [![Coverage Status](https://img.shields.io/coveralls/caitp/node-mock-stdin.svg)](https://coveralls.io/r/caitp/node-mock-stdin?branch=master) [![NPM Version](http://img.shields.io/npm/v/mock-stdin.svg)](https://www.npmjs.org/package/mock-stdin)
4
5Provide a mock readable stream, useful for testing interactive CLI applications.
6
7Maybe simple mocks for other standard files wouldn't be a terrible idea, if anyone
8feels like those are needed. Patches welcome.
9
10## API
11
12- **Module**
13 - [stdin()](#modulestdin)
14- **MockSTDIN**
15 - [send()](#mockstdinsenddata-encoding)
16 - [end()](#mockstdinend)
17 - [restore()](#mockstdinrestore)
18 - [reset()](#mockstdinresetremovelisteners)
19
20---
21
22### Module.stdin()
23
24**example**
25
26```js
27require('mock-stdin').stdin();
28```
29
30Replaces the existing `process.stdin` value with a mock object exposing a `send` method (a
31`MockSTDIN` instance). This allows APIs like `process.openStdin()` or `process.stdin.on()`
32to operate on a mock instance.
33
34**note**: Event listeners from the original `process.stdin` instance are not added to the
35mock instance. Installation of the mock should occur before any event listeners are
36registered.
37
38**return value**: A `MockSTDIN` instance
39
40---
41
42### MockSTDIN.send(data, encoding)
43
44**example**
45
46```js
47var stdin = require('mock-stdin').stdin();
48stdin.send("Some text", "ascii");
49stdin.send(Buffer("Some text", "Some optional encoding"));
50stdin.send([
51 "Array of lines",
52 " which are joined with a linefeed."
53]);
54
55// sending a null will trigger EOF and dispatch an 'end' event.
56stdin.send(null);
57```
58
59Queue up data to be read by the stream. Results in data (and possibly end) events being
60dispatched.
61
62**parameters**
63 - `data`: A `String`, `Buffer`, `Array<String>`, or `null`. The `data` parameter will result in
64 the default encoding if specified as a string or array of strings.
65 - `encoding`: An optional encoding which is used when `data` is a `String`.
66 Node.js's internal Readable Stream will convert the specified encoding into the output
67 encoding, which is transcoded if necessary.
68
69**return value**: The `MockSTDIN` instance, for chaining.
70
71---
72
73### MockSTDIN.end()
74
75**example**
76
77```js
78var stdin = require('mock-stdin').stdin();
79stdin.end();
80```
81
82Alias for [MockSTDIN.send(null)](#mockstdinsend). Results in dispatching an `end` event.
83
84**return value**: The `MockSTDIN` instance, for chaining.
85
86---
87
88### MockSTDIN.restore()
89
90**example**
91
92```js
93var stdin = require('mock-stdin').stdin();
94// process.stdin is now a mock stream
95stdin.restore();
96// process.stdin is returned to its original state
97```
98
99Restore the target of the mocked stream. If only a single mock stream is created, will restore
100the original `stdin` TTY stream. If multiple mock streams are created, it will restore the
101stream which was active at the time the mock was created.
102
103**return value**: The `MockSTDIN` instance, for chaining.
104
105---
106
107### MockSTDIN.reset(removeListeners)
108
109**example**
110
111```js
112var stdin = require('mock-stdin').stdin();
113stdin.end();
114stdin.reset();
115stdin.send("some data");
116```
117
118Ordinarily, a Readable stream will throw when attempting to push after an EOF. This routine will
119reset the `ended` state of a Readable stream, preventing it from throwing post-EOF. This prevents
120being required to re-create a mock STDIN instance during certain tests where a fresh stdin is
121required.
122
123If the `removeListeners` flag is set to `true`, all event listeners will also be reset. This is
124useful in cases where you need to emulate restarting an entire application, without fully
125re-creating the mock object.
126
127**parameters**
128 - `removeListeners`: Boolean value which, when set to `true`, will remove all event listeners
129 attached to the stream.
130
131**return value**: The `MockSTDIN` instance, for chaining.
132
133---
134
135## [LICENSE](LICENSE)
136
137The MIT License (MIT)
138
139Copyright (c) 2014 Caitlin Potter & Contributors
140
141Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
142
143The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
144
145THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.