Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: only and skip support #55

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions example_tests/only/only_case.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Rhum } from "../../mod.ts";

Rhum.testPlan("test_plan_1", () => {
Rhum.testSuite("test_suite_1a", () => {
Rhum.testCase("test_case_1a1", () => {
Rhum.asserts.assertEquals(true, true);
});
Rhum.testCase("test_case_1a2", () => {
Rhum.asserts.assertEquals(true, true);
});
Rhum.testCase("test_case_1a3", () => {
Rhum.asserts.assertEquals(true, true);
});
});

Rhum.testSuite("test_suite_1b", () => {
Rhum.testCase("test_case_1b1", () => {
Rhum.asserts.assertEquals(true, true);
});
Rhum.only("test_case_1b2", () => {
Rhum.asserts.assertEquals(true, true);
});
Rhum.testCase("test_case_1b3", () => {
Rhum.asserts.assertEquals(true, true);
});
});
});

Rhum.run();
29 changes: 29 additions & 0 deletions example_tests/only/only_suite.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Rhum } from "../../mod.ts";

Rhum.testPlan("test_plan_1", () => {
Rhum.testSuite("test_suite_1a", () => {
Rhum.testCase("test_case_1a1", () => {
Rhum.asserts.assertEquals(true, true);
});
Rhum.testCase("test_case_1a2", () => {
Rhum.asserts.assertEquals(true, true);
});
Rhum.testCase("test_case_1a3", () => {
Rhum.asserts.assertEquals(true, true);
});
});

Rhum.only("test_suite_1b", () => {
Rhum.testCase("test_case_1b1", () => {
Rhum.asserts.assertEquals(true, true);
});
Rhum.testCase("test_case_1b2", () => {
Rhum.asserts.assertEquals(true, true);
});
Rhum.testCase("test_case_1b3", () => {
Rhum.asserts.assertEquals(true, true);
});
});
});

Rhum.run();
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();
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to figure this out

}


/**
* 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
93 changes: 93 additions & 0 deletions tests/integration/only_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { StdAsserts as asserts, colors } from "../../deps.ts";
import {Rhum} from "../../mod.ts";

/**
* To be clear, we are making sure that when a user runs their tests using Bourbon, that everything works correctly,
* and the output is correct
*/

Deno.test({
name: "Integration | only_test.ts | Only runs the test case that has `Rhum.only`",
async fn(): Promise<void> {
const p = await Deno.run({
cmd: [
"deno",
"test",
"--allow-run",
"--allow-env",
"example_tests/only/only_case.ts",
],
stdout: "piped",
stderr: "piped",
env: {"NO_COLOR": "false"},
});
const status = await p.status();
p.close();
asserts.assertEquals(status.success, true);
asserts.assertEquals(status.code, 0);
/**
* Because the timing for each test is dynamic, we can't really test it ("... ok (3ms)"), so strip all that out
*/
let stdout = new TextDecoder("utf-8")
.decode(await p.output())
.replace(/\(\d+ms\)/g, ""); // (*ms)
/**
* There are also some odd empty lines at the end of the stdout... so we just strip those out
*/
stdout = stdout.substring(0, stdout.indexOf("filtered out") + 12);

Rhum.asserts.assertEquals(stdout,
"running 1 tests\n" +
" \n" +
"test_plan_1\n" +
" test_suite_1b\n" +
" test_case_1b2 ... ok \n" +
"\n" +
"test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out "
)
}
})

Deno.test({
name: "Integration | only_test.ts | Only runs the test suite that has `Rhum.only`",
async fn(): Promise<void> {
const p = await Deno.run({
cmd: [
"deno",
"test",
"--allow-run",
"--allow-env",
"example_tests/only/only_suite.ts",
],
stdout: "piped",
stderr: "piped",
env: {"NO_COLOR": "false"},
});
const status = await p.status();
p.close();
asserts.assertEquals(status.success, true);
asserts.assertEquals(status.code, 0);
/**
* Because the timing for each test is dynamic, we can't really test it ("... ok (3ms)"), so strip all that out
*/
let stdout = new TextDecoder("utf-8")
.decode(await p.output())
.replace(/\(\d+ms\)/g, ""); // (*ms)
/**
* There are also some odd empty lines at the end of the stdout... so we just strip those out
*/
stdout = stdout.substring(0, stdout.indexOf("filtered out") + 12);

Rhum.asserts.assertEquals(stdout,
"running 1 tests\n" +
" \n" +
"test_plan_1\n" +
" test_suite_1b\n" +
" test_case_1b1 ... ok \n" +
" test_case_1b2 ... ok \n" +
" test_case_1b2 ... ok \n" +
"\n" +
"test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out "
)
}
})