diff --git a/mod.ts b/mod.ts index 188f6ada..4ef70413 100644 --- a/mod.ts +++ b/mod.ts @@ -75,7 +75,7 @@ export class RhumRunner { protected test_suite_in_progress = ""; - protected plan: ITestPlan = { suites: {} }; + protected plan: ITestPlan = { suites: {}, only: false }; // FILE MARKER - METHODS - CONSTRUCTOR /////////////////////////////////////// @@ -224,9 +224,29 @@ export class RhumRunner { } } - // public only(cb: Function): void { - // // Do something - // } + /** + * Can be used for suites and cases. Will only run that. + * You can just switch a `Rhum.testSuite(...` to `Rhum,only(...` + * + * @param name - The name + * @param cb - The callback + */ + public only(name: string, cb: Function): void { + // FIXME(edward) Any test suite within a plan will a ctually hit this block, if there was a test suite before it. It should not hit this block + if (this.passed_in_test_plan && this.passed_in_test_suite) { // is a test case being skipped + this.plan.suites[this.passed_in_test_suite].cases!.push({ + name, + new_name: this.formatTestCaseName(name), + testFn: cb, + only: true + }); + } else if (this.passed_in_test_plan) { // is a test suite being skipped + this.passed_in_test_suite = name; + this.plan.suites![name] = {cases: [], only: true}; + cb(); + } + } + /** * Allows a test plan, suite, or case to be skipped when the tests run. @@ -334,6 +354,7 @@ export class RhumRunner { name, new_name: this.formatTestCaseName(name), testFn, + only: false }); } @@ -374,7 +395,7 @@ export class RhumRunner { */ public testSuite(name: string, testCases: Function): void { this.passed_in_test_suite = name; - this.plan.suites![name] = { cases: [] }; + this.plan.suites![name] = { cases: [], only: false }; testCases(); } @@ -388,6 +409,24 @@ export class RhumRunner { * Rhum.run(); */ public run(): void { + const onlySuite: string|undefined = Object.keys(this.plan.suites).filter(suiteName => this.plan.suites[suiteName].only === true)[0] + const onlyCase: string|undefined = Object.keys(this.plan.suites).filter(suiteName => this.plan.suites[suiteName].cases!.filter(c => c.only === true).length > 0)[0] + if (onlySuite) { + this.plan.suites = { + [onlySuite]: this.plan.suites[onlySuite] + }; + } else if (onlyCase) { + // Select that test case + this.plan.suites = { + [onlyCase]: this.plan.suites[onlyCase] + } + this.plan.suites[onlyCase].cases = this.plan.suites[onlyCase].cases!.filter(c => c.only === true) + // We need to re-format the name to make it display the plan and suite + const tmpTestPlanInProgress = this.test_suite_in_progress + this.test_plan_in_progress = ""; + this.plan.suites[onlyCase].cases![0].new_name = this.formatTestCaseName(this.plan.suites[onlyCase].cases![0].name); + this.passed_in_test_suite = tmpTestPlanInProgress + } const tc = new TestCase(this.plan); tc.run(); this.deconstruct(); @@ -455,7 +494,7 @@ export class RhumRunner { this.passed_in_test_plan = ""; this.test_plan_in_progress = ""; this.test_suite_in_progress = ""; - this.plan = { suites: {} }; + this.plan = { suites: {}, only: false }; } } diff --git a/src/interfaces.ts b/src/interfaces.ts index 00a1c93e..de7bbf37 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -72,6 +72,7 @@ export interface ITestPlan { suites: { [key: string]: ITestSuite; // "key" is the suite name }; + only: boolean; after_all_suite_hook?: Function; after_each_suite_hook?: Function; before_all_suite_hook?: Function; @@ -96,6 +97,7 @@ export interface ITestPlan { */ export interface ITestSuite { cases?: ITestCase[]; + only: boolean; after_all_case_hook?: Function; after_each_case_hook?: Function; before_all_case_hook?: Function; @@ -118,6 +120,7 @@ export interface ITestSuite { */ export interface ITestCase { name: string; + only: boolean; new_name: string; testFn: Function; } diff --git a/src/test_case.ts b/src/test_case.ts index b3466598..67ca82fd 100644 --- a/src/test_case.ts +++ b/src/test_case.ts @@ -58,6 +58,7 @@ export class TestCase { await this.plan.after_all_suite_hook(); } }; + // (ebebbington) To stop the output of test running being horrible // in the CI, we will only display the new name which should be // "plan | suite " case", as opposed to the "super saiyan"