Coffee
Test command line on Node.js.
Install
$ npm i coffee --save-dev
Usage
Coffee is useful for test command line in test frammework (like Mocha).
describe('cat', function() {
it('should concat input', function(done) {
const coffee = require('coffee');
coffee.spawn('cat')
.write('1')
.write('2')
.expect('stdout', '12')
.expect('code', 0)
.end(done);
})
})
You can also use fork for spawning Node processes.
coffee.fork('/path/to/file.js', [ 'args' ])
.expect('stdout', '12\n')
.expect('stderr', '34\n')
.expect('code', 0)
.end(done);
In file.js
console.log(12);
console.error(34);
Support multiple process coverage with nyc
Recommend to use nyc for coverage, you can use any test frammework supported by nyc.
API
coffee.spawn
Run command using child_process.spawn
, then return Coffee
instance.
Arguments see child_process.spawn
coffee.fork
Run command using child_process.fork
, then return Coffee
instance.
Arguments see child_process.fork
coffee.Coffee
Assertion object
coffee.expect(type, expected)
Assert type with expected value, expected value can be string, regular expression, and array.
coffee.spawn('echo', [ 'abcdefg' ])
.expect('stdout', 'abcdefg')
.expect('stdout', /^abc/)
.expect('stdout', [ 'abcdefg', /abc/ ])
.end(done);
Accept type: stdout
, stderr
, code
, error
coffee.notExpect(type, expected)
The opposite assertion of expect
.
coffee.write(data)
Write data to stdin, see example above.
coffee.waitForPrompt(bool)
If you set false, coffee will write stdin immediately, otherwise will wait for prompt
message.
coffee.fork('/path/to/cli', [ 'abcdefg' ])
.waitForPrompt()
.write('tz\n')
.write('2\n');
.end(done);
cli process should emit prompt
message:
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.
coffee.fork('path/to/cli')
.expect('stdout', 'abcdefg')
.end(done);
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
you could add your custom rule, see test/fixtures/extendable
for more details.
const { Coffee, Rule } = require('coffee');
class FileRule extends Rule {
assert(actual, expected, message) {
return super.assert(content, pattern, message);
}
}
class MyCoffee extends Coffee {
constructor(...args) {
super(...args);
this.setRule('file', FileRule);
}
}
LISENCE
Copyright (c) 2017 node-modules. Licensed under the MIT license.