UNPKG

flagpole

Version:

Simple and fast DOM integration, headless or headful browser, and REST API testing framework.

184 lines 6.27 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const cli_1 = require("./cli"); const __1 = require(".."); const flagpole_1 = require("../flagpole"); const exec = require('child_process').exec; class SuiteExecution { constructor(suiteConfig) { this._output = []; this._exitCode = null; this.config = suiteConfig; } get output() { return this._output.join(' '); } get exitCode() { return this._exitCode; } run(callback) { const suite = this; const filePath = this.config.getPath(); this._onBefore(); let opts = ''; if (__1.Flagpole.getEnvironment()) { opts += ' -e ' + __1.Flagpole.getEnvironment(); } if (__1.Flagpole.quietMode) { opts += ' -q'; } if (__1.Flagpole.logOutput) { opts += ' -l'; } opts += ' -x'; opts += ' -o ' + __1.Flagpole.output; TestRunner.execute(filePath, opts) .then((result) => { this._exitCode = result.exitCode; suite._output = result.output; suite._onAfter(result.exitCode); callback(suite); }) .catch((ex) => { console.log(ex); }); } _reset() { this._output = []; this._exitCode = null; } _onBefore() { this._reset(); } _onAfter(exitCode) { this._exitCode = exitCode; } } exports.SuiteExecution = SuiteExecution; class TestRunner { constructor() { this.suites = {}; this._timeStart = Date.now(); } static execute(filePath, opts) { return new Promise((resolve) => { let child = exec(`node ${filePath} ${opts}`); let output = []; child.stdout.on('data', function (data) { data && output.push(data); }); child.stderr.on('data', function (data) { data && output.push(data); }); child.on('error', function (data) { data && output.push(data); }); child.on('exit', function (exitCode) { if (exitCode > 0 && __1.Flagpole.output == flagpole_1.FlagpoleOutput.console) { output.push('FAILED TEST SUITE:'); output.push(filePath + ' exited with error code ' + exitCode); output.push("\n"); } resolve({ output: output, exitCode: exitCode }); }); }); } addSuite(suiteConfig) { if (!this.suites[suiteConfig.name]) { this.suites[suiteConfig.name] = new SuiteExecution(suiteConfig); } } reset() { this.suites = {}; } getSuites() { const suiteNames = Object.keys(this.suites); let suites = []; suiteNames.forEach((suiteName) => { suites.push(this.suites[suiteName]); }); return suites; } run() { const suites = this.getSuites(); suites.forEach((suite) => { suite.run((suite) => { this._onTestExit(suite); }); }); } _onDone() { const areAllPassing = Object.keys(this.suites).every((key) => { return (this.suites[key].exitCode === 0); }); const suites = this.getSuites(); const duration = Date.now() - this._timeStart; let output = ''; if (__1.Flagpole.output == flagpole_1.FlagpoleOutput.json) { let suiteOutput = []; let overall = { pass: 0, fail: 0 }; suites.forEach((suite) => { suiteOutput.push(suite.output); if (suite.exitCode == 0) { overall.pass += 1; } else { overall.fail += 1; } }); output = `{ "summary": { "passCount": ${overall.pass}, "failCount": ${overall.fail}, "duration": ${duration} }, ` + `"suites": [${suiteOutput.join(',')}] }`; } else { suites.forEach((suite) => { output += suite.output + "\n"; }); } if (__1.Flagpole.output == flagpole_1.FlagpoleOutput.browser) { const open = require('open'); const fs = require('fs'); const tmp = require('tmp'); const tmpObj = tmp.fileSync({ postfix: '.html' }); const filePath = tmpObj.name; let template = fs.readFileSync(`${__dirname}/report.html`, 'utf8'); template = template.replace('${output}', output).replace('${nav}', ''); fs.writeFileSync(filePath, template); cli_1.Cli.log(`Writing output to: ${filePath}`); (() => __awaiter(this, void 0, void 0, function* () { yield open(filePath); cli_1.Cli.exit(areAllPassing ? 0 : 1); }))(); } else { cli_1.Cli.log(output); if (!areAllPassing && __1.Flagpole.output == flagpole_1.FlagpoleOutput.console) { cli_1.Cli.log('Some suites failed.'); } cli_1.Cli.exit(areAllPassing ? 0 : 1); } } _onTestExit(suite) { if (Object.keys(this.suites).every((key) => { return this.suites[key].exitCode !== null; })) { this._onDone(); } } ; } exports.TestRunner = TestRunner; //# sourceMappingURL=testrunner.js.map