ckn.server
Version:
98 lines (94 loc) • 5.44 kB
JavaScript
import { ckn } from "ckn";
import { CKNController } from './ckn.server.controller.js';
class CKNTest {
#testResults = [];
constructor(server) {
this.server = server;
this.initial().then(() => {});
}
#run = async () => {
this.#testResults = [];
for (let app of this.server.apps) {
for (let controller of Object.values(app.controllers)) {
if (controller instanceof CKNController) {
for (let methodObject of Object.values(controller)) {
if (methodObject.process != null
&& methodObject.test != null) {
methodObject.runTest = (message = "", param) => {
ckn.log.info("Testing: " + message);
let session = {};
let testingResult = null;
session.send = (data) => {
testingResult = data;
};
session.request = {};
session.request.body = param.body == null ? {} : param.body;
session.request.query = param.query == null ? {} : param.query;
return {
result: (testingFunction) => {
methodObject.process(session).then(() => {
let expect = (checkObject) => {
return {
toBe: (value) => {
if (checkObject == value) {
this.#testResults.push({
status: true,
app: app.name,
controller: controller.name,
process: methodObject.name,
message: "[PASS] Value should be " + value,
expect: checkObject,
actual: value
});
console.log("[PASS] Value should be " + value);
}
else {
this.#testResults.push({
status: false,
app: app.name,
controller: controller.name,
process: methodObject.name,
message: `[FAIL] Value should be '${value}' but actual value is '${checkObject}'`,
expect: checkObject,
actual: value
});
console.log("[FAIL] Value should be " + value);
}
}
};
}
testingFunction(testingResult, expect);
});
}
}
};
await methodObject.test(methodObject);
}
}
}
}
}
}
initial = async () => {
this.server.get("/ckn.server/test", async session => {
await this.#run();
session.send(this.#testResults);
});
this.server.get("/ckn.server/test/report", async session => {
await this.#run();
let html = `<h2>Unit Test Report</h2>`;
html += "<div style='background-color: #ABCDEF; padding: 8px'>";
html += "<h3>Summary</h3>";
html += `<div>Pass: ${this.#testResults.where(s => s.status).length}</div>`;
html += `<div>Fail: ${this.#testResults.where(s => !s.status).length}</div>`;
html += "</div>";
for (let result of this.#testResults) {
let statusStyle = result.status ? "color: white;background-color: green" : "color: white;background-color: red";
html += `<div style='${statusStyle}; padding: 8px; margin-top:10px'>${result.message}</div>`;
}
session.response.set('Content-Type', 'text/html');
session.response.send(Buffer.from("<html><body>"+html+"</body></html>"));
});
}
}
export { CKNTest }