mirror of https://github.com/n2geoff/testit.git
Compare commits
No commits in common. "master" and "v0.7.0" have entirely different histories.
|
@ -1,6 +1,6 @@
|
|||
# Contributing
|
||||
|
||||
So you want to contribute, awesome. **Thank you**.
|
||||
So you want to contribute, nice. **Thank you**.
|
||||
|
||||
Bug reports and code and documentation patches are all welcome. You can help this project also by using the development version and by reporting any bugs you might encounter.
|
||||
|
||||
|
@ -33,12 +33,12 @@ The `dist` includes the minified version of the source code.
|
|||
Run unit tests using this command:
|
||||
|
||||
```bash
|
||||
npm run test
|
||||
npm test
|
||||
```
|
||||
|
||||
## Reporting a bug
|
||||
|
||||
Use the [GitHub issue tracker](https://github.com/n2geoff/testit/issues/new) to report any bug you find.
|
||||
Use the [GitHub issue tracker](https://github.com/n2geoff/js-lib/issues) to report any bug you find.
|
||||
Bugs description should include:
|
||||
|
||||
* How to reproduce the bug;
|
||||
|
@ -48,9 +48,11 @@ Would be nice to have some code showing how to reproduce the code, you may use [
|
|||
|
||||
## Request a feature
|
||||
|
||||
Use the [GitHub issue tracker](https://github.com/n2geoff/testit/issues/new) to request a new feature.
|
||||
Use the [GitHub issue tracker](https://github.com/n2geoff/js-lib/issues) to request a new feature.
|
||||
|
||||
Keep in mind, this is a pure javascript library, no dependencies as minimal as possible
|
||||
Keep in mind, this is a pure javascript library
|
||||
|
||||
Feel free to port it to your favorite framework, such as [RiotJS](http://riotjs.com), Angular or VueJs in a new repository.
|
||||
|
||||
## Commit message
|
||||
|
||||
|
|
2
LICENSE
2
LICENSE
|
@ -1,6 +1,6 @@
|
|||
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
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
|
|
78
README.md
78
README.md
|
@ -1,41 +1,39 @@
|
|||
# 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](), [Tape]() or [Mocha]() -- this is to...
|
||||
|
||||
**Test small things, with small things**
|
||||
|
||||
### Features
|
||||
|
||||
- Designed for the Browser
|
||||
- *Under* a 100 lines
|
||||
- Works in the Browser
|
||||
- Works with CommonJS (aka NodeJS)
|
||||
- Barely over a 100 lines
|
||||
- Single File
|
||||
- No Dependicies
|
||||
- 2kb footprint (*before gzip*)
|
||||
- Extend with custom reporters
|
||||
- Uses Simple Assert
|
||||
- Has an Expect-like style BDD assertions
|
||||
|
||||
**No Bloat Here!**
|
||||
|
||||
- [Download Here](https://raw.githubusercontent.com/n2geoff/testit/master/src/testit.js)
|
||||
- [Or Minified Version Here](https://raw.githubusercontent.com/n2geoff/testit/master/src/testit.min.js)
|
||||
- [Download Now Available](https://raw.githubusercontent.com/n2geoff/testit/master/src/testit.min.js)
|
||||
|
||||
## Usage
|
||||
|
||||
By default, you can run your tests like
|
||||
|
||||
```js
|
||||
import test from 'testit';
|
||||
|
||||
test.it({
|
||||
'my passing test'() {
|
||||
test.assert(true);
|
||||
'my passing test': function() {
|
||||
test.expects().to.pass();
|
||||
},
|
||||
'my failing test'() {
|
||||
test.assert(true === false, 'just wanted to fail fast');
|
||||
'my failing test': function() {
|
||||
test.expects().to.fail('just wanted to fail fast');
|
||||
}
|
||||
}).run();
|
||||
```
|
||||
|
@ -44,19 +42,18 @@ test.it({
|
|||
by default, your test results are logged to the console
|
||||
|
||||
```
|
||||
+OK my passing test
|
||||
-ERR my failing test
|
||||
---
|
||||
Error: just wanted to fail fast
|
||||
+ my passing test
|
||||
- my failing test
|
||||
- - Error: just wanted to fail fast
|
||||
...error stack...
|
||||
---
|
||||
|
||||
# tests 2 pass 1 fail 1
|
||||
```
|
||||
|
||||
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 `+` 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 `- -`
|
||||
|
||||
You can, also, write your own custom test runner...
|
||||
You can, however, write your own custom test runner...
|
||||
|
||||
> NOTE: API still in flux, and may change to closer match TAP
|
||||
|
||||
### Custom Test Runners
|
||||
|
||||
|
@ -68,10 +65,10 @@ For Example...
|
|||
|
||||
```js
|
||||
test.it({
|
||||
'my passing test'() {
|
||||
test.assert(true);
|
||||
'my passing test': function() {
|
||||
test.pass();
|
||||
}
|
||||
}, (results) => {
|
||||
}, function(results) {
|
||||
if (window.document && document.body) {
|
||||
document.body.style.backgroundColor = (
|
||||
results.fail.length ? '#ff9999' : '#99ff99'
|
||||
|
@ -91,29 +88,38 @@ If using the optional `next` param will return results as JSON
|
|||
|
||||
From this object 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.
|
||||
|
||||
> 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
|
||||
|
||||
To stay minimal, `test.it` only has 3 core functions:
|
||||
- `it` to capture your tests
|
||||
- `run` to execute yours tests
|
||||
- and `assert` to write your assertions
|
||||
- and `expects` to write your test 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
|
||||
|
||||
let assert = test.assert;
|
||||
let expect = test.expects;
|
||||
|
||||
putting that above your tests will allow you to write like
|
||||
|
||||
```js
|
||||
test.it({
|
||||
"my test should work"() {
|
||||
assert(true);
|
||||
"my test should work": function() {
|
||||
expect().to.pass();
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -121,8 +127,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
|
||||
|
||||
|
@ -130,8 +136,8 @@ Please open [an issue](https://github.com/n2geoff/testit/issues/new) for support
|
|||
|
||||
## Contributing
|
||||
|
||||
Anyone is welcome to contribute, however, if you decide to get involved, please take a moment to review the [guidelines](CONTRIBUTING.md), they're minimalistic;)
|
||||
Anyone is welcome to contribute, however, if you decide to get involved, please take a moment to review the [guidelines](CONTRIBUTING.md), there minimalistic;)
|
||||
|
||||
## License
|
||||
|
||||
[MIT](LICENSE) Geoff Doty
|
||||
[MIT](LICENSE)
|
||||
|
|
|
@ -0,0 +1,94 @@
|
|||
/*! Test.it v 0.7.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 (next) {
|
||||
|
||||
let tests = this._tests;
|
||||
let failed = [];
|
||||
let passed = [];
|
||||
|
||||
Object.keys(tests).forEach(function (name) {
|
||||
let test = tests[name];
|
||||
|
||||
try {
|
||||
test();
|
||||
passed.push(`\n+ ${name}`);
|
||||
} catch (err) {
|
||||
failed.push(`\n- ${name}`);
|
||||
console.error(err);
|
||||
}
|
||||
});
|
||||
|
||||
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 (tests) {
|
||||
this._tests = tests;
|
||||
return this;
|
||||
},
|
||||
"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;
|
||||
}));
|
|
@ -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(`
|
||||
+OK ${n}`)}catch(i){t?e.push(`
|
||||
-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
|
||||
/*! Test.it v 0.7.0 | MIT | https://github.com/n2geoff/testit */
|
||||
!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){let e=this._tests,r=[],o=[];return Object.keys(e).forEach(function(t){let n=e[t];try{n(),o.push(`\n+ ${t}`)}catch(e){r.push(`\n- ${t}`),console.error(e)}}),"function"==typeof t?t({pass:o,fail:r}):(console.log(...o,...r),console.log(`\n# tests ${r.length+o.length} pass ${o.length} fail ${r.length}`),!r.length)},it:function(t){return this._tests=t,this},expects:e=>({to:{be:{a:r=>t.expects(e).to.be.an(r),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});
|
|
@ -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"]
|
||||
}
|
|
@ -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: {},
|
||||
}];
|
|
@ -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"))
|
||||
});
|
File diff suppressed because it is too large
Load Diff
23
package.json
23
package.json
|
@ -1,20 +1,21 @@
|
|||
{
|
||||
"name": "testit.run",
|
||||
"version": "1.2.0",
|
||||
"description": "minimalistic client-side testing library",
|
||||
"name": "testit",
|
||||
"version": "0.7.0",
|
||||
"description": "a minimalistic testing library",
|
||||
"main": "src/testit.js",
|
||||
"type": "module",
|
||||
"directories": {
|
||||
"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": {
|
||||
"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": {
|
||||
"type": "git",
|
||||
|
|
100
src/testit.js
100
src/testit.js
|
@ -1,34 +1,35 @@
|
|||
/*! Test.it v1.2.0 | MIT | https://github.com/n2geoff/testit */
|
||||
export const test = {
|
||||
log: console.log,
|
||||
version: "v1.2.0",
|
||||
_tests: {},
|
||||
run(errors, next) {
|
||||
// TODO: rewrite to allow show errors flag (optional)
|
||||
if(typeof errors !== "boolean") {
|
||||
next = errors;
|
||||
errors = true;
|
||||
/*! Test.it v 0.6.2 | MIT | https://github.com/n2geoff/testit */
|
||||
(function (root, factory) {
|
||||
"use strict";
|
||||
// support browser & commonjs
|
||||
if (typeof module === "object" && module.exports) {
|
||||
module.exports = factory(root.test);
|
||||
} else {
|
||||
root.test = factory(root.test);
|
||||
}
|
||||
}(this, function () {
|
||||
"use strict";
|
||||
|
||||
let tests = this._tests || [];
|
||||
const test = {
|
||||
"_tests": {},
|
||||
"run": function (next) {
|
||||
|
||||
let tests = this._tests;
|
||||
// capture results
|
||||
let failed = [];
|
||||
let passed = [];
|
||||
|
||||
// loop through tests
|
||||
Object.keys(tests).forEach((name) => {
|
||||
Object.keys(tests).forEach(function (name) {
|
||||
let test = tests[name];
|
||||
|
||||
// execute
|
||||
try {
|
||||
test();
|
||||
passed.push(`\n+OK ${name}`);
|
||||
passed.push(`\n+ ${name}`);
|
||||
} catch (err) {
|
||||
if (errors) {
|
||||
failed.push(`\n-ERR ${name} \n --- \n ${err.stack} \n ---`);
|
||||
} else {
|
||||
failed.push(`\n-ERR ${name}`);
|
||||
}
|
||||
failed.push(`\n- ${name}`);
|
||||
console.error(err);
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -39,25 +40,60 @@ export const test = {
|
|||
fail: failed
|
||||
});
|
||||
} else {
|
||||
test.log(...passed, ...failed);
|
||||
test.log(`\n# tests ${failed.length + passed.length} pass ${passed.length} fail ${failed.length}`);
|
||||
console.log(...passed, ...failed);
|
||||
console.log(`\n# tests ${failed.length + passed.length} pass ${passed.length} fail ${failed.length}`);
|
||||
|
||||
return failed.length ? false : true;
|
||||
}
|
||||
},
|
||||
it(tests) {
|
||||
"it": function (tests) {
|
||||
this._tests = tests;
|
||||
return this;
|
||||
},
|
||||
assert(expression, msg) {
|
||||
try {
|
||||
if(!expression) {
|
||||
throw new Error(msg || "Assertion Failed");
|
||||
}
|
||||
} catch {
|
||||
throw new Error(msg);
|
||||
}
|
||||
}
|
||||
};
|
||||
"expects": (val) => {
|
||||
return {
|
||||
"to": {
|
||||
"be": {
|
||||
"a": (type) => {
|
||||
return test.expects(val).to.be.an(type);
|
||||
},
|
||||
"an": (type) => {
|
||||
|
||||
export default test;
|
||||
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;
|
||||
}));
|
||||
|
|
|
@ -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>
|
|
@ -1,36 +1,33 @@
|
|||
import test from "../src/testit.js";
|
||||
var test = test || require("../src/testit");
|
||||
|
||||
export default test.it({
|
||||
"'like' should do truthy evaluation via =="() {
|
||||
test.assert(1 == "1");
|
||||
test.assert(1);
|
||||
test.it({
|
||||
"'like' should do truthy evaluation via ==": function() {
|
||||
test.expects(1).to.be.like('1');
|
||||
test.expects("1").to.be.like(1);
|
||||
},
|
||||
"'equal' should do === evaluation exist"() {
|
||||
test.assert(1 === 1);
|
||||
test.assert("hello" === "hello");
|
||||
"'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"() {
|
||||
test.assert(1);
|
||||
"you should be able to 'pass' a test": function() {
|
||||
test.expects().to.pass();
|
||||
},
|
||||
"you should be able to fail' a test too"() {
|
||||
"you should be able to fail' a test too": function() {
|
||||
try {
|
||||
test.assert(0);
|
||||
} catch (e) {
|
||||
// correct
|
||||
test.expects().to.fail();
|
||||
} catch(e) {
|
||||
test.expects().to.pass();
|
||||
}
|
||||
},
|
||||
"you should be able to test if something 'exists'"() {
|
||||
test.assert({});
|
||||
test.assert(typeof ({}) === "object");
|
||||
"you should be able to see if something 'exists'": function() {
|
||||
test.expects({}).to.exist();
|
||||
},
|
||||
"should be able to check types"() {
|
||||
|
||||
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");
|
||||
"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');
|
||||
}
|
||||
});
|
|
@ -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(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>
|
|
@ -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();
|
||||
});
|
||||
});
|
|
@ -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} |`;
|
||||
});
|
Loading…
Reference in New Issue