Compare commits

..

No commits in common. "master" and "v0.8.0" have entirely different histories.

17 changed files with 4985 additions and 1088 deletions

3
.jshintrc Normal file
View File

@ -0,0 +1,3 @@
{
"esversion": 6
}

View File

@ -33,7 +33,7 @@ The `dist` includes the minified version of the source code.
Run unit tests using this command: Run unit tests using this command:
```bash ```bash
npm run test npm test
``` ```
## Reporting a bug ## Reporting a bug

View File

@ -1,6 +1,6 @@
The MIT License The MIT License
Copyright (c) 2023 Geoff Doty Copyright (c) 2018 testit authors
Permission is hereby granted, free of charge, to any person obtaining a copy of Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in this software and associated documentation files (the "Software"), to deal in

View File

@ -1,8 +1,8 @@
# Test.it # Test.it
> A minimalistic client-side testing library > A minimalistic testing library
**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) **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)
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...
@ -10,13 +10,14 @@ This is probally not a *cure-all* testing solution, if you want something more r
### Features ### Features
- Designed for the Browser - Works in the Browser
- *Under* a 100 lines - Works with CommonJS (aka NodeJS)
- *Barely* over 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
- Uses Simple Assert - Has an Expect-like style BDD assertions
**No Bloat Here!** **No Bloat Here!**
@ -28,14 +29,12 @@ 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'() { 'my passing test': function() {
test.assert(true); test.expects().to.pass();
}, },
'my failing test'() { 'my failing test': function() {
test.assert(true === false, 'just wanted to fail fast'); test.expects().to.fail('just wanted to fail fast');
} }
}).run(); }).run();
``` ```
@ -56,7 +55,7 @@ Error: just wanted to fail fast
A `+OK` will proceed test lines that *pass* and a `-ERR` for those that *fail*, An error stack is included by default after the failing test wrapped in `---`. You can suppress outputing the error stack by passing `false` as an argument to `run()`, ie `run(false)`. A `+OK` will proceed test lines that *pass* and a `-ERR` for those that *fail*, An error stack is included by default after the failing test wrapped in `---`. You can suppress outputing the error stack by passing `false` as an argument to `run()`, ie `run(false)`.
You can, also, write your own custom test runner... You can, however, write your own custom test runner...
### Custom Test Runners ### Custom Test Runners
@ -68,10 +67,10 @@ For Example...
```js ```js
test.it({ test.it({
'my passing test'() { 'my passing test': function() {
test.assert(true); test.pass();
} }
}, (results) => { }, function(results) {
if (window.document && document.body) { if (window.document && document.body) {
document.body.style.backgroundColor = ( document.body.style.backgroundColor = (
results.fail.length ? '#ff9999' : '#99ff99' results.fail.length ? '#ff9999' : '#99ff99'
@ -93,27 +92,38 @@ 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 the **BROWSER** in the `test/` directory; `index.html` and `runner.js` respectfully, with the spec in `index.spec.js`. A sample test runner is provided for both **HTML** and **NODE** in the `test/` directory; `run.html` and `run.js` respectfully.
## 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 `assert` to write your assertions - and `expects` to write your assertions
While you can use your own assertion library, the included `assert` evaluates an expression/condition tests: 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 if value exists |
| `.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 assert = test.assert; let expect = test.expects;
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"() { "my test should work": function() {
assert(true); expect().to.pass();
} }
}); });
@ -121,8 +131,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
@ -134,4 +144,4 @@ Anyone is welcome to contribute, however, if you decide to get involved, please
## License ## License
[MIT](LICENSE) Geoff Doty [MIT](LICENSE)

103
dist/testit.js vendored Normal file
View File

@ -0,0 +1,103 @@
/*! Test.it v 0.8.0 | MIT | https://github.com/n2geoff/testit */
(function (root, factory) {
"use strict";
if (typeof module === "object" && module.exports) {
module.exports = factory(root.test);
} else {
root.test = factory(root.test);
}
}(this, function () {
"use strict";
const test = {
"_tests": {},
"run": function run(errors, next) {
if(typeof errors !== "boolean") {
next = errors;
errors = true;
}
let tests = this._tests;
let failed = [];
let passed = [];
Object.keys(tests).forEach((name) => {
let test = tests[name];
try {
test();
passed.push(`\n+OK ${name}`);
} catch (err) {
if (errors) {
console.log('ERRORS: YES');
failed.push(`\n-ERR ${name} \n --- \n ${err.stack} \n ---`);
} else {
console.log('ERRORS: NO');
failed.push(`\n-ERR ${name}`);
}
}
});
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;
}
},
"it": function it(tests) {
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}`);
}
},
"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); }
}
};
}
};
return test;
}));

11
dist/testit.min.js vendored
View File

@ -1,9 +1,2 @@
/*! Test.it v1.2.0 | MIT | https://github.com/n2geoff/testit */const o={log:console.log,version:"v1.2.0",_tests:{},run(t,s){typeof t!="boolean"&&(s=t,t=!0);let r=this._tests||[],e=[],l=[];return Object.keys(r).forEach(n=>{let h=r[n];try{h(),l.push(` /*! Test.it v 0.8.0 | MIT | https://github.com/n2geoff/testit */
+OK ${n}`)}catch(i){t?e.push(` !function(t,e){"use strict";"object"==typeof module&&module.exports?module.exports=e(t.test):t.test=e(t.test)}(this,function(){"use strict";const t={_tests:{},run:function(t,e){"boolean"!=typeof t&&(e=t,t=!0);let o=this._tests,n=[],r=[];return Object.keys(o).forEach(e=>{let s=o[e];try{s(),r.push(`\n+OK ${e}`)}catch(o){t?(console.log("ERRORS: YES"),n.push(`\n-ERR ${e} \n --- \n ${o.stack} \n ---`)):(console.log("ERRORS: NO"),n.push(`\n-ERR ${e}`))}}),"function"==typeof e?e({pass:r,fail:n}):(console.log(...r,...n),console.log(`\n# tests ${n.length+r.length} pass ${r.length} fail ${n.length}`),!n.length)},it:function(t){return this._tests=t,this},expects:function(e){return{to:{be:{a:o=>t.expects(e).to.be.an(o),an:t=>{if(-1!==["array"].indexOf(t)){if("array"!==e.constructor.name.toLowerCase())throw new Error(`expected ${typeof e} to be an ${t}`);return!0}if(typeof e!==t)throw new Error(`expected ${typeof e} to be an ${t}`)},like:t=>{if(e!=t)throw new Error(`expected ${e} == ${t}`)}},equal:t=>{if(e!==t)throw new Error(`expected ${e} === ${t}`)},exist:()=>{if(!e)throw new Error(`expected ${e} to be truthy`)},pass:()=>!0,fail:t=>{throw new Error(t)}}}}};return t});
-ERR ${n}
---
${i.stack}
---`):e.push(`
-ERR ${n}`)}}),typeof s=="function"?s({pass:l,fail:e}):(o.log(...l,...e),o.log(`
# tests ${e.length+l.length} pass ${l.length} fail ${e.length}`),!e.length)},it(t){return this._tests=t,this},assert(t,s){try{if(!t)throw new Error(s||"Assertion Failed")}catch{throw new Error(s)}}};var f=o;export{f as default,o as test};
//# sourceMappingURL=testit.min.js.map

View File

@ -1,7 +0,0 @@
{
"version": 3,
"sources": ["../src/testit.js"],
"sourcesContent": ["/*! Test.it v1.2.0 | MIT | https://github.com/n2geoff/testit */\nexport const test = {\n log: console.log,\n version: \"v1.2.0\",\n _tests: {},\n run(errors, next) {\n // TODO: rewrite to allow show errors flag (optional)\n if(typeof errors !== \"boolean\") {\n next = errors;\n errors = true;\n }\n\n let tests = this._tests || [];\n // capture results\n let failed = [];\n let passed = [];\n\n // loop through tests\n Object.keys(tests).forEach((name) => {\n let test = tests[name];\n\n // execute\n try {\n test();\n passed.push(`\\n+OK ${name}`);\n } catch (err) {\n if (errors) {\n failed.push(`\\n-ERR ${name} \\n --- \\n ${err.stack} \\n ---`);\n } else {\n failed.push(`\\n-ERR ${name}`);\n }\n }\n });\n\n // summary\n if(typeof next === \"function\") {\n return next({\n pass: passed,\n fail: failed\n });\n } else {\n test.log(...passed, ...failed);\n test.log(`\\n# tests ${failed.length + passed.length} pass ${passed.length} fail ${failed.length}`);\n\n return failed.length ? false : true;\n }\n },\n it(tests) {\n this._tests = tests;\n return this;\n },\n assert(expression, msg) {\n try {\n if(!expression) {\n throw new Error(msg || \"Assertion Failed\");\n }\n } catch {\n throw new Error(msg);\n }\n }\n};\n\nexport default test;\n"],
"mappings": "AAAA,+DACO,MAAMA,EAAO,CAChB,IAAK,QAAQ,IACb,QAAS,SACT,OAAQ,CAAC,EACT,IAAIC,EAAQC,EAAM,CAEX,OAAOD,GAAW,YACjBC,EAAOD,EACPA,EAAS,IAGb,IAAIE,EAAQ,KAAK,QAAU,CAAC,EAExBC,EAAS,CAAC,EACVC,EAAS,CAAC,EAoBd,OAjBA,OAAO,KAAKF,CAAK,EAAE,QAASG,GAAS,CACjC,IAAIN,EAAOG,EAAMG,CAAI,EAGrB,GAAI,CACAN,EAAK,EACLK,EAAO,KAAK;AAAA,MAASC,CAAI,EAAE,CAC/B,OAASC,EAAK,CACNN,EACAG,EAAO,KAAK;AAAA,OAAUE,CAAI;AAAA;AAAA,GAAcC,EAAI,KAAK;AAAA,KAAS,EAE1DH,EAAO,KAAK;AAAA,OAAUE,CAAI,EAAE,CAEpC,CACJ,CAAC,EAGE,OAAOJ,GAAS,WACRA,EAAK,CACR,KAAMG,EACN,KAAMD,CACV,CAAC,GAEDJ,EAAK,IAAI,GAAGK,EAAQ,GAAGD,CAAM,EAC7BJ,EAAK,IAAI;AAAA,UAAaI,EAAO,OAASC,EAAO,MAAM,SAASA,EAAO,MAAM,SAASD,EAAO,MAAM,EAAE,EAE1F,CAAAA,EAAO,OAEtB,EACA,GAAGD,EAAO,CACN,YAAK,OAASA,EACP,IACX,EACA,OAAOK,EAAYC,EAAK,CACpB,GAAI,CACA,GAAG,CAACD,EACA,MAAM,IAAI,MAAMC,GAAO,kBAAkB,CAEjD,MAAQ,CACJ,MAAM,IAAI,MAAMA,CAAG,CACvB,CACJ,CACJ,EAEA,IAAOC,EAAQV",
"names": ["test", "errors", "next", "tests", "failed", "passed", "name", "err", "expression", "msg", "testit_default"]
}

View File

@ -1,26 +0,0 @@
import globals from "globals";
import path from "node:path";
import { fileURLToPath } from "node:url";
import js from "@eslint/js";
import { FlatCompat } from "@eslint/eslintrc";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const compat = new FlatCompat({
baseDirectory: __dirname,
recommendedConfig: js.configs.recommended,
allConfig: js.configs.all
});
export default [...compat.extends("eslint:recommended"), {
languageOptions: {
globals: {
...globals.browser,
},
ecmaVersion: "latest",
sourceType: "module",
},
rules: {},
}];

10
gulpfile.js Normal file
View File

@ -0,0 +1,10 @@
const gulp = require("gulp");
const minify = require("gulp-minify");
const strip = require("gulp-strip-comments");
gulp.task("default", function build() {
return gulp.src("./src/testit.js")
.pipe(strip({safe: true}))
.pipe(minify({ext: {min: ".min.js"}}))
.pipe(gulp.dest("dist"))
});

5530
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,20 +1,21 @@
{ {
"name": "testit.run", "name": "testit",
"version": "1.2.0", "version": "0.8.0",
"description": "minimalistic client-side testing library", "description": "a minimalistic testing library",
"main": "src/testit.js", "main": "src/testit.js",
"type": "module",
"directories": { "directories": {
"test": "test" "test": "test"
}, },
"scripts": {
"build": "npx esbuild src/testit.js --minify --sourcemap --format=esm --outfile=dist/testit.min.js",
"lint": "npx eslint src/testit.js",
"test": "npx live-server --open=test/ --port=5000"
},
"dependencies": {},
"devDependencies": { "devDependencies": {
"eslint": "^9.23.0" "gulp": "^4.0.0",
"gulp-minify": "^3.1.0",
"gulp-strip-comments": "^2.5.2",
"jshint": "^2.9.6"
},
"scripts": {
"build": "node_modules/.bin/gulp",
"lint": "node_modules/.bin/jshint src/testit.js",
"test": "node test/run.js"
}, },
"repository": { "repository": {
"type": "git", "type": "git",

View File

@ -1,63 +1,109 @@
/*! Test.it v1.2.0 | MIT | https://github.com/n2geoff/testit */ /*! Test.it v 0.8.0 | MIT | https://github.com/n2geoff/testit */
export const test = { (function (root, factory) {
log: console.log, "use strict";
version: "v1.2.0", // support browser & commonjs
_tests: {}, if (typeof module === "object" && module.exports) {
run(errors, next) { module.exports = factory(root.test);
// TODO: rewrite to allow show errors flag (optional) } else {
if(typeof errors !== "boolean") { root.test = factory(root.test);
next = errors;
errors = true;
}
let tests = this._tests || [];
// capture results
let failed = [];
let passed = [];
// loop through tests
Object.keys(tests).forEach((name) => {
let test = tests[name];
// execute
try {
test();
passed.push(`\n+OK ${name}`);
} catch (err) {
if (errors) {
failed.push(`\n-ERR ${name} \n --- \n ${err.stack} \n ---`);
} else {
failed.push(`\n-ERR ${name}`);
}
}
});
// summary
if(typeof next === "function") {
return next({
pass: passed,
fail: failed
});
} else {
test.log(...passed, ...failed);
test.log(`\n# tests ${failed.length + passed.length} pass ${passed.length} fail ${failed.length}`);
return failed.length ? false : true;
}
},
it(tests) {
this._tests = tests;
return this;
},
assert(expression, msg) {
try {
if(!expression) {
throw new Error(msg || "Assertion Failed");
}
} catch {
throw new Error(msg);
}
} }
}; }(this, function () {
"use strict";
export default test; const test = {
"_tests": {},
"run": function run(errors, next) {
// rewrite to allow a show errors flag (optional)
if(typeof errors !== "boolean") {
next = errors;
errors = true;
}
let tests = this._tests;
// capture results
let failed = [];
let passed = [];
// loop through tests
Object.keys(tests).forEach((name) => {
let test = tests[name];
// execute
try {
test();
passed.push(`\n+OK ${name}`);
} catch (err) {
if (errors) {
console.log('ERRORS: YES');
failed.push(`\n-ERR ${name} \n --- \n ${err.stack} \n ---`);
} else {
console.log('ERRORS: NO');
failed.push(`\n-ERR ${name}`);
}
}
});
// 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;
}
},
"it": function it(tests) {
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}`);
}
},
"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); }
}
};
}
};
return test;
}));

View File

@ -1,17 +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>
<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,36 +1,33 @@
import test from "../src/testit.js"; var test = test || require("../src/testit");
export default test.it({ test.it({
"'like' should do truthy evaluation via =="() { "'like' should do truthy evaluation via ==": function() {
test.assert(1 == "1"); test.expects(1).to.be.like('1');
test.assert(1); test.expects("1").to.be.like(1);
}, },
"'equal' should do === evaluation exist"() { "'equal' should do === evaluation exist": function() {
test.assert(1 === 1); test.expects(1).to.equal(1);
test.assert("hello" === "hello"); test.expects('hello').to.equal('hello');
}, },
"you should be able to 'pass' a test"() { "you should be able to 'pass' a test": function() {
test.assert(1); test.expects().to.pass();
}, },
"you should be able to fail' a test too"() { "you should be able to fail' a test too": function() {
try { try {
test.assert(0); test.expects().to.fail();
} catch (e) { } catch(e) {
// correct test.expects().to.pass();
} }
}, },
"you should be able to test if something 'exists'"() { "you should be able to see if something 'exists'": function() {
test.assert({}); test.expects({}).to.exist();
test.assert(typeof ({}) === "object");
}, },
"should be able to check types"() { "should be able to check types": function() {
test.expects(123).to.be.a('number');
test.assert(Array.isArray([])); test.expects([]).to.be.an('array');
test.assert(typeof (123) === "number"); test.expects({}).to.be.a('object');
test.expects(true).to.be.a('boolean');
test.assert(typeof ({}) === "object"); test.expects(false).to.be.a('boolean');
test.assert(typeof (true) === "boolean"); test.expects(undefined).to.be.a('undefined');
test.assert(typeof (false) === "boolean");
test.assert(typeof (undefined) === "undefined");
} }
}); });

25
test/run.html Normal file
View File

@ -0,0 +1,25 @@
<!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/testit.js"></script>
<script src="./index.spec.js"></script>
</head>
<body>
<script>
test.run(false, function(r) {
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>`));
document.write(`<p>tests ${r.pass.length + r.fail.length} pass ${r.pass.length} fail ${r.fail.length}`);
});
</script>
</body>
</html>

24
test/run.js Normal file
View File

@ -0,0 +1,24 @@
var path = require("path");
var fs = require("fs");
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 maybe evil, but its your code, are you evil?
eval(me.toString());
test.run();
});
});

View File

@ -1,15 +0,0 @@
import spec from "./index.spec.js";
spec.run(false, (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} |`;
});