ThunkTest
Modular testing for JavaScript. Declare tests as thunks, then execute them with a call.
const Test = const identity = value // identity// ✓ identity(1) -> 1// ✓ identity('hey') -> 'hey'// ✓ identity(NaN) |> result => assert(isNaN(result))// ✓ returns whatever was passed to it
thunk Tests are composed of a string descriptor, a function to test, and test cases denoted by .case
and .throws
. Any test cases may be asynchronous - either by returning a Promise explicitly or using the async
keyword. Both .case
and .throws
accept a variadic number of arguments - the same as those provided to the function - with the exception of the last argument:
- not a function - compare the return value directly by SameValueZero
- an asserter function - pass the return value to the asserter function and let the asserter handle all the assertions. Note that if this value is a Promise, it is resolved before calling this function
Testtestname stringtester function case...argsexpectedResult any case...argsasserter Promise|disposer function|null throws...argsexpectedError Error throws...argserrorAsserter Promise|disposer function|null
Concisely test many different cases with a declarative, idiomatic API.
// objects deep equal, otherwise strict equal // can supply a callback // async ok // -- pipe: awesome username generator// ✓ pipeline('deimos') -> '_xXxDEIMOSxXx_'// ✓ pipeline('|') -> result => assert.equal(result, '_xXx|xXx_')// ✓ pipeline(1) throws TypeError: string.toUpperCase is not a function// ✓ pipeline(null) throws; (err, arg0) => {// assert.strictEqual(arg0, null)// assert.strictEqual(err.name, 'TypeError')// assert.strictEqual(err.message, 'Cannot read property \'toUpperCase\' of null')// }// ✓ pipeline('?') -> async result => assert.equal(result, '_xXx?xXx_')// ✓ pipeline(NaN) throws; async (err, arg0) => {// assert.strictEqual(arg0, NaN)// assert.strictEqual(err.name, 'TypeError')// assert.strictEqual(err.message, 'string.toUpperCase is not a function')// }
Preprocessing and postprocessing are available with callbacks supplied to .before
and .after
.
Note: since all callbacks are run with the same context, you can get and set values in the execution context (this
) of a thunk Test from any provided callback.
// -- square// ✓ square(3) |> function (squared) {// assert(squared == 9)// assert(this.hello == 'world')// }
Syntax
= before: function=>this after: function=>this beforeEach: function=>this afterEach: function=>this : ...argsexpectedResult|function=>disposer Promise<>||=>this : ...argsexpectedError|function=>disposer Promise<>||=>this Test ThunkTest Test ThunkTest
Installation
with npm
npm i thunk-test
browser script, global Test
browser module