Simple and powerful end-to-end testing for command-line apps.
Nixt is aiming to make testing of command-line apps as simple as possible. It plays nice with the testing tools that you are already using and in case you are one of those guys who practice outside-in BDD, it has the potential to become something that lives in every command-line app that you are going to build.
var nixt = require('nixt');
nixt()
.touch('/tmp/test')
.run('ls /tmp/')
.stdout(/test/)
.end();
Nixt can strip newlines and colors. You can tell it to do so by passing an object that looks like this:
var options = {
colors: false,
newlines: false,
};
nixt(options).stdout...
While Nixt comes with built-in expectations, you can use your own too.
nixt()
.expect(function(result) {
if (result.stdout !== 'unicorns') {
return new Error('NO!');
}
})
.run('unicorns')
.end(fn);
You can register as many before and after middlewares as you wish.
nixt()
.before(setupDatabase)
.before(runMigrations)
.run(cmd)
.after(downgradeCron)
.after(deleteDatabase)
.end();
The Middleware execution order is very simple - "before" middlewares always run before everything else, "after" middlewares always run after everything else. The other middlewares will match the order that you have specified.
nixt()
.before(before1)
.before(before2)
.after(after1)
.after(after2)
.touch(file)
.run(cmd)
.unlink(file)
.end(fn)
// Execution order:
// before1, before2, touch, cmd, unlink, after1, after2
You may also want to reuse before and after middlewares as much as possible, especially when testing something that requires extensive setup and cleanup. You can accomplish this by cloning a Nixt instance.
var base = nixt()
.before(setupDatabase)
.after(removeDatabase);
// Later on
base.clone().run....
Nixt has primitive support for plugins. You can register any expectation or/and
any middleware by calling nixt.register
.
var fn = function() {};
nixt.register('foo', fn);
Or you may want to register many functions at once.
var fn = function() {};
var fn1 = function() {};
nixt.register({ baz: fn, bar: fn1 });
Nixt plays nice with any test runner out there. Here is a minimal example how you could use it with Mocha.
describe('todo add', function() {
it('adds a new todo item', function(done) {
nixt()
.run('todo add')
.stdout('A new todo has been added')
.end(done);
});
});
While using a test runner is recommended nixt is completely 'nodeable'. Here is a simple example how you could accomplish that:
var assert = require('assert');
function refute(err) {
assert(!err);
}
nixt()
.run(cmd)
.end(refute);
nixt()
.run(anotherCmd)
.end(refute);
Register a "before" middleware.
nixt()
.before(fn)
.before(fn2)
.run(cmd)
.end();
Register an "after" middleware.
nixt()
.run(cmd)
.after(fn)
.after(fn2)
.end();
Change the current working directory of the main command (specified with run
).
Please not that this won't affect any other commands like unlink
etc.
nixt()
.cwd(path.join(__dirname, 'node_modules', '.bin'))
.run('mocha --version')
.stdout('1.13.0')
.end();
Set a base command. Useful for templates.
nixt()
.cwd(path.join(__dirname, 'node_modules', '.bin'))
.base('mocha ')
.run('--version')
.stdout('1.13.0')
.end();
Set a primary command to execute:
nixt()
.run('node --version')
.stdout('0.10.16')
.end(fn);
You could also run the test right after specifying the command to run:
nixt()
.stdout('0.10.16')
.run('node --version', fn)
Set environment variables.
nixt()
.env('foo', 'bar')
.env('baz', 'boo')
.run('node --version')
.stdout('0.10.16')
.end(fn);
Set a timeout for the main command that you are about to test.
nixt()
.timeout(1) // ms
.run('cat /dev/null')
.end(fn);
Set expectations on stdout.
nixt()
.stdout('LICENSE Makefile')
.run('ls')
.end(fn);
Works with regular expressions too.
nixt()
.stdout(/system/)
.run('time')
.end(fn);
Same as stdout
but well.. surprise works with stderr.
nixt()
.run('todo add')
.stderr('Please speicfy a todo')
.end(fn);
Expect a given exit code.
nixt()
.run('todo add')
.code(1)
.end(fn);
Check if a given path exists (works with both files and directories).
nixt()
.run('mkdir /tmp/test')
.exist('/tmp/test')
.end(fn);
Check the contents of a file.
nixt()
.writeFile(file, 'Hello')
.run('node void.js')
.match(file, 'Hello')
.unlink(file)
.end(done);
nixt()
.writeFile(file, 'Hello')
.run('node void.js')
.match(file, /ello/)
.unlink(file)
.end(done);
Create a new directory.
nixt()
.mkdir('xml-database')
.run('this does stuff with the xml-database directory')
.end(fn);
Execute a given command.
nixt()
.touch('LICENSE')
.exec('git add -a')
.exec('git commit -m "Add LICENSE"')
.run('git log')
.stdout(/LICENSE/)
.end();
Create a file with or without given contents.
Without:
nixt()
.writeFile(pathToFile)
.end();
With:
nixt()
.writeFile(pathToFile, data)
.end();
Remove a directory.
nixt()
.mkdir('xml-database')
.run('this does stuff with the xml-database directory')
.rmdir('xml-database')
.end(fn);
Unlink a file.
nixt()
.touch('my-file')
.run('this does stuff with my file')
.unlink('my-file')
.end(fn);
Run the given test.
nixt()
.run('ls')
.stdout('this-is-not-porn-i-promise')
.end(function(err) {
});
The same might be accomplished with supplying a function to run
:
nixt()
.stdout('this-is-not-porn-i-promise')
.run('ls', function(err) {
})
Deep clone a Nixt instance.
var clone = nixt()
.before(fn)
.after(fn)
.run('my awesome command')
.end()
.clone();
Register a custom expectation.
nixt()
.expect(function(result) {
if (result.stdout !== 'Unicorns') {
return new Error('OMG');
}
})
.run('ls')
.end(fn);
$ npm install nixt
$ npm install
$ make test
$ make test-cov
$ make jshint
project : nixt
repo age : 5 days
active : 4 days
commits : 59
files : 50
authors :
58 Veselin Todorov 98.3%
1 Radoslav Stankov 1.7%
Special thanks to:
- Alexander Petkov - logo design
- Martin Lazarov - various ideas
- Radoslav Stankov
Do you like this project? Star the repository, spread the word - it really helps. You may want to follow me on Twitter and GitHub. Thanks!
MIT License
Copyright (C) 2013 Veselin Todorov ([email protected])
Permission 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:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE 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.