• Stars
    star
    308
  • Rank 135,712 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 11 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Simple and elegant end-to-end testing for command-line apps

NPM version Build Status

Nixt

Synopsis

Simple and powerful end-to-end testing for command-line apps.

Description

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 devs 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.

How it looks

var nixt = require('nixt');

nixt()
.exec('touch /tmp/test')
.run('ls /tmp/')
.stdout(/test/)
.end();

Formatting options

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...

Custom expectations

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

Custom middlewares

You can register as many before and after middlewares as you wish.

nixt()
.before(setupDatabase)
.before(runMigrations)
.run(cmd)
.after(downgradeCron)
.after(deleteDatabase)
.end();

Middleware order

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)
.writeFile(file, '')
.run(cmd)
.unlink(file)
.end(fn)

// Execution order:
// before1, before2, writeFile, 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....

Plugins

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

Usage with a test runner

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

Usage without a test runner

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

Responding to interactive prompts

Nixt can respond to apps that run interactively using the on() and respond() functions.

nixt()
.run(cmd)
.on('Your name: ').respond('Joe User\n')
.end();

See test/prompt.test.js for more examples.

API

#before

Register a "before" middleware.

nixt()
.before(fn)
.before(fn2)
.run(cmd)
.end();

#after

Register an "after" middleware.

nixt()
.run(cmd)
.after(fn)
.after(fn2)
.end();

#cwd

Change the current working directory of the main command (specified with run). Please note that this won't affect any other commands like unlink etc.

nixt()
.cwd(__dirname)
.run('pwd')
.stdout(/test$/)
.end();

#base

Set a base command. Useful for templates.

nixt()
.base('node ')
.run('--version')
.stdout('0.10.16')
.end();

#run

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)

#stdin

Set the contents of stdin.

nixt()
.stdin('foobar')
.run('rev')
.stdout('raboof')
.end(fn);

#env

Set environment variables.

nixt()
.env('foo', 'bar')
.env('baz', 'boo')
.run('node --version')
.stdout('0.10.16')
.end(fn);

#timeout

Set a timeout for the main command that you are about to test.

nixt()
.timeout(1) // ms
.run('cat /dev/null')
.end(fn);

#stdout

Set expectations on stdout.

nixt()
.stdout('LICENSE Makefile')
.run('ls')
.end(fn);

Works with regular expressions too.

nixt()
.stdout(/system/)
.run('time')
.end(fn);

#stderr

Same as stdout but well.. surprise works with stderr.

nixt()
.run('todo add')
.stderr('Please speicfy a todo')
.end(fn);

#code

Expect a given exit code.

nixt()
.run('todo add')
.code(1)
.end(fn);

#exist

Check if a given path exists (works with both files and directories).

nixt()
.run('mkdir /tmp/test')
.exist('/tmp/test')
.end(fn);

#notExist

Check if a given path does not exist (works with both files and directories).

nixt()
.run('rm /tmp/file')
.notExist('/tmp/file')
.end(fn);

#match

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

#mkdir

Create a new directory.

nixt()
.mkdir('xml-database')
.run('this does stuff with the xml-database directory')
.end(fn);

#exec

Execute a given command.

nixt()
.writeFile('LICENSE', 'MIT License')
.exec('git add -a')
.exec('git commit -m "Add LICENSE"')
.run('git log')
.stdout(/LICENSE/)
.end();

By default the commands will inherit the "world" for the main command which includes environment variables, cwd, timeout. However, you can override this by supplying a different "world":

nixt()
.exec('git add LICENSE', { timeout: 4, cwd: '/tmp' })
.run('git log')
.stdout(/LICENSE/)
.end();

#writeFile

Create a file with or without given contents.

Without:

nixt()
.writeFile(pathToFile)
.end();

With:

nixt()
.writeFile(pathToFile, data)
.end();

#rmdir

Remove a directory.

nixt()
.mkdir('xml-database')
.run('this does stuff with the xml-database directory')
.rmdir('xml-database')
.end(fn);

#unlink

Unlink a file.

nixt()
.writeFile('my-file', data)
.run('this does stuff with my file')
.unlink('my-file')
.end(fn);

#on

Detect a prompt for user input. Accepts a String or RegExp that appears in the the stdout stream. Must be paired with #respond.

nixt()
.run(cmd)
.on('Your name: ').respond('Joe User\n')
.end();

#respond

Write a response to the stdin stream when a prompt is detected.

See #on

#end

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) {

})

#clone

Deep clone a Nixt instance.

var clone = nixt()
.before(fn)
.after(fn)
.run('my awesome command')
.end()
.clone();

#expect

Register a custom expectation.

nixt()
.expect(function(result) {
  if (result.stdout !== 'Unicorns') {
    return new Error('OMG');
  }
})
.run('ls')
.end(fn);

Installation

$ npm install nixt

Tests

Running the tests

$ make

Credits

Special thanks to:

Support the author

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!

License

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.

More Repositories

1

hippie

End-to-end API testing made easy
JavaScript
556
star
2

timekeeper

Easy testing of time-dependent code. Node.js
JavaScript
423
star
3

todo

Todos in the CLI like what.
JavaScript
400
star
4

issues

GitHub Issues from the CLI.
JavaScript
211
star
5

r...e

Powerful Range implementation. Node.js & browsers
JavaScript
93
star
6

logme

Minimalistic logger for Node.js.
JavaScript
89
star
7

teacher

Spell check for Node.js
JavaScript
79
star
8

bot

Feeling lonely? You personal bot is here.
JavaScript
68
star
9

temporary

Temporary files and dirs for Node.js
JavaScript
68
star
10

sourcery

Consume any RESTful API with just a few lines of JavaScript
JavaScript
66
star
11

word

Functions that assist in working with strings. Node.js
CoffeeScript
56
star
12

pin

Is my site up? Node.js edition.
JavaScript
55
star
13

curiosity

Curious metrics about your JavaScript project
JavaScript
51
star
14

jsmd

jsmd ensures that you will never have outdated and non-working JavaScript code in your README files.
JavaScript
50
star
15

jack

Test-double framework for Node.js and browsers
JavaScript
41
star
16

fine

Tiny, recursive and synchronous file finder utility
JavaScript
35
star
17

moni

Process monitoring with node.
JavaScript
29
star
18

robber.py

BDD / TDD assertion library for Python
Python
29
star
19

let

Force timeouts. Node.js.
JavaScript
27
star
20

b

Benchmarks for Node.js
JavaScript
26
star
21

package

Easy package.json exports.
JavaScript
24
star
22

cyclop

Highly focused async. Does one thing... like a boss.
JavaScript
20
star
23

captureme

CLI utility that helps you capture screenshots via browsers in the cloud
JavaScript
19
star
24

box

Powerful key -> value storage for the CLI.
JavaScript
15
star
25

hell

Experimental complexity analyzer for JavaScript projects
JavaScript
11
star
26

mill

Readable milliseconds for you and other humans. Node.js
JavaScript
11
star
27

structure

Struct generator. Node.js
JavaScript
10
star
28

storr

The most simple JSON storage created ever.
JavaScript
9
star
29

hydro

JavaScript
9
star
30

.dotfiles

Ponnies!
Vim Script
9
star
31

core_ext

JavaScript extensions.
JavaScript
8
star
32

redr

Lazy require with filter logic for Node.js
JavaScript
7
star
33

seed-forge

Factory builder for Mongoose & Seed
JavaScript
6
star
34

surround

Surround a method, save a polar bear.
JavaScript
6
star
35

times

times loop for your coffee.
CoffeeScript
6
star
36

refractory

Thin require wrapper that will allow you to load files from conventional paths
JavaScript
6
star
37

obsessed

Retry mechanism for Node.js and the browsers
JavaScript
5
star
38

tryc

Async try-catch for browsers and Node.js
JavaScript
4
star
39

decorator

JavaScript decorators. POW! Arrghhh!
JavaScript
4
star
40

babel-plugin-react-auto-display-name

Add displayName to React.createClass calls
JavaScript
4
star
41

gist-client

GitHub Gists from the cli.
JavaScript
4
star
42

exports

Easy data exports to your client-side scripts.
JavaScript
4
star
43

socialcount

Get social stats for given url
JavaScript
4
star
44

eyehurt

Color utility for zombies
JavaScript
3
star
45

racks

Reusable middleware implementation for Node.js & the browsers.
JavaScript
3
star
46

freedom

Mind-blowing issue tracker powered by Ruby on Rails.
Ruby
3
star
47

evts

Minimal event emitter implementation that supports both sync and async event handlers
JavaScript
3
star
48

koa-http-logger

koa http request & response logger
JavaScript
2
star
49

printr

Print utils
JavaScript
2
star
50

mini

Minimalistic test runner
JavaScript
2
star
51

chai-mongoose

Mongoose model assertions for chai.js
JavaScript
2
star
52

vim-swag

Turn your swag on. Dark color scheme for Vim
Vim Script
2
star
53

stylec

JavaScript
1
star
54

gorilla

JavaScript
1
star
55

globalo

Top-level object detector for Node.js, Titanium, Titanium + Alloy, browsers
JavaScript
1
star
56

to-date

Time ago/from now date generator
JavaScript
1
star
57

jstore

Minimalistic JSON-based persistence layer
JavaScript
1
star
58

generator-mod

Yeoman Node.js module generator
JavaScript
1
star
59

homepath

$HOME
JavaScript
1
star
60

seltron

Tiny Selenium spawner
JavaScript
1
star
61

pcal

Go
1
star
62

surround.rb

Ruby port of surround.js
Ruby
1
star
63

vim-sws

Example code from "Testing Vim plugins with Vimrunner and RSpec" on Vim Ninjas
Ruby
1
star
64

fload

JavaScript file loader for browsers and Node.js
JavaScript
1
star
65

copycat

HTTP request and response recorder
JavaScript
1
star