Skip to content

Commit

Permalink
💯 Test overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
markbrouch committed Dec 20, 2019
1 parent 4003266 commit 1986a37
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 2 deletions.
112 changes: 112 additions & 0 deletions src/__tests__/overrides.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import { CLIEngine } from "eslint";

import {
stripEmptyFiles,
stripNullRuleIds,
mapReportToOverrides
} from "../overrides";

function generateLintResult(ruleIds: string[] = []): CLIEngine.LintResult {
return {
filePath: "foo",
messages: ruleIds.map(ruleId => ({
column: 0,
line: 0,
ruleId,
message: "",
nodeType: "",
severity: 0,
source: ""
})),
errorCount: 0,
warningCount: 0,
fixableErrorCount: 0,
fixableWarningCount: 0
};
}

function generateReport(
results: CLIEngine.LintResult[] = []
): CLIEngine.LintReport {
return {
results,
errorCount: 0,
warningCount: 0,
fixableErrorCount: 0,
fixableWarningCount: 0
};
}

describe("stripEmptyFiles", () => {
test("removes results with no messages", () => {
const results = [
generateLintResult(),
generateLintResult(["foo", "bar"]),
generateLintResult()
];

expect(stripEmptyFiles(results)).toEqual([results[1]]);
});
});

describe("stripNullRuleIds", () => {
test("removes null rule IDs", () => {
const ruleIds = ["foo", null, "bar", null, "baz"];

expect(stripNullRuleIds(ruleIds)).toEqual(["foo", "bar", "baz"]);
});
});

describe("mapReportToOverrides", () => {
test("creates a list of overrides from a report", () => {
const report = generateReport([generateLintResult(["foo", "bar"])]);

expect(mapReportToOverrides(report, "warn")).toEqual([
{
files: ["foo"],
rules: {
foo: "warn",
bar: "warn"
}
}
]);
});

test("sets the level of the rules", () => {
const report = generateReport([generateLintResult(["foo", "bar"])]);

expect(mapReportToOverrides(report, "warn")).toEqual([
{
files: ["foo"],
rules: {
foo: "warn",
bar: "warn"
}
}
]);

expect(mapReportToOverrides(report, "off")).toEqual([
{
files: ["foo"],
rules: {
foo: "off",
bar: "off"
}
}
]);
});

test("combines duplicate rule violations", () => {
const report = generateReport([generateLintResult(["foo", "foo", "bar"])]);

expect(mapReportToOverrides(report, "warn")).toEqual([
{
files: ["foo"],
rules: {
foo: "warn",
bar: "warn"
}
}
]);
});
});
4 changes: 2 additions & 2 deletions src/overrides.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import path from "path";
import { CLIEngine, Linter } from "eslint";

function stripEmptyFiles(
export function stripEmptyFiles(
results: CLIEngine.LintResult[]
): CLIEngine.LintResult[] {
return results.filter(result => !!result.messages.length);
}

function stripNullRuleIds(ruleIds: (string | null)[]): string[] {
export function stripNullRuleIds(ruleIds: (string | null)[]): string[] {
return ruleIds.filter((ruleId): ruleId is string => ruleId !== null);
}

Expand Down

0 comments on commit 1986a37

Please sign in to comment.