UNPKG

typespec-bdd

Version:

BDD framework for TypeScript.

127 lines 5.21 kB
(function (factory) { if (typeof module === "object" && typeof module.exports === "object") { var v = factory(require, exports); if (v !== undefined) module.exports = v; } else if (typeof define === "function" && define.amd) { define(["require", "exports", "./Parser", "./FileSystem", "./Steps", "./Hooks"], factory); } })(function (require, exports) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const Parser_1 = require("./Parser"); const FileSystem_1 = require("./FileSystem"); const Steps_1 = require("./Steps"); const Hooks_1 = require("./Hooks"); var Kind; (function (Kind) { Kind[Kind["Sync"] = 0] = "Sync"; Kind[Kind["Async"] = 1] = "Async"; })(Kind = exports.Kind || (exports.Kind = {})); class SpecRunner { constructor(testReporter = new Hooks_1.TestReporter(), testHooks = new Hooks_1.TestHooks()) { this.testReporter = testReporter; this.testHooks = testHooks; this.timeLimitMS = 30000; this.excludedTags = []; this.urls = []; this.expectedFiles = 0; this.completedFiles = 0; this.steps = new Steps_1.StepCollection(); this.fileReader = FileSystem_1.FileReader.getInstance(this.testReporter); } addStep(expression, step, kind = Kind.Sync) { this.steps.add(expression, step, (kind == Kind.Async)); } addStepAsync(expression, step) { this.steps.add(expression, step, true); } given(expression, step, kind = Kind.Sync) { this.steps.add(expression, step, (kind == Kind.Async), Steps_1.StepType.Given); } givenAsync(expression, step) { this.steps.add(expression, step, true, Steps_1.StepType.Given); } when(expression, step, kind = Kind.Sync) { this.steps.add(expression, step, (kind == Kind.Async), Steps_1.StepType.When); } whenAsync(expression, step) { this.steps.add(expression, step, true, Steps_1.StepType.When); } then(expression, step, kind = Kind.Sync) { this.steps.add(expression, step, (kind == Kind.Async), Steps_1.StepType.Then); } thenAsync(expression, step) { this.steps.add(expression, step, true, Steps_1.StepType.Then); } run(...url) { this.expectedFiles = url.length; this.urls = url; this.readFile(0); return new Promise((resolve, reject) => { this.runCompleted = resolve; }); } runInRandomOrder(...url) { const specList = new SpecificationList(url); this.expectedFiles = url.length; this.urls = specList.randomise(); this.readFile(0); return new Promise((resolve, reject) => { this.runCompleted = resolve; }); } excludeTags(...tags) { for (const tag of tags) { this.excludedTags.push(tag.replace(/@/g, '')); } } fileCompleted(index) { this.completedFiles++; if (this.completedFiles === this.expectedFiles) { this.testReporter.complete(); this.runCompleted(); } else { this.readFile(index); } } readFile(index) { // TODO: Probably need a timeout per file as if the test ran "forever" the overall test would never pass or fail if (index < this.urls.length) { const nextIndex = index + 1; const afterFeatureHandler = () => { this.testHooks.afterFeature(); this.fileCompleted(nextIndex); }; this.fileReader.getFile(this.urls[index], (responseText) => { this.processSpecification(responseText, afterFeatureHandler); }); } } processSpecification(spec, afterFeatureHandler) { const featureParser = new Parser_1.FeatureParser(this.testReporter, this.testHooks, this.steps, this.excludedTags); featureParser.run(spec, afterFeatureHandler); } } exports.SpecRunner = SpecRunner; class SpecificationList { constructor(specifications) { this.specifications = specifications; } randomise() { let orderedSpecs = []; while (this.specifications.length > 0) { const index = this.getRandomInt(0, this.specifications.length); orderedSpecs.push(this.specifications[index]); this.specifications.splice(index, 1); } return orderedSpecs; } getRandomInt(min, max) { return Math.floor(Math.random() * (max - min)) + min; } } exports.SpecificationList = SpecificationList; }); //# sourceMappingURL=Runner.js.map