333
schangxiang@126.com
2025-09-19 18966e02fb573c7e2bb0c6426ed792b38b910940
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# Coffee
 
Test command line on Node.js.
 
---
 
[![NPM version](https://img.shields.io/npm/v/coffee.svg?style=flat)](https://npmjs.org/package/coffee)
[![Build Status](https://img.shields.io/travis/node-modules/coffee.svg?style=flat)](https://travis-ci.org/node-modules/coffee)
[![codecov.io](https://img.shields.io/codecov/c/github/node-modules/coffee.svg?style=flat)](http://codecov.io/github/node-modules/coffee?branch=master)
[![NPM downloads](http://img.shields.io/npm/dm/coffee.svg?style=flat)](https://npmjs.org/package/coffee)
 
## Install
 
```bash
$ npm i coffee --save-dev
```
 
## Usage
 
Coffee is useful for test command line in test frammework (like Mocha).
 
### Fork
 
You can use `fork` for spawning Node processes.
 
```js
const coffee = require('coffee');
 
describe('cli', () => {
  it('should fork node cli', () => {
    return coffee.fork('/path/to/file.js')
    .expect('stdout', '12\n')
    .expect('stderr', /34/)
    .expect('code', 0)
    .end();
  });
});
```
 
In file.js
 
```js
console.log(12);
console.error(34);
```
 
You can pass `args` and `opts` to [child_process fork](https://nodejs.org/api/child_process.html#child_process_child_process_fork_modulepath_args_options).
 
```js
coffee.fork('/path/to/file.js', [ 'args' ], { execArgv: [ '--inspect' ]})
  .expect('stdout', '12\n')
  .expect('stderr', '34\n')
  .expect('code', 0)
  .end();
```
 
And more:
 
```js
coffee.fork('/path/to/file.js')
  // print origin stdio
  .debug()
 
  // inject a script
  .beforeScript(mockScript)
 
  // interact with prompt
  .waitForPrompt()
  .write('tz\n')
 
  // string strict equals
  .expect('stdout', 'abcdefg')
  // regex
  .expect('stdout', /^abc/)
  // multiple
  .expect('stdout', [ 'abcdefg', /abc/ ])
  .expect('code', 0)
  .end();
```
 
see the API chapter below for more details.
 
### Spwan
 
You can also use `spawn` for spawning normal shell scripts.
 
```js
coffee.spawn('cat')
  .write('1')
  .write('2')
  .expect('stdout', '12')
  .expect('code', 0)
  .end();
```
 
## Rule
 
### code
 
Check the exit code.
 
```js
coffee.fork('/path/to/file.js', [ 'args' ])
  .expect('code', 0)
  // .expect('code', 1)
  .end();
```
 
### stdout / stderr
 
Check the stdout and stderr.
 
```js
coffee.fork('/path/to/file.js', [ 'args' ])
  .expect('stdout', '12\n')
  .expect('stderr', '34\n')
  .expect('code', 0)
  .end();
```
 
### custom
 
Support custom rules, see `test/fixtures/extendable` for more details.
 
```js
const { Coffee, Rule } = require('coffee');
 
class FileRule extends Rule {
  constructor(opts) {
    super(opts);
    // `args` is which pass to `expect(type, ...args)`, `expected` is the first args.
    const { args, expected } = opts;
  }
 
  assert(actual, expected, message) {
    // do sth
    return super.assert(fs.existsSync(expected), true, `should exists file ${expected}`);
  }
}
 
class MyCoffee extends Coffee {
  constructor(...args) {
    super(...args);
    this.setRule('file', FileRule);
  }
 
  static fork(modulePath, args, opt) {
    return new MyCoffee({
      method: 'fork',
      cmd: modulePath,
      args,
      opt,
    });
  }
}
```
 
Usage:
 
```js
// test/custom.test.js
const coffee = require('MyCoffee');
coffee.fork('/path/to/file.js', [ 'args' ])
  .expect('file', `${root}/README.md`);
  .notExpect('file', `${root}/not-exist`);
```
 
## Support multiple process coverage with nyc
 
Recommend to use [nyc] for coverage, you can use [any test frammework supported by nyc](https://istanbul.js.org/docs/tutorials/).
 
## API
 
### coffee.spawn
 
Run command using `child_process.spawn`, then return `Coffee` instance.
 
Arguments see [child_process.spawn](http://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options)
 
### coffee.fork
 
Run command using `child_process.fork`, then return `Coffee` instance.
 
Arguments see [child_process.fork](http://nodejs.org/api/child_process.html#child_process_child_process_fork_modulepath_args_options)
 
### coffee.Coffee
 
Assertion object
 
#### coffee.expect(type, ...args)
 
Assert type with expected value, expected value can be string, regular expression, and array.
 
```js
coffee.spawn('echo', [ 'abcdefg' ])
  .expect('stdout', 'abcdefg')
  .expect('stdout', /^abc/)
  .expect('stdout', [ 'abcdefg', /abc/ ])
  .end();
```
 
Accept type: `stdout` / `stderr` / `code` / `error`, see built-in rules description above.
 
#### coffee.notExpect(type, ...args)
 
The opposite assertion of `expect`.
 
#### coffee.write(data)
 
Write data to stdin.
 
```js
coffee.fork(path.join(fixtures, 'stdin.js'))
  .write('1\n')
  .write('2')
  .expect('stdout', '1\n2')
  .end();
```
 
#### coffee.writeKey(...args)
 
Write special key sequence to stdin, support `UP` / `DOWN` / `LEFT` / `RIGHT` / `ENTER` / `SPACE`.
 
All args will join as one key.
 
```js
coffee.fork(path.join(fixtures, 'stdin.js'))
  .writeKey('1', 'ENTER', '2')
  .expect('stdout', '1\n2')
  .end();
```
 
#### coffee.waitForPrompt(bool)
 
If you set false, coffee will write stdin immediately, otherwise will wait for `prompt` message.
 
```js
coffee.fork('/path/to/cli', [ 'abcdefg' ])
  .waitForPrompt()
  .write('tz\n')
  // choose the second item
  .writeKey('DOWN', 'DOWN', 'ENTER');
  .end(done);
```
 
cli process should emit `prompt` message:
 
```js
const readline = require('readline');
 
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});
 
function ask(q, callback) {
  process.send({ type: 'prompt' });
  rl.question(q, callback);
}
 
ask('What\'s your name? ', answer => {
  console.log(`hi, ${answer}`);
  ask('How many coffee do you want? ', answer => {
    console.log(`here is your ${answer} coffee`);
    rl.close();
  });
});
```
 
#### coffee.end([callback])
 
Callback will be called after completing the assertion, the first argument is Error if throw exception.
 
```js
coffee.fork('path/to/cli')
  .expect('stdout', 'abcdefg')
  .end(done);
 
// recommended to left undefind and use promise style.
const { stdout, stderr, code } = await coffee.fork('path/to/cli').end();
assert(stdout.includes(abcdefg));
```
 
#### coffee.debug(level)
 
Write data to process.stdout and process.stderr for debug
 
`level` can be
 
- 0 (default): pipe stdout + stderr
- 1: pipe stdout
- 2: pipe stderr
- false: disable
 
Alternative you can use `COFFEE_DEBUG` env.
 
#### coffee.coverage()
 
If you set false, coffee will not generate coverage.json, default: true.
 
#### coffee.beforeScript(scriptFile)
 
Add a hook script before fork child process run.
 
### coffee.Rule
 
Assertion Rule base class.
 
## LISENCE
 
Copyright (c) 2017 node-modules. Licensed under the MIT license.
 
[nyc]: https://github.com/istanbuljs/nyc