replace expect with simpler assert

This commit is contained in:
Geoff Doty 2020-09-20 15:58:00 -04:00
parent 7e268c6303
commit cacc7a5d33
7 changed files with 85 additions and 162 deletions

View File

@ -2,7 +2,7 @@
> A minimalistic testing library > A minimalistic testing library
**Test.it** is a small testing library for people that want to live in code, not in tests. No over engineering here. Inspired by the simplicity of libraries like [Tape](https://github.com/substack/tape),but the implementation ideas of things like [Expect](https://github.com/Automattic/expect.js) and [TinyTest](https://github.com/joewalnes/jstinytest) **Test.it** is a small client-side testing library for people that want to live in code, not in tests. No over engineering here. Inspired by the simplicity of libraries like [Jasmine](https://jasmine.github.io/), but implementation ideas based on [TinyTest](https://github.com/joewalnes/jstinytest)
This is probally not a *cure-all* testing solution, if you want something more robust checkout [Jasmine](https://jasmine.github.io/), [Tape](https://github.com/substack/tape) or [Mocha](https://mochajs.org/) -- this is to... This is probally not a *cure-all* testing solution, if you want something more robust checkout [Jasmine](https://jasmine.github.io/), [Tape](https://github.com/substack/tape) or [Mocha](https://mochajs.org/) -- this is to...
@ -11,13 +11,12 @@ This is probally not a *cure-all* testing solution, if you want something more r
### Features ### Features
- Designed for the Browser - Designed for the Browser
- Works with NodeJS
- *Under* a 100 lines - *Under* a 100 lines
- Single File - Single File
- No Dependicies - No Dependicies
- 2kb footprint (*before gzip*) - 2kb footprint (*before gzip*)
- Extend with custom reporters - Extend with custom reporters
- Has an Expect-like style BDD assertions - Uses Simple Assert
**No Bloat Here!** **No Bloat Here!**
@ -29,12 +28,14 @@ This is probally not a *cure-all* testing solution, if you want something more r
By default, you can run your tests like By default, you can run your tests like
```js ```js
import test from 'testit';
test.it({ test.it({
'my passing test': function() { 'my passing test': function() {
test.expects().to.pass(); test.assert(true);
}, },
'my failing test': function() { 'my failing test': function() {
test.expects().to.fail('just wanted to fail fast'); test.assert(true === false, 'just wanted to fail fast');
} }
}).run(); }).run();
``` ```
@ -68,7 +69,7 @@ For Example...
```js ```js
test.it({ test.it({
'my passing test': function() { 'my passing test': function() {
test.pass(); test.assert(true);
} }
}, function(results) { }, function(results) {
if (window.document && document.body) { if (window.document && document.body) {
@ -92,38 +93,27 @@ From this object you can easily find the number of tests ran `pass.length`, numb
> REMEMBER: you can bypass error output too > REMEMBER: you can bypass error output too
A sample test runner is provided for both **HTML** and **NODE** in the `test/` directory; `run.html` and `run.js` respectfully. A sample test runner is provided for the **BROWSER** in the `test/` directory; `index.html` and `runner.js` respectfully, with the spec in `index.spec.js`.
## Methods ## Methods
To stay minimal, `test.it` only has 3 core functions: To stay minimal, `test.it` only has 3 core functions:
- `it` to capture your tests - `it` to capture your tests
- `run` to execute yours tests - `run` to execute yours tests
- and `expects` to write your assertions - and `assert` to write your assertions
While you can use your own assertion library, the included `expects` provides the following methods for writing your tests: While you can use your own assertion library, the included `assert` evaluates an expression/condition tests:
| Methods | Description |
| --------------------------------- | --------------------------------------- |
| `.expects(tests).to.exist()` | truthy evalution `.exist` or `.be.ok()` |
| `.expects().to.pass()` | pass test |
| `.expects().to.fail(message)` | fails test with message |
| `.expects(this).to.equal(that)` | strictly equal evaluation using `===` |
| `.expects(this).to.be.like(that)` | loose evaluation using `==` |
| `.expects(123).to.be.a('number')` | check typeof value (`.a()` or `.an()`) |
> NOTE: wish `eval` was not so evil, `assert(expression, message)` would be ideal
if you want to shorten test typing try if you want to shorten test typing try
let expect = test.expects; let assert = test.assert;
putting that above your tests will allow you to write like putting that above your tests will allow you to write like
```js ```js
test.it({ test.it({
"my test should work": function() { "my test should work": function() {
expect().to.pass(); assert(true);
} }
}); });
@ -131,8 +121,8 @@ test.it({
## TODO ## TODO
- write `not` expects, ie `expects(this).to.not.equal(this)`
- provide sample test runner for CI environments - provide sample test runner for CI environments
- maybe spec files export results && runner identifies failure
## Support ## Support

View File

@ -48,46 +48,14 @@ const test = {
this._tests = tests; this._tests = tests;
return this; return this;
}, },
"expects": function expects(val) { "assert": (expression, msg) => {
return { try {
"to": { if(!expression) {
"be": { throw new Error(msg || "Assertion Failed");
"a": (type) => { return test.expects(val).to.be.an(type); },
"an": (type) => {
if(["array"].indexOf(type) !== -1) {
if(val.constructor.name.toLowerCase() !== "array") {
throw new Error(`expected ${typeof val} to be an ${type}`);
} }
} catch (e) {
return true; throw new Error(msg);
} }
if(typeof val !== type) {
throw new Error(`expected ${typeof val} to be an ${type}`);
}
},
"ok": () => { return test.expects(val).to.exist(); },
"like": (comp) => {
if(val != comp) {
throw new Error(`expected ${val} == ${comp}`);
}
}
},
"equal": (comp) => {
if(val !== comp) {
throw new Error(`expected ${val} === ${comp}`);
}
},
"exist": () => {
if(!val) {
throw new Error(`expected ${val} to be truthy`);
}
},
"pass": () => { return true; },
"fail": (msg) => { throw new Error(msg); }
}
};
} }
}; };

17
test/index.html Normal file
View File

@ -0,0 +1,17 @@
<!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>
<style>
#summary {font-size: 2rem; text-transform: uppercase;}
</style>
</head>
<body>
<div id="summary"></div>
<div id="errors"></div>
<script type="module" src="./runner.js"></script>
</body>
</html>

View File

@ -1,43 +1,35 @@
function spec(test) { import test from "../src/testit.js";
// npm run test
if(!test) {
try {
var test = require("../dist/testit.umd.js");
} catch(e) {};
}
return { export default test.it({
"'like' should do truthy evaluation via ==": function () { "'like' should do truthy evaluation via ==": function () {
test.expects(1).to.be.like('1'); test.assert(1 == '1');
test.expects("1").to.be.like(1); test.assert(1);
test.expects(1).to.be.ok();
}, },
"'equal' should do === evaluation exist": function () { "'equal' should do === evaluation exist": function () {
test.expects(1).to.equal(1); test.assert(1 === 1);
test.expects('hello').to.equal('hello'); test.assert('hello' === 'hello');
}, },
"you should be able to 'pass' a test": function () { "you should be able to 'pass' a test": function () {
test.expects().to.pass(); test.assert(1);
}, },
"you should be able to fail' a test too": function () { "you should be able to fail' a test too": function () {
try { try {
test.expects().to.fail(); test.assert(0);
} catch (e) { } catch (e) {
test.expects().to.pass();
} }
}, },
"you should be able to test if something 'exists'": function () { "you should be able to test if something 'exists'": function () {
test.expects({}).to.exist(); test.assert({});
test.expects({}).to.be.ok(); test.assert(typeof ({}) === 'object');
test.expects({}).to.be.a('object');
}, },
"should be able to check types": function () { "should be able to check types": function () {
test.expects(123).to.be.a('number');
test.expects([]).to.be.an('array'); test.assert(Array.isArray([]));
test.expects({}).to.be.a('object'); test.assert(typeof (123) === 'number');
test.expects(true).to.be.a('boolean');
test.expects(false).to.be.a('boolean'); test.assert(typeof ({}) === 'object');
test.expects(undefined).to.be.a('undefined'); test.assert(typeof (true) === 'boolean');
} test.assert(typeof (false) === 'boolean');
}; test.assert(typeof (undefined) === 'undefined');
} }
});

View File

@ -1,34 +0,0 @@
<!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="index.spec.js"></script>
<style>
#summary {font-size: 2rem; text-transform: uppercase;}
</style>
</head>
<body>
<div id="summary"></div>
<div id="errors"></div>
<script type="module">
import test from "../src/testit.js";
test.it(spec(test)).run(false, function(r) {
let errors = [];
document.body.style.backgroundColor = (
r.fail.length ? "#ff9999" : "#99ff99"
);
r.pass.forEach((p) => errors.push(`<br>${p}`));
r.fail.forEach((f) => errors.push(`<br><b>${f}</b>`));
document.querySelector('#errors').innerHTML = errors;
document.querySelector('#summary').innerHTML = `tests: ${r.pass.length + r.fail.length} pass: ${r.pass.length} fail: ${r.fail.length}`;
});
</script>
</body>
</html>

View File

@ -1,25 +0,0 @@
var path = require("path");
var fs = require("fs");
var test = require("../dist/testit.umd.js");
fs.readdir(__dirname, function(err, files) {
if(err) {
process.exit(1);
}
var tests = files.filter(function(item) {
return item.indexOf("spec.js") !== -1;
});
tests.forEach(function(file) {
console.log(`: ${file}`);
var me = fs.readFileSync(path.join(__dirname, file));
// eval can be evil, but it is YOUR code, are you evil?
eval(me.toString());
test.it(spec()).run();
});
});

15
test/runner.js Normal file
View File

@ -0,0 +1,15 @@
import spec from "./index.spec.js";
spec.run(false, function (r) {
let errors = [];
document.body.style.backgroundColor = (
r.fail.length ? "#ff9999" : "#99ff99"
);
r.pass.forEach((p) => errors.push(`<br>${p}`));
r.fail.forEach((f) => errors.push(`<br><b>${f}</b>`));
document.querySelector('#errors').innerHTML = errors;
document.querySelector('#summary').innerHTML = `| tests: ${r.pass.length + r.fail.length} | pass: ${r.pass.length} | fail: ${r.fail.length} |`;
});