Skip to content

Commit

Permalink
replace expect with simpler assert
Browse files Browse the repository at this point in the history
  • Loading branch information
n2geoff committed Sep 20, 2020
1 parent 7e268c6 commit cacc7a5
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 162 deletions.
36 changes: 13 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

> 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...

Expand All @@ -11,13 +11,12 @@ This is probally not a *cure-all* testing solution, if you want something more r
### Features

- Designed for the Browser
- Works with NodeJS
- *Under* a 100 lines
- Single File
- No Dependicies
- 2kb footprint (*before gzip*)
- Extend with custom reporters
- Has an Expect-like style BDD assertions
- Uses Simple Assert

**No Bloat Here!**

Expand All @@ -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

```js
import test from 'testit';

test.it({
'my passing test': function() {
test.expects().to.pass();
test.assert(true);
},
'my failing test': function() {
test.expects().to.fail('just wanted to fail fast');
test.assert(true === false, 'just wanted to fail fast');
}
}).run();
```
Expand Down Expand Up @@ -68,7 +69,7 @@ For Example...
```js
test.it({
'my passing test': function() {
test.pass();
test.assert(true);
}
}, function(results) {
if (window.document && document.body) {
Expand All @@ -92,47 +93,36 @@ From this object you can easily find the number of tests ran `pass.length`, numb

> 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

To stay minimal, `test.it` only has 3 core functions:
- `it` to capture your tests
- `run` to execute yours tests
- and `expects` to write your assertions

While you can use your own assertion library, the included `expects` provides the following methods for writing your 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()`) |
- and `assert` to write your assertions

> NOTE: wish `eval` was not so evil, `assert(expression, message)` would be ideal
While you can use your own assertion library, the included `assert` evaluates an expression/condition tests:

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

```js
test.it({
"my test should work": function() {
expect().to.pass();
assert(true);
}
});

```

## TODO

- write `not` expects, ie `expects(this).to.not.equal(this)`
- provide sample test runner for CI environments
- maybe spec files export results && runner identifies failure

## Support

Expand Down
46 changes: 7 additions & 39 deletions src/testit.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,46 +48,14 @@ const test = {
this._tests = tests;
return this;
},
"expects": function expects(val) {
return {
"to": {
"be": {
"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}`);
}

return true;
}

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); }
"assert": (expression, msg) => {
try {
if(!expression) {
throw new Error(msg || "Assertion Failed");
}
};
} catch (e) {
throw new Error(msg);
}
}
};

Expand Down
17 changes: 17 additions & 0 deletions test/index.html
Original file line number Diff line number Diff line change
@@ -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>
74 changes: 33 additions & 41 deletions test/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,35 @@
function spec(test) {
// npm run test
if(!test) {
try {
var test = require("../dist/testit.umd.js");
} catch(e) {};
}
import test from "../src/testit.js";

return {
"'like' should do truthy evaluation via ==": function() {
test.expects(1).to.be.like('1');
test.expects("1").to.be.like(1);
test.expects(1).to.be.ok();
},
"'equal' should do === evaluation exist": function() {
test.expects(1).to.equal(1);
test.expects('hello').to.equal('hello');
},
"you should be able to 'pass' a test": function() {
test.expects().to.pass();
},
"you should be able to fail' a test too": function() {
try {
test.expects().to.fail();
} catch(e) {
test.expects().to.pass();
}
},
"you should be able to test if something 'exists'": function() {
test.expects({}).to.exist();
test.expects({}).to.be.ok();
test.expects({}).to.be.a('object');
},
"should be able to check types": function() {
test.expects(123).to.be.a('number');
test.expects([]).to.be.an('array');
test.expects({}).to.be.a('object');
test.expects(true).to.be.a('boolean');
test.expects(false).to.be.a('boolean');
test.expects(undefined).to.be.a('undefined');
export default test.it({
"'like' should do truthy evaluation via ==": function () {
test.assert(1 == '1');
test.assert(1);
},
"'equal' should do === evaluation exist": function () {
test.assert(1 === 1);
test.assert('hello' === 'hello');
},
"you should be able to 'pass' a test": function () {
test.assert(1);
},
"you should be able to fail' a test too": function () {
try {
test.assert(0);
} catch (e) {
}
};
}
},
"you should be able to test if something 'exists'": function () {
test.assert({});
test.assert(typeof ({}) === 'object');
},
"should be able to check types": function () {

test.assert(Array.isArray([]));
test.assert(typeof (123) === 'number');

test.assert(typeof ({}) === 'object');
test.assert(typeof (true) === 'boolean');
test.assert(typeof (false) === 'boolean');
test.assert(typeof (undefined) === 'undefined');
}
});
34 changes: 0 additions & 34 deletions test/run.html

This file was deleted.

25 changes: 0 additions & 25 deletions test/run.js

This file was deleted.

15 changes: 15 additions & 0 deletions test/runner.js
Original file line number Diff line number Diff line change
@@ -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} |`;
});

0 comments on commit cacc7a5

Please sign in to comment.