var path = require("path"); var fs = require("fs"); var Module = require("module"); const jasmine = require("jasmine-core"); const booter = require("jasmine-core/lib/jasmine-core/node_boot.js"); import * as g6 from "./g6.entry"; class FailureNoticerReporter implements jasmine.Reporter { results_pending: g6.SpecResult[] = []; public results: g6.SpecResult[]; public resolve_results: Function; public reportRunnerStarting(runner: jasmine.Runner) { } public reportRunnerResults(runner: jasmine.Runner) { } public reportSuiteResults(suite: jasmine.Suite) { } public reportSpecStarting(spec: jasmine.Spec) { } public reportSpecResults(spec: jasmine.Spec) { } public log(str: string) { } public specDone(r) { this.results_pending.push(r) // console.log('Finished spec: ' + JSON.stringify(r)); } public jasmineDone() { this.results = this.results_pending; // console.log('Finished jasmine: ' + JSON.stringify(this.results)); if (this.resolve_results) { // console.log('getResults() was pending, resolving...'); this.resolve_results(this.results); this.resolve_results = null; } } public async getResults(): Promise { // console.log('getResults()...'); return new Promise((resolve: (results: g6.SpecResult[]) => void, reject) => { if (this.results) { //console.log('getResults() resolving immediately...'); return Promise.resolve(this.results); } this.resolve_results = resolve; }); } } export async function test(spec_file: string) { if (!spec_file) throw new Error("specify the spec file to run"); const failed_specs: g6.SpecResult[] = []; const results_reporter = new FailureNoticerReporter(); var jasmine_env = booter(jasmine).getEnv(); jasmine_env.addReporter(results_reporter); eval("require")(spec_file); jasmine_env.execute(); const file_results = await results_reporter.getResults(); for (const spec_result of file_results) if ((spec_result as any).failedExpectations && (spec_result as any).failedExpectations.length) failed_specs.push(spec_result); if (failed_specs.length) { for (const spec of failed_specs) { console.log(); console.log("Spec failed: " + spec.fullName); for (const expectation of spec.failedExpectations) { // console.log(expectataion.message) console.log(expectation.stack); console.log(); } console.log(); } process.exit(1); // throw new Error("failed specs"); } } test(process.argv[2]) .catch(error => { console.error(error); process.exit(1); }); // function removeJasmineFrames(text) { // var i, len, line, lines, ref; // if (text == null) { // return; // } // lines = []; // ref = text.split(/\n/); // for (i = 0, len = ref.length; i < len; i++) { // line = ref[i]; // if (line.indexOf(jasminejs) >= 0) { // continue; // } // lines.push(line); // } // return lines.join("\n"); // }; // export function loadHelpersInFolder(folder, matcher) { // var e, error1, file, folderStats, help, helper, helperItem, helperNames, helpers, i, key, len; // folderStats = fs.statSync(folder); // if (folderStats.isFile()) { // folder = path.dirname(folder); // } // helpers = fileFinder.find([folder], matcher); // fileFinder.sortFiles(helpers); // helperNames = []; // for (i = 0, len = helpers.length; i < len; i++) { // helper = helpers[i]; // file = helper; // try { // helperItem = require(file.replace(/\.*$/, "")); // } catch (error1) { // e = error1; // console.log("Exception loading helper: " + file); // console.log(e); // throw e; // } // for (key in helperItem) { // help = helperItem[key]; // global[key] = help; // helperNames.push(key); // } // } // return (global as any).loadedHelpers = helperNames; // };