Skip to content

Commit

Permalink
[issue-#5-only] WIP: support only
Browse files Browse the repository at this point in the history
  • Loading branch information
Edward Bebbington committed Aug 24, 2020
1 parent c749613 commit 13f143b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 6 deletions.
51 changes: 45 additions & 6 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ///////////////////////////////////////

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -334,6 +354,7 @@ export class RhumRunner {
name,
new_name: this.formatTestCaseName(name),
testFn,
only: false
});
}

Expand Down Expand Up @@ -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();
}

Expand All @@ -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();
Expand Down Expand Up @@ -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 };
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -118,6 +120,7 @@ export interface ITestSuite {
*/
export interface ITestCase {
name: string;
only: boolean;
new_name: string;
testFn: Function;
}
Expand Down
1 change: 1 addition & 0 deletions src/test_case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 13f143b

Please sign in to comment.