|
@@ -10,6 +10,8 @@
|
|
* @license Apache-2.0
|
|
* @license Apache-2.0
|
|
*/
|
|
*/
|
|
import Chef from "../../src/core/Chef.mjs";
|
|
import Chef from "../../src/core/Chef.mjs";
|
|
|
|
+import Utils from "../../src/core/Utils.mjs";
|
|
|
|
+import cliProgress from "cli-progress";
|
|
|
|
|
|
/**
|
|
/**
|
|
* Object to store and run the list of tests.
|
|
* Object to store and run the list of tests.
|
|
@@ -47,68 +49,99 @@ class TestRegister {
|
|
/**
|
|
/**
|
|
* Runs all the tests in the register.
|
|
* Runs all the tests in the register.
|
|
*/
|
|
*/
|
|
- runTests () {
|
|
|
|
- console.log("Running tests...");
|
|
|
|
- return Promise.all(
|
|
|
|
- this.tests.map(function(test, i) {
|
|
|
|
- const chef = new Chef();
|
|
|
|
-
|
|
|
|
- return chef.bake(
|
|
|
|
- test.input,
|
|
|
|
- test.recipeConfig,
|
|
|
|
- {},
|
|
|
|
- 0,
|
|
|
|
- false
|
|
|
|
- ).then(function(result) {
|
|
|
|
- const ret = {
|
|
|
|
- test: test,
|
|
|
|
- status: null,
|
|
|
|
- output: null,
|
|
|
|
- };
|
|
|
|
-
|
|
|
|
- if (result.error) {
|
|
|
|
- if (test.expectedError) {
|
|
|
|
- ret.status = "passing";
|
|
|
|
- } else {
|
|
|
|
- ret.status = "erroring";
|
|
|
|
- ret.output = result.error.displayStr;
|
|
|
|
- }
|
|
|
|
- } else {
|
|
|
|
- if (test.expectedError) {
|
|
|
|
- ret.status = "failing";
|
|
|
|
- ret.output = "Expected an error but did not receive one.";
|
|
|
|
- } else if (result.result === test.expectedOutput) {
|
|
|
|
- ret.status = "passing";
|
|
|
|
- } else if ("expectedMatch" in test && test.expectedMatch.test(result.result)) {
|
|
|
|
- ret.status = "passing";
|
|
|
|
- } else {
|
|
|
|
- ret.status = "failing";
|
|
|
|
- const expected = test.expectedOutput ? test.expectedOutput :
|
|
|
|
- test.expectedMatch ? test.expectedMatch.toString() : "unknown";
|
|
|
|
- ret.output = [
|
|
|
|
- "Expected",
|
|
|
|
- "\t" + expected.replace(/\n/g, "\n\t"),
|
|
|
|
- "Received",
|
|
|
|
- "\t" + result.result.replace(/\n/g, "\n\t"),
|
|
|
|
- ].join("\n");
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- return ret;
|
|
|
|
- });
|
|
|
|
- })
|
|
|
|
- );
|
|
|
|
|
|
+ async runTests () {
|
|
|
|
+ const progBar = new cliProgress.SingleBar({
|
|
|
|
+ format: formatter,
|
|
|
|
+ stopOnComplete: true
|
|
|
|
+ }, cliProgress.Presets.shades_classic);
|
|
|
|
+ const testResults = [];
|
|
|
|
+
|
|
|
|
+ console.log("Running operation tests...");
|
|
|
|
+ progBar.start(this.tests.length, 0, {
|
|
|
|
+ msg: "Setting up"
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ for (const test of this.tests) {
|
|
|
|
+ progBar.update(testResults.length, {
|
|
|
|
+ msg: test.name
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ const chef = new Chef();
|
|
|
|
+ const result = await chef.bake(
|
|
|
|
+ test.input,
|
|
|
|
+ test.recipeConfig,
|
|
|
|
+ {},
|
|
|
|
+ 0,
|
|
|
|
+ false
|
|
|
|
+ );
|
|
|
|
+
|
|
|
|
+ const ret = {
|
|
|
|
+ test: test,
|
|
|
|
+ status: null,
|
|
|
|
+ output: null,
|
|
|
|
+ duration: result.duration
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ if (result.error) {
|
|
|
|
+ if (test.expectedError) {
|
|
|
|
+ ret.status = "passing";
|
|
|
|
+ } else {
|
|
|
|
+ ret.status = "erroring";
|
|
|
|
+ ret.output = result.error.displayStr;
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ if (test.expectedError) {
|
|
|
|
+ ret.status = "failing";
|
|
|
|
+ ret.output = "Expected an error but did not receive one.";
|
|
|
|
+ } else if (result.result === test.expectedOutput) {
|
|
|
|
+ ret.status = "passing";
|
|
|
|
+ } else if ("expectedMatch" in test && test.expectedMatch.test(result.result)) {
|
|
|
|
+ ret.status = "passing";
|
|
|
|
+ } else {
|
|
|
|
+ ret.status = "failing";
|
|
|
|
+ const expected = test.expectedOutput ? test.expectedOutput :
|
|
|
|
+ test.expectedMatch ? test.expectedMatch.toString() : "unknown";
|
|
|
|
+ ret.output = [
|
|
|
|
+ "Expected",
|
|
|
|
+ "\t" + expected.replace(/\n/g, "\n\t"),
|
|
|
|
+ "Received",
|
|
|
|
+ "\t" + result.result.replace(/\n/g, "\n\t"),
|
|
|
|
+ ].join("\n");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ testResults.push(ret);
|
|
|
|
+ progBar.increment();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return testResults;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
* Run all api related tests and wrap results in report format
|
|
* Run all api related tests and wrap results in report format
|
|
*/
|
|
*/
|
|
- runApiTests() {
|
|
|
|
- return Promise.all(this.apiTests.map(async function(test, i) {
|
|
|
|
|
|
+ async runApiTests() {
|
|
|
|
+ const progBar = new cliProgress.SingleBar({
|
|
|
|
+ format: formatter,
|
|
|
|
+ stopOnComplete: true
|
|
|
|
+ }, cliProgress.Presets.shades_classic);
|
|
|
|
+ const testResults = [];
|
|
|
|
+
|
|
|
|
+ console.log("Running Node API tests...");
|
|
|
|
+ progBar.start(this.apiTests.length, 0, {
|
|
|
|
+ msg: "Setting up"
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ global.TESTING = true;
|
|
|
|
+ for (const test of this.apiTests) {
|
|
|
|
+ progBar.update(testResults.length, {
|
|
|
|
+ msg: test.name
|
|
|
|
+ });
|
|
|
|
+
|
|
const result = {
|
|
const result = {
|
|
test: test,
|
|
test: test,
|
|
status: null,
|
|
status: null,
|
|
- output: null,
|
|
|
|
|
|
+ output: null
|
|
};
|
|
};
|
|
try {
|
|
try {
|
|
await test.run();
|
|
await test.run();
|
|
@@ -117,10 +150,37 @@ class TestRegister {
|
|
result.status = "erroring";
|
|
result.status = "erroring";
|
|
result.output = e.message;
|
|
result.output = e.message;
|
|
}
|
|
}
|
|
- return result;
|
|
|
|
- }));
|
|
|
|
|
|
+
|
|
|
|
+ testResults.push(result);
|
|
|
|
+ progBar.increment();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return testResults;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Formatter for the progress bar
|
|
|
|
+ *
|
|
|
|
+ * @param {Object} options
|
|
|
|
+ * @param {Object} params
|
|
|
|
+ * @param {Object} payload
|
|
|
|
+ * @returns {string}
|
|
|
|
+ */
|
|
|
|
+function formatter(options, params, payload) {
|
|
|
|
+ const bar = options.barCompleteString.substr(0, Math.round(params.progress * options.barsize)) +
|
|
|
|
+ options.barIncompleteString.substr(0, Math.round((1-params.progress) * options.barsize));
|
|
|
|
+
|
|
|
|
+ const percentage = Math.floor(params.progress * 100),
|
|
|
|
+ duration = Math.floor((Date.now() - params.startTime) / 1000);
|
|
|
|
+
|
|
|
|
+ let testName = payload.msg ? payload.msg : "";
|
|
|
|
+ if (params.value >= params.total) testName = "Tests completed";
|
|
|
|
+ testName = Utils.truncate(testName, 25).padEnd(25, " ");
|
|
|
|
+
|
|
|
|
+ return `${testName} ${bar} ${params.value}/${params.total} | ${percentage}% | Duration: ${duration}s`;
|
|
|
|
+}
|
|
|
|
+
|
|
// Export an instance to make a singleton
|
|
// Export an instance to make a singleton
|
|
export default new TestRegister();
|
|
export default new TestRegister();
|