mirror of https://github.com/n2geoff/testit.git
initial commit
This commit is contained in:
commit
9cc5d03b79
|
@ -0,0 +1,82 @@
|
||||||
|
# Test.it
|
||||||
|
|
||||||
|
> A minimalistic testing library
|
||||||
|
|
||||||
|
**Test.it** is a small testing library for people that want to live in code, not tests. No over engineering here. Inspired by the simplicity of libraries like [Tape](https://github.com/substack/tape), but the implementation ideas of [TinyTest](https://github.com/joewalnes/jstinytest)
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
By default, you can run your tests like
|
||||||
|
|
||||||
|
```js
|
||||||
|
test.it({
|
||||||
|
'my passing test': function() {
|
||||||
|
test.pass();
|
||||||
|
},
|
||||||
|
'my failing test': function() {
|
||||||
|
test.fail('just wanted to fail fast');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
`test.it` will return `true` if the tests pass or `false` otherwise, in addition you should see the following console output
|
||||||
|
|
||||||
|
```
|
||||||
|
+ my passing test
|
||||||
|
- my failing test
|
||||||
|
- - Error: just wanted to fail fast
|
||||||
|
...error stack...
|
||||||
|
# tests 1 pass 1 fail 0
|
||||||
|
```
|
||||||
|
|
||||||
|
A `+` will proceed test lines that pass and a `-` for those that fail, the trace back `file:line` is included after the failing test proceeded by `- -`
|
||||||
|
|
||||||
|
### Optional Next
|
||||||
|
|
||||||
|
In addition to the default operation, `test.it` provides an optional `next` functional parameter that will return the results as an `object` for you to process *however* you like
|
||||||
|
|
||||||
|
**For Fans of [TinyTest](https://github.com/joewalnes/jstinytest)**
|
||||||
|
|
||||||
|
```js
|
||||||
|
test.it({
|
||||||
|
'my passing test': function() {
|
||||||
|
test.pass();
|
||||||
|
}
|
||||||
|
}, function(results) {
|
||||||
|
if (window.document && document.body) {
|
||||||
|
document.body.style.backgroundColor = (
|
||||||
|
results.fail.length ? '#ff9999' : '#99ff99'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
### Sample Results Object
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"pass": ['list of passed tests', ...],
|
||||||
|
"fail": ['list of errored tests', ...],
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
From this you can easily find the number of tests ran `pass.length`, number of failed tests `fail.length` or the total test count by adding the two. Simple.
|
||||||
|
|
||||||
|
## Methods
|
||||||
|
|
||||||
|
To stay minimal, `test.it` only provides 6 testing methods
|
||||||
|
|
||||||
|
| Method | Description |
|
||||||
|
| ------------------------------- | ----------------------------- |
|
||||||
|
| `test.pass()` | pass test |
|
||||||
|
| `test.fail(message)` | fails test with message |
|
||||||
|
| `test.exists(value)` | check if value exists |
|
||||||
|
| `test.assert(expected, actual)` | evaluates results using `==` |
|
||||||
|
| `test.equals(expected, actual)` | evaluates results using `===` |
|
||||||
|
|
||||||
|
> NOTE: wish `eval` was not so evil, `assert(expression, message)` would be ideal
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
MIT
|
||||||
|
|
|
@ -0,0 +1,46 @@
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
const test = {
|
||||||
|
it: function(tests, next) {
|
||||||
|
// capture results
|
||||||
|
let failed = [];
|
||||||
|
let passed = [];
|
||||||
|
|
||||||
|
// loop through tests
|
||||||
|
Object.keys(tests).forEach(function(name) {
|
||||||
|
let test = tests[name];
|
||||||
|
|
||||||
|
// execute
|
||||||
|
try {
|
||||||
|
test();
|
||||||
|
passed.push(`\n+ ${name}`);
|
||||||
|
} catch (err) {
|
||||||
|
// TODO: include error stack option
|
||||||
|
// let back = err.stack.split('\n')[2];
|
||||||
|
// let re = /(?<=\()(.*?)(?=\))/g;
|
||||||
|
// let trace = re.exec(back)[0];
|
||||||
|
failed.push(`\n- ${name}`);
|
||||||
|
console.error(err);
|
||||||
|
// failed.push(`\n- ${name} - - ${trace}`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// summary
|
||||||
|
if(typeof next === 'function') {
|
||||||
|
return next({
|
||||||
|
pass: passed,
|
||||||
|
fail: failed
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
console.log(...passed, ...failed);
|
||||||
|
console.log(`\n# tests ${failed.length + passed.length} pass ${passed.length} fail ${failed.length}`);
|
||||||
|
|
||||||
|
return failed.length ? false : true;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
assert: function (e, a) { if (e != a) throw Error(`expected ${e} == ${a}`); },
|
||||||
|
equals: function (e, a) { if (e !== a) throw Error(`expected ${e} === ${a}`); },
|
||||||
|
exists: function (v) { if (!v) throw Error(`exists value ${v}`); },
|
||||||
|
pass: function () { return true; },
|
||||||
|
fail: function (m) { throw Error(m); }
|
||||||
|
}
|
|
@ -0,0 +1,58 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<meta http-equiv="x-ua-compatible" content="ie=edge">
|
||||||
|
<title>Test It Spec</title>
|
||||||
|
<script src="../src/test.it.js"></script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<script>
|
||||||
|
test.it({
|
||||||
|
"'assert' should exist": function() {
|
||||||
|
test.exists(test.assert);
|
||||||
|
},
|
||||||
|
"truty assert should work": function() {
|
||||||
|
test.assert(1, '1');
|
||||||
|
test.assert('1', 1);
|
||||||
|
},
|
||||||
|
"'equals' should exist": function() {
|
||||||
|
test.exists(test.equals);
|
||||||
|
},
|
||||||
|
"'equals' should be exact": function() {
|
||||||
|
test.equals(1,1);
|
||||||
|
test.equals('hello', 'hello');
|
||||||
|
},
|
||||||
|
"'pass' should exist": function() {
|
||||||
|
test.exists(test.pass);
|
||||||
|
},
|
||||||
|
"'fail' should exist": function() {
|
||||||
|
test.exists(test.fail);
|
||||||
|
},
|
||||||
|
"'exists' should exist": function() {
|
||||||
|
test.exists(test.exists);
|
||||||
|
}
|
||||||
|
}, function(r) {
|
||||||
|
if (window.document && document.body) {
|
||||||
|
document.body.style.backgroundColor = (
|
||||||
|
r.fail.length ? "#ff9999" : "#99ff99"
|
||||||
|
);
|
||||||
|
|
||||||
|
r.pass.forEach((p) => document.write(`<br>${p}`));
|
||||||
|
r.fail.forEach((f) => document.write(`<br><b>${f}</b>`));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
function assert(exp, msg) {
|
||||||
|
try {
|
||||||
|
let val = eval(exp);
|
||||||
|
|
||||||
|
} catch (err) {
|
||||||
|
console.log(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
Reference in New Issue