JUnit
A simple promise based function for unit tests. I believe we shouldn't waste time on learning, debugging and waiting the unit test framework itself, that's why I created JUnit. It's just a curried function, everything inside is controllable, nothing will be fancy.
Install
Node.js
npm install junit
then you can var junit = require("junit").default
or import junit from "junit"
.
Browser
You can to use something like browserify
or webpack
,
or download the bundled junit.js
.
A real world example.
Features
- Supports both Node.js and old browsers
- Should work well from ES3 to ES7
- Make it super easy to concurrently test async functions, designed for
async-await
- Automatically garbage collect the unhandled error
- Full customizable report style
- Not a single global variable pollution
- Only one dependency, light weight and behavior predictable
FAQ
-
I don't want to use
async-await
.No problem. Just replace all the await expresses with standard promise ones is enough.
-
I cannot
require('junit')
.For non-es6, use
require('junit').default
. -
IE6?
The core framework of JUnit will work. But the default reporter only supports IE8>=, you may have to install & config to another reporter to support old browsers.
CLI
Install junit globally: npm i -g junit
.
It will automatically take advantage of the babel
if
you have installed it globally.
For example, created a file test/fib-test.js
,
it should export a function, if the function is async it should return a promise, such as:
; moduleexports = async { await ; ; ; ;};
Run the tests via junit test/*.js
.
For more documentation, run junit -h
.
To watch and auto-rerun test please use noe
:
noe -b junit -w 'test/*.js' -- 'test/*.js'
API
- junit(opts)
- run()
- eq(actual, expected, maxDepth)
- describe(msg, fn)
- junit.reporter(opts)
- junit.Promise
- junit.yutils
-
## junit(opts)
A simple promise based module for unit tests.
-
param:
opts
{ Object }Defaults:
true// Stop test when error occurred.isBail: trueisFailOnUnhandled: true// If any test failed, throw on final.isThrowOnFinal: true// Fail a test after timeout.timeout: 5000reporter:// You can even use jsdiff here to generate more fancy error info.{}{}{}{} -
return: { Function }
(msg, fn) => Function
Themsg
can be anything. Thefn
's first param is a function(after) =>
, you can pass a after hook to it. -
example:
;var it = ;async {// Async tests.;;// Run sync tests within the main async flow.await;it;}; -
example:
Filter the tests, only the message starts with "test" will be tested.
;var it =;async {;;;// Get the result of the test.var total tested passed failed = await it;console;};
-
-
## run()
Start the tests.
-
return: { Promise }
It will resolve
{ total, passed, failed }
-
-
## eq(actual, expected, maxDepth)
A smart strict deep equality assertion helper function. If any of the arguments is promise, it will be auto-resolved before comparision.
-
param:
actual
{ Any } -
param:
expected
{ Any } -
param:
maxDepth
{ Number = 7 }Optional. The max depth of the recursion check.
-
return: { Promise }
-
-
Extend the msg of the test with a new test closure.
-
param:
msg
{ Any }The msg object of the test.
-
param:
fn
{ Function }(it) => Promise
The new msg closure. -
return: { Promise }
-
example:
;var it = ;var eq = it;it;it;
-
-
An example reporter for junit.
-
param:
opts
{ Object }Defaults:
prompt: String // The prompt prefixmode: "console" // "console", "browser" or "none" -
return: { Function }
() => Object
. -
example:
var it = ;
-
-
The promise class that junit uses: Yaku
- type: { Object }
-
## junit.yutils
The promise helpers: Yaku Utils
- type: { Object }