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

make the test results fit in a table with timestamps #423

Merged
merged 10 commits into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ dist
log.md
.env
bins
.DS_Store
**/target/
*.swp
.vscode
4 changes: 4 additions & 0 deletions src/test-runner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,10 @@ export async function run(
test.timeout(0);
}

// pass the file path, don't load the reporter as a module
const resolvedReporterPath = path.resolve(__dirname, "../utils/testReporter");
mocha.reporter(resolvedReporterPath);

// run
mocha.run(exitMocha);
}
Expand Down
78 changes: 78 additions & 0 deletions src/utils/testReporter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import Mocha from "mocha";

import { decorators } from "../utils/colors";
import { CreateLogTable } from "../utils/tableCli";

const { EVENT_RUN_END, EVENT_TEST_FAIL, EVENT_TEST_PASS, EVENT_RUN_BEGIN } =
Mocha.Runner.constants;

interface TestReporterProps {
runner: Mocha.Runner;
stats: Mocha.Stats;
on: any;
once: any;
}

class TestReporter {
constructor(runner: TestReporterProps) {
const stats = runner.stats!;

const logTable = new CreateLogTable({
head: [
{
colSpan: 2,
hAlign: "center",
content: `${decorators.green("Test Results")}`,
},
],
colWidths: [30, 100],
});

let announcement = new CreateLogTable({
colWidths: [120],
});
announcement.pushToPrint([
[
decorators.green(
"🛎️ Tests are currently running. Results will appear at the end",
),
],
]);
runner
.once(EVENT_RUN_BEGIN, () => {
announcement.print();
})
.on(EVENT_TEST_PASS, (test: Mocha.Test) => {
logTable.pushTo([
[
new Date().toLocaleString(),
`✅ ${test.title} ${decorators.red(`(${test.duration}ms)`)}`,
],
]);
})
.on(EVENT_TEST_FAIL, (test: Mocha.Test, err: any) => {
logTable.pushTo([
[
new Date().toLocaleString(),
`❌ ${test.title} ${decorators.red(`(${test.duration}ms)`)}`,
],
]);
})
.once(EVENT_RUN_END, () => {
logTable.pushTo([
[
{
colSpan: 2,
hAlign: "left",
content: `Result: ${stats.passes}/${
stats.passes + stats.failures
}`,
},
],
]);
logTable.print();
});
}
}

module.exports = TestReporter;