diff --git a/README.md b/README.md index b13791a..4a4bfc8 100644 --- a/README.md +++ b/README.md @@ -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... @@ -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!** @@ -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(); ``` @@ -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) { @@ -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 -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 +- 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: - -| 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 +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); } }); @@ -131,8 +121,8 @@ test.it({ ## 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 diff --git a/src/testit.js b/src/testit.js index 27723ff..9aab1a9 100644 --- a/src/testit.js +++ b/src/testit.js @@ -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); + } } }; diff --git a/test/index.html b/test/index.html new file mode 100644 index 0000000..4ba8d20 --- /dev/null +++ b/test/index.html @@ -0,0 +1,17 @@ + + + + + + + Test It Spec + + + +
+
+ + + diff --git a/test/index.spec.js b/test/index.spec.js index e5bf54e..6bded03 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -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'); + } +}); diff --git a/test/run.html b/test/run.html deleted file mode 100644 index fd48165..0000000 --- a/test/run.html +++ /dev/null @@ -1,34 +0,0 @@ - - - - - - - Test It Spec - - - - -
-
- - - diff --git a/test/run.js b/test/run.js deleted file mode 100644 index 149fe57..0000000 --- a/test/run.js +++ /dev/null @@ -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(); - }); -}); diff --git a/test/runner.js b/test/runner.js new file mode 100644 index 0000000..a251b5d --- /dev/null +++ b/test/runner.js @@ -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(`
${p}`)); + r.fail.forEach((f) => errors.push(`
${f}`)); + + document.querySelector('#errors').innerHTML = errors; + document.querySelector('#summary').innerHTML = `| tests: ${r.pass.length + r.fail.length} | pass: ${r.pass.length} | fail: ${r.fail.length} |`; +});