apxtrib/unittest.js
2023-03-27 07:52:21 +02:00

106 lines
3.6 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const fs = require("fs-extra");
const glob = require("glob");
//const config = require( './tribes/townconf.js' );
const config = {
unittesting: ["middlewares", "models", "routes", "nationchains"],
};
global.__base = __dirname + "/";
const ut = {};
ut.help = `
\x1b[32m###################################\x1b[0m
\x1b[32m### Make it simple Unit Testing ###\x1b[0m
\x1b[32m###################################\x1b[0m
Any file into a folder **/unittest/*.js can be run as a unit test.
Each file have to be independant to test a full process in production as well than in dev.
After each test the data has to be clean up (don't forget that your running test is apply into production).
Please add explicit research track message to help understand your code and how to debug it.
yarn unittest [-Options] [filtername ]
Options available:
\x1b[1m-verbose :\x1b[0m add system error to debug.
\x1b[1m-publish :\x1b[0m if test valid then push to production server list for propagation.
\x1b[1m-help :\x1b[0m this help.
filername: any test file name usefull when you just want to test your code under dev
Each unit testing file is structured like
\x1b[100m\x1b[32m
const assert=require('assert');
const any components you require
...
any testing process from create to delete based on assert lib that allow to test if your test expected what it has to be
see https://nodejs.org/api/assert.html
...
const ut={name:"testing name", run: ()=>{the function to run}};
module.exports=ut;
\x1b[0m
To request any update or change in the code add to your git branch a new file into where you test your modifications /inittest/
We first run your test and check with git diff your code before merge your branch with your inittest
You'll be inform when a new release including your change into azpXtrib will be available.
\x1b[7mThanks for improving our democracy tool.\x1b[0m
`;
ut.run = (options) => {
if (options.filetotest.length == 0)
console.log(
"Check your files because no test to run (for more: yarn unittest -help)"
);
options.filetotest.forEach((f) => {
const unittest = require(f);
console.log("\x1b[34m", `Unittest ${f}`, "\x1b[0m");
try {
unittest.run(options);
console.log("✅", "\x1b[32m", unittest.name, "\x1b[0m");
} catch (e) {
console.log(
"❌",
"\x1b[41m",
unittest.name,
e.stack.split("\n")[0],
"\x1b[0m"
);
if (options.verbose) console.log(e.stack);
}
});
};
const options = { filetotest: [] };
process.argv.slice(2).forEach((arg) => {
switch (arg.substring(0, 2)) {
case "-v":
options.verbose = true;
break;
case "-p":
options.publish = true;
break;
case "-h":
options.active = true;
options.help = true;
break;
default:
options.active = true;
config.unittesting.forEach((codefolder) => {
glob
.sync(`${__base}${codefolder}/**/unittest/${arg}.js`)
.forEach((f) => {
if (!options.filetotest.includes(f)) options.filetotest.push(f);
});
});
break;
}
});
if (!options.active) {
config.unittesting.forEach((codefolder) => {
glob.sync(`${__base}${codefolder}/**/unittest/*.js`).forEach((f) => {
if (!options.filetotest.includes(f)) options.filetotest.push(f);
});
});
console.log(
"You can test only some unittest by passing yarn unittest name where name is a /unittest/name.js file that exist, type 'yarn unittest -help' for more"
);
console.log("Looking for unittest folder into ", config.unittesting);
}
options.help ? console.log(ut.help) : ut.run(options);