clean-code-metrics
Version:
metrics for clean code
90 lines (77 loc) • 2.6 kB
text/typescript
import TaskMetrics from "../index";
import { TaskOccurrence } from "../types/TaskMetrics";
import { SummaryTask } from "../types/SummaryTask";
import { TaskFile } from "../types/TaskMetrics";
const configPath = "src/TEST/test.config.json";
function compareTaskOccurrences(a: TaskOccurrence, b: TaskOccurrence): number {
return b.file.localeCompare(a.file) || a.lineNumber - b.lineNumber;
}
function compareSummaryTasks(a: SummaryTask, b: SummaryTask): number {
return b.task.localeCompare(a.task) || b.amount - a.amount;
}
function sortTaskFiles(files: TaskFile[]): TaskFile[] {
return files
.sort((a, b) => b.file.localeCompare(a.file))
.map((f) => {
f.tasks.sort((a, b) => {
return a.lineNumber - b.lineNumber;
});
return f;
});
}
describe("collectTasks returns right json", () => {
it("is equal to snapshot", async () => {
const metrics = await TaskMetrics.createMetrics(configPath);
expect(
metrics.get().getRawData().sort(compareTaskOccurrences),
).toMatchSnapshot();
});
});
describe("collect Tasks with ignoreCase:false", () => {
it("is equal to snapshot", async () => {
const metrics = await TaskMetrics.createMetrics(configPath);
const list = metrics.get().getRawData().sort(compareTaskOccurrences);
expect(list).toMatchSnapshot();
});
});
describe("taskSummary", () => {
it("summary is equal to snapshot", async () => {
const metrics = await TaskMetrics.createMetrics(configPath);
expect(
metrics.get().taskSummary(true).sort(compareSummaryTasks),
).toMatchSnapshot();
expect(
metrics.get().taskSummary().sort(compareSummaryTasks),
).toMatchSnapshot();
});
});
describe("getTasksOfType", () => {
it("summary is equal to snapshot", async () => {
const metrics = await TaskMetrics.createMetrics(configPath);
const occurrences = metrics
.get()
.getTasksOfType("FIXME")
.sort(compareTaskOccurrences);
expect(occurrences.length).toMatchInlineSnapshot(`1`);
expect(occurrences).toMatchSnapshot();
expect(
metrics.get().getTasksOfType(["FIXME"]).sort(compareTaskOccurrences),
).toMatchSnapshot();
});
});
describe("getList", () => {
it("summary is equal to snapshot", async () => {
const metrics = await TaskMetrics.createMetrics(configPath);
const list = sortTaskFiles(metrics.get().getList());
expect(list).toMatchSnapshot();
const list2 = sortTaskFiles(
metrics.get().getList(["src/TEST/exampleDirTree/test.ts"]),
);
expect(list2).toMatchSnapshot();
});
});
describe("default init works", () => {
it("without error", async () => {
await TaskMetrics.createMetrics();
});
});