changed how testit is self tested

This commit is contained in:
Geoff Doty 2020-09-08 17:37:24 -05:00
parent ec4e087e90
commit 85a491ce98
5 changed files with 65 additions and 48 deletions

View File

@ -7,9 +7,9 @@
"test": "test" "test": "test"
}, },
"scripts": { "scripts": {
"build": "node_modules/.bin/gulp", "build": "npx gulp default",
"lint": "node_modules/.bin/eslint src/testit.js", "lint": "npx eslint src/testit.js",
"test": "node test/run.js" "test": "npm run build && npm run lint && node test/run.js"
}, },
"devDependencies": { "devDependencies": {
"eslint": "^7.8.1", "eslint": "^7.8.1",

View File

@ -1,6 +1,6 @@
/*! Test.it v 0.9.0 | MIT | https://github.com/n2geoff/testit */ /*! Test.it v 0.9.0 | MIT | https://github.com/n2geoff/testit */
const test = { const test = {
"log": console.log, "log": console.log, // eslint-disable-line
"_tests": {}, "_tests": {},
"run": function run(errors, next) { "run": function run(errors, next) {
// TODO: rewrite to allow a show errors flag (optional) // TODO: rewrite to allow a show errors flag (optional)

View File

@ -1,36 +1,43 @@
var test = test || require("../src/testit"); function spec(test) {
// npm run test
test.it({ if(!test) {
"'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 { try {
test.expects().to.fail(); var test = require("../dist/testit.umd.js");
} catch(e) { } 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');
} }
});
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');
}
};
}

View File

@ -5,20 +5,29 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="x-ua-compatible" content="ie=edge"> <meta http-equiv="x-ua-compatible" content="ie=edge">
<title>Test It Spec</title> <title>Test It Spec</title>
<script src="../src/testit.js"></script> <script src="index.spec.js"></script>
<script src="./index.spec.js"></script> <style>
#summary {font-size: 2rem; text-transform: uppercase;}
</style>
</head> </head>
<body> <body>
<script> <div id="summary"></div>
test.run(false, function(r) { <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 = ( document.body.style.backgroundColor = (
r.fail.length ? "#ff9999" : "#99ff99" r.fail.length ? "#ff9999" : "#99ff99"
); );
r.pass.forEach((p) => document.write(`<br>${p}`)); r.pass.forEach((p) => errors.push(`<br>${p}`));
r.fail.forEach((f) => document.write(`<br><b>${f}</b>`)); r.fail.forEach((f) => errors.push(`<br><b>${f}</b>`));
document.write(`<p>tests ${r.pass.length + r.fail.length} pass ${r.pass.length} fail ${r.fail.length}`); 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> </script>
</body> </body>

View File

@ -1,5 +1,6 @@
var path = require("path"); var path = require("path");
var fs = require("fs"); var fs = require("fs");
var test = require("../dist/testit.umd.js");
fs.readdir(__dirname, function(err, files) { fs.readdir(__dirname, function(err, files) {
if(err) { if(err) {
@ -16,9 +17,9 @@ fs.readdir(__dirname, function(err, files) {
var me = fs.readFileSync(path.join(__dirname, file)); var me = fs.readFileSync(path.join(__dirname, file));
// eval maybe evil, but it is YOUR code, are you evil? // eval can be evil, but it is YOUR code, are you evil?
eval(me.toString()); // jshint ignore:line eval(me.toString());
test.run(); test.it(spec()).run();
}); });
}); });