thunks/tman

Name: tman

Owner: thunks

Description: T-man: Super test manager for JavaScript.

Created: 2016-03-29 15:42:59.0

Updated: 2017-11-01 10:19:35.0

Pushed: 2017-12-30 03:40:58.0

Homepage:

Size: 1437

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

T-man

Super test manager for JavaScript.

NPM version Build Status Coverage Status Downloads

T-man is a refactor version of mocha, but more lightweight, more flexible. In most case, you can use tman replace of mocha directly.

Summary
Examples
Simple tests

It define test cases in top level, and no suites.

t assert = require('assert')
t tman = require('tman')
t Rx = require('rxjs')

count = 0

.it('synchronous test', function () {
sert.strictEqual(count++, 0)


.it('callback style asynchronous test', function (done) {
sert.strictEqual(count++, 1)
tTimeout(done, 100)


.it('promise style asynchronous test', function () {
sert.strictEqual(count++, 2)
turn new Promise(function (resolve) {
assert.strictEqual(count++, 3)
setTimeout(resolve, 100)



.it('thunk style asynchronous test', function () {
sert.strictEqual(count++, 4)
turn function (done) {
assert.strictEqual(count++, 5)
setTimeout(done, 100)



.it('generator style asynchronous test', function * () {
sert.strictEqual(count++, 6)
eld function (done) { setTimeout(done, 50) }
eld new Promise(function (resolve) { setTimeout(resolve, 50) })
sert.strictEqual(count++, 7)


.it('Rx.Observable asynchronous test', function () {
sert.strictEqual(count++, 8)
turn Rx.Observable.fromPromise(new Promise(function (resolve) {
assert.strictEqual(count++, 9)
setTimeout(resolve, 100)
)


ode.js v8
.it('async/await style asynchronous test', async function () {
sert.strictEqual(count++, 10)
ait new Promise(function (resolve) { setTimeout(resolve, 50) })
sert.strictEqual(count++, 11)

Run by T-man CLI (need npm i tman -g):

 example/simple
Mocha style tests

It is a mocha style tests. It only can be run by T-man CLI: tman example/mocha. Through T-man CLI, some method are registered to node global object. And you can use generator as well, it is equal to mocha + thunk-mocha.

t assert = require('assert')

count = 0

ribe('mocha style', function () {
fore(function () {
assert.strictEqual(count++, 0)


ter(function () {
assert.strictEqual(count++, 9)


('synchronous test', function () {
assert.strictEqual(count++, 1)


('callback style asynchronous test', function (done) {
assert.strictEqual(count++, 2)
setTimeout(done, 100)


('promise style asynchronous test', function () {
assert.strictEqual(count++, 3)
return new Promise(function (resolve) {
  assert.strictEqual(count++, 4)
  setTimeout(resolve, 100)
})


('thunk style asynchronous test', function () {
assert.strictEqual(count++, 5)
return function (done) {
  assert.strictEqual(count++, 6)
  setTimeout(done, 100)
}


('generator style asynchronous test', function * () {
assert.strictEqual(count++, 7)
yield function (done) { setTimeout(done, 100) }
assert.strictEqual(count++, 8)


Es-next tests with babel

tman -r babel-register -r babel-polyfill example/es-next.es:

rt assert from 'assert'
rt tman from 'tman'

count = 0
sync "after hook"
.after(async () => {
sert.strictEqual(await Promise.resolve(count++), 4)


.it('async/await asynchronous test', async function () {
sert.strictEqual(await Promise.resolve(count++), 0)
sert.strictEqual(await new Promise((resolve, reject) => {
setTimeout(() => {
  resolve(count++)
}, 100)
, 1)


.it('generator asynchronous test', function * () {
 yield Promise
sert.strictEqual(yield Promise.resolve(count++), 2)
 yield thunk function
sert.strictEqual(yield (done) => {
setTimeout(() => {
  done(null, count++)
}, 100)
 3)

Tests in source code

It shows writing tests in source code. The tests will run in test mode.

Practical tests

It includes nested suites and tests, just simulate practical use case.

Complex tests

It is the test of tman, not only nested suites and tests, but also several tman instance compose!

Usage
Use as CLI

T-man is easiest to use when installed with npm:

install tman -g

Run test in myproject_dir:

yproject_dir && tman

T-man will try to load myproject_dir/test/*.{js,ts,es,coffee} and run it.

Use with npm package.json

npm script in package.json(, also with istanbul):

ipts": {
est": "tman",
est-cov": "istanbul cover _tman"

Then run:

test

or

run test-cov

The tman will try to load tests with glob test/*.js and run them.

You may also run tests with your own globs: tman test/index.js test/service/*.js test/api/*.js.

Assertions

T-man has no built-in assertion method, but allows you to use any assertion library you want, if it throws an error, it will work! You can utilize libraries such as:

Suites and tests
tman.suite(title, fn), tman.describe(title, fn)

You may use suite to organize huge scale tests. describe is an alias of suite. You can define any level of nested suites and test cases.

.suite('User', function () {
an.suite('#save()', function () {
tman.it('should save without error', function * () {
  yield new User('Tman').save()
})


tman.test(title, fn), tman.it(title, fn)

Define test logic, support synchronous or asynchronous test. it is an alias of test.

.it('synchronous test', function () {
 test body


.it('callback style asynchronous test', function (done) {
 test body
tTimeout(done, 100)


.it('promise style asynchronous test', function () {
 test body
turn new Promise(function (resolve) {
// test body
setTimeout(resolve, 100)



.it('thunk style asynchronous test', function () {
 test body
turn function (done) {
// test body
setTimeout(done, 100)



.it('generator style asynchronous test', function * () {
 test body
eld thunk.delay(100)
 test body

Hooks

This hooks can be used to set up preconditions and clean up after your tests. All of them support synchronous or asynchronous function, just like tman.it. You can define any level hooks for suite or test.

tman.before(fn) tman.after(fn) tman.beforeEach(fn) tman.afterEach(fn)
.suite('hooks', function () {

an.before(function () {
// runs before all tests in this block


an.after(function () {
// runs after all tests in this block


an.beforeEach(function () {
// runs before each test in this block


an.afterEach(function () {
// runs after each test in this block


 test cases
an.it('test', function () {
// ...


Exclusive or inclusive tests

only and skip will work as your expectation. If you have more than one only in your tests or suites, only the first only will take effect, all other will not be read.

tman.suite.only(title, fn) tman.it.only(title, fn) tman.suite.skip(title, fn) tman.it.skip(title, fn)
.suite('Array', function () {
an.suite('#indexOf()', function () {
tman.it.only('should return -1 unless present', function () {
  // ...
})

tman.it('should return the index when present', function () {
  // ...
})


tman.grep(pattern)

Sets grep pattern and run tests matching pattern, same as --grep <pattern> CLI option.

tman.exclude(pattern)

Sets exclude pattern and exclude tests matching pattern, same as --exclude <pattern> CLI option.

Timeouts

Default timeout is 2000ms.

Suite-level timeouts may be applied to entire test “suites”, or disabled via this.timeout(0). This will be inherited by all nested suites and test-cases that do not override the value.

.suite('a suite of tests', function () {
is.timeout(500)

an.it('should take less than 500ms', function (done) {
setTimeout(done, 300)


an.it('should take less than 500ms as well', function (done) {
setTimeout(done, 200)


Test-specific timeouts may also be applied, or the use of this.timeout(0) to disable timeouts all together.

.it('should take less than 500ms', function (done) {
is.timeout(500)
tTimeout(done, 300)

Write tests in source code
tman(title, fn) tman.only(title, fn) tman.skip(title, fn)

You can write tests in your source code:

rts.stringify = function (val) {
turn val == null ? '' : String(val)


('test in source code', function () {
nst assert = require('assert')

an.it('stringify', function () {
assert.strictEqual(exports.stringify(), '')
assert.strictEqual(exports.stringify(null), '')
assert.strictEqual(exports.stringify(0), '0')
assert.strictEqual(exports.stringify(false), 'false')
assert.strictEqual(exports.stringify(NaN), 'NaN')


The tests will only run in test mode.

Run tests
tman.run([callback])

You can run the tests programmatically:

un: `node example.js`
.suite('User', function () {
an.suite('#save()', function () {
tman.it('should save without error', function * () {
  yield new User('Tman').save()
})

 others


.run()

If you run tests with CLI, you will not need to use tman.run, the tman command will run tests automatically.

tman.mocha()

Enable mocha compatible mode, same as --mocha CLI option.

tman.reset()

Clear all tests of tman instance.

tman.loadFiles(filePath, sort)

Load test files to tman, it will clear previous tests file that in require.cache.

tman.globals(globals)

Set the given globals.

T-man CLI
an --help

e: tman [debug] [options] [files]

tions:

-h, --help                           output usage information
-V, --version                        output the version number
-c, --colors                          force enabling of colors
-C, --no-colors                       force disabling of colors
-d, --debug                          enable node\'s debugger, synonym for node --debug
-e, --exclude <pattern>              exclude tests matching <pattern>
-g, --grep <pattern>                 run tests matching <pattern>
-gc, --expose-gc                     expose gc extension
-r, --require <name>                 require the given module
-R, --reporter <name>                specify the reporter to use [spec]
-t, --timeout <ms>                   set test-case timeout in milliseconds [2000]
--debug-brk                          enable node\'s debugger breaking on the first line
--es_staging                         enable all staged features
--globals <names>                    allow the given comma-delimited global [names]
--harmony<_classes,_generators,...>  all node --harmony* flags are available
--icu-data-dir                       include ICU data
--mocha                              Mocha compatible mode
--no-sort                            don\'t sort test files
--no-timeout                         disables timeouts, given implicitly with --debug
--no-exit                            require a clean shutdown of the event loop: T-man will not call process.exit
--opts <path>                        specify opts path
--perf-basic-prof                    enable perf linux profiler (basic support)
--preserve-symlinks                  Instructs the module loader to preserve symbolic links when resolving and caching modules
--reporters                          display available reporters
--throw-deprecation                  throw an exception anytime a deprecated function is used
--trace                              trace function calls
--trace-deprecation                  show stack traces on deprecations
--use_strict                         enforce strict mode
T-man test mode

There are 3 ways to run with test mode:

  1. tman example/test_in_source_code.js
  2. node example/test_in_source_code.js --test
  3. TEST=* node example/test_in_source_code.js
TypeScript Typings
rt * as tman from 'tman'
rt { tman, suite, it, before, after, beforeEach, afterEach } from 'tman'
Reporters
spec

spec reporter

dot

dot reporter

base

base reporter

FAQ
How to run CoffeeScript (or TypeScript) tests?

Use --require option:

  1. tman -r coffee-script/register test/*.coffee
  2. tman -r ts-node/register test/*.ts

Here is a simple example. You can require one more modules.

License

T-man is licensed under the MIT license. Copyright © 2016-2017 thunks.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.