# kludjs

> Ultimately minimal unit-testing library

Latest version **1.0.0** (published 2014-12-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install kludjs
pnpm add kludjs
yarn add kludjs
bun add kludjs
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2014-12-18 |
| First published | 2014-12-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Serge Zaitsev |
| Maintainers | zserge |
| Keywords | unit, test, testing, minimal, assert, spy, mock |

## Links

- npm: https://www.npmjs.com/package/kludjs
- npm.io page: https://npm.io/package/kludjs

## Alternatives

- [duck](https://npm.io/package/duck.md) — 4.2M weekly downloads
- [ava](https://npm.io/package/ava.md) — 560.2K weekly downloads
- [storybook-addon-module-mock](https://npm.io/package/storybook-addon-module-mock.md) — 71.7K weekly downloads
- [vest](https://npm.io/package/vest.md) — 50.1K weekly downloads
- [@ethereum-waffle/mock-contract](https://npm.io/package/@ethereum-waffle/mock-contract.md) — 40.0K weekly downloads

## Recent versions

- 1.0.0 (latest) — 2014-12-18

## README

klud.js
=======

Klud.js (_kludges_) is an ultimately minimal unit testing library.

Briefly, klud.js gives you just four functions:

* `test()` to write tests or to configure test reporting
* `ok()` to do assertions
* `eq()` to compare complex data
* `spy()` to make spies (testable function wrappers)

Installation
------------

Download `klud.js` or `klud.min.js` if you want a core version without any test reporters.

Download `kludjs.js` or `kludjs.min.js` if you want to use it in browser or
Node:

	var test = require('kludjs');
	test('My first test', function() {
		...
	});

Usage
-----

If you're using a bare minimal version you may want to setup a function to
report about passed/failed assertions etc:

	test(function(e, test, msg) {
		switch (e) {
			case 'begin':
				console.log('Test started: ' + test);
				break;
			case 'end':
				console.log('Test finished: ' + test);
				break;
			case 'pass':
				console.log('Assertion passed: ' + test + ':' + msg);
				break;
			case 'fail':
				console.log('Assertion failed: ' + test + ':' + msg);
				break;
			case 'except':
				console.log('Unhandled exception: ' + test + ':' + msg);
				break;
		}
	});

For Node and Browser version you already have a simple reporter included.

Now you can start writing your tests:
	
	var test = require('kludjs');

	function isEven(x) {
		return x % 2 == 0;
	}

	test('Testing isEven', function() {
		ok(isEven(0), 'Zero is even');
		ok(isEven(1) == false, 'One is odd');
		ok(isEven(12), 'Twelve is even');
	});

`test()` is a test declaration. Inside a test function you can use `ok()` to do
assertions.

If your assertion needs to compare objects or arrays you can use `eq()` function:

	test('Equal objects', function() {
		ok(eq([1, 2, 3], [1, 2, 3]), 'Arrays are equal');
		ok(eq({a:1, b:false}, {b:false, a:1}), 'Objects are equal');
	});

Finally, if you have asynchronous code (I bet you have if you're using
javascript) you can use async tests and spies to test your functions.

Asynchronous test looks very similar to normal tests, except for a `next`
parameter is added to a test routine and the third parameter is added to the
`test` function:

	test('Async test', function(next) {
		setTimeout(function() {
			ok(1 == 1, 'it works!');
			next();
		}, 1000);
	}, true);

All asynchronous tests are put to the queue and executed one by one, so be
careful to not miss the `next()` call when your async test is finished.

A spy wraps a normal function but remembers function calls and thrown
exceptions:

	test('Function should be called', function() {
		var f = spy();
		ok(!f.called, 'function is not called yet');
		f();
		ok(f.called, 'function is called');
	});

	test('Function should throw exception', function() {
		var f = spy(function() {
			var a = unknownVariable; // should throw an exception
		});
		f();
		ok(f.thrown.length == 1, 'exception thrown');
	});

`called` and `thrown` are arrays. The first one contains the lists of arguments
for every spy call, and the second one contains exception objects for every
function call or `undefined` if no exception was thrown during the call.

Spy without an underlying function behaves like a function that returns
`undefined`. It's useful to test is the callbacks are called in the right time
in the right place with the right arguments.

That's all for now. Have fun!

---
_Source: https://npm.io/package/kludjs · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
