UNPKG

@drozdik.m/unit-test

Version:

Unit test with test cases with Assert functions. Simple and easy.

191 lines (190 loc) 7.43 kB
exports.__esModule = true; var UnitTestFailedError_1 = require("./Errors/UnitTestFailedError"); var SyncTestCase_1 = require("./TestCases/SyncTestCase"); var AsyncTestCase_1 = require("./TestCases/AsyncTestCase"); var Assert_1 = require("./Assert"); exports.Assert = Assert_1.Assert; /** * Unit test */ var UnitTest = /** @class */ (function () { //-------------------------------------------------- //----------CONSTRUCTOR----------------------------- //-------------------------------------------------- /** * Create new unit test * @param testName Test name */ function UnitTest(testName) { //-------------------------------------------------- //----------ATTRIBUTES------------------------------ //-------------------------------------------------- this.testName = ""; this.testCases = []; //Async this.awaitingAsync = 0; this.asyncCallbackFunction = null; this.checkInterval = -1; this.testName = testName; this.AsyncTestCaseCallback = this.AsyncTestCaseCallback.bind(this); } //-------------------------------------------------- //----------TEST CASE------------------------------- //-------------------------------------------------- /** * Creates and saves new sync test case * @param name Test case name * @param testFunction Test case function */ UnitTest.prototype.AddTestCase = function (name, testFunction) { var newTestFunction = new SyncTestCase_1.SyncTestCase(name, testFunction); this.testCases.push(newTestFunction); }; /** * Creates and saves new sync test case * @param name Test case name * @param testFunction Test case function */ UnitTest.prototype.AddSyncTestCase = function (name, testFunction) { var newTestFunction = new SyncTestCase_1.SyncTestCase(name, testFunction); this.testCases.push(newTestFunction); }; /** * Creates and save new async test case * @param name Test case name * @param testFunction Test case function */ UnitTest.prototype.AddAsyncTestCase = function (name, testFunction) { var newTestFunction = new AsyncTestCase_1.AsyncTestCase(name, testFunction, this.AsyncTestCaseCallback); this.testCases.push(newTestFunction); this.awaitingAsync += 1; }; /** * Callback function for async test cases * */ UnitTest.prototype.AsyncTestCaseCallback = function () { this.awaitingAsync -= 1; if (this.awaitingAsync <= 0) if (this.asyncCallbackFunction != null) this.asyncCallbackFunction(this.ResultsJSON()); }; /** * Runs added test cases and logs result (sync and async) * @param timeout Time to timeout [ms] (default is 60 sec) */ UnitTest.prototype.Run = function (timeout) { if (timeout === void 0) { timeout = 60000; } var results = this.ResultsJSON(function (results) { UnitTest.ShowResults(results); this.ClearTimeout(); }); if (results.finished) UnitTest.ShowResults(results); else this.StartTimeout(timeout); }; /** * Start counting timeout * @param timeout */ UnitTest.prototype.StartTimeout = function (timeout) { if (this.checkInterval != -1) clearInterval(this.checkInterval); var intervalDelay = 100; var object = this; this.checkInterval = setInterval(function () { timeout -= intervalDelay; if (timeout <= 0) { clearInterval(object.checkInterval); throw new UnitTestFailedError_1.UnitTestFailedError("Timeout (did you forget to call Done() function?)"); } }, intervalDelay); }; /** * Clear timeout * */ UnitTest.prototype.ClearTimeout = function () { if (this.checkInterval != -1) clearInterval(this.checkInterval); }; /** * Logs passed results JSON * @param results IResultJSON */ UnitTest.ShowResults = function (results) { console.log("\nUnit test %s", results.name); console.group(); for (var i = 0; i < results.testCases.length; i++) { if (!results.testCases[i].finished) { console.log("%s - UNFINISHED", results.testCases[i].name); } //Test case success \u2713 else if (results.testCases[i].success) { console.log("%s - PASSED", results.testCases[i].name); } //Test case failed \u2717 else { console.log("%s - FAILED", results.testCases[i].name); console.log(" -> Error: " + results.testCases[i].errorMessage); } } console.groupEnd(); console.log("Unit test " + (!results.finished ? "UNFINISHED" : (results.success ? "PASSED" : "FAILED"))); //console.log(results); if (results.success != true) throw new UnitTestFailedError_1.UnitTestFailedError("Unit test " + results.name + " FAILED"); if (results.finished != true) throw new UnitTestFailedError_1.UnitTestFailedError("Unit test " + results.name + " UNFINISHED"); }; /** * Returns results in JSON format * @param callback If some async functions has been present, returns complete JSON here */ UnitTest.prototype.ResultsJSON = function (callback) { if (callback === void 0) { callback = null; } //Set variables var success = true; var finished = true; var resultJSON = {}; resultJSON.testCases = []; //Set name resultJSON.name = this.testName; //Save callback if (callback != null) this.asyncCallbackFunction = callback; //Get results for (var i = 0; i < this.testCases.length; i++) { var testCaseResultJSON = {}; testCaseResultJSON.name = this.testCases[i].Name(); //Run if (!this.testCases[i].IsDone() && !this.testCases[i].IsRunning()) this.testCases[i].Run(); //Success? if (this.testCases[i].Success()) { //Test case success testCaseResultJSON.success = true; testCaseResultJSON.errorMessage = ""; } else { success = false; testCaseResultJSON.success = false; testCaseResultJSON.errorMessage = (this.testCases[i].Error() == null ? "" : this.testCases[i].Error().message); } //Finished? testCaseResultJSON.finished = true; if (!this.testCases[i].IsDone()) { testCaseResultJSON.finished = false; finished = false; } //Add test case results resultJSON.testCases[i] = testCaseResultJSON; } //Final success and finished resultJSON.success = success; resultJSON.finished = finished; return resultJSON; }; return UnitTest; }()); exports.UnitTest = UnitTest;