Skip to content

Commit

Permalink
fix: Improving test coverage & efficiency
Browse files Browse the repository at this point in the history
  • Loading branch information
codejedi365 committed Oct 23, 2021
1 parent a1604e8 commit d46600a
Show file tree
Hide file tree
Showing 7 changed files with 654 additions and 235 deletions.
8 changes: 4 additions & 4 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ module.exports = {
collectCoverage: true,
coverageThreshold: {
"global": {
branches: 90,
functions: 100,
branches: 85,
lines: 90,
statements: 90
statements: 90,
functions: 100
},
"./lib/rules/expect-expect.test.ts": {
"./lib/rules/expect-expect.ts": {
// allow error handling
lines: -3,
statements: -5
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/expect-expect.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @fileoverview Don't allow debug() to be committed to the repository.
* @fileoverview Don't forget to have at least 1 expect() call in each test!
* @author Ben Monro
* @author codejedi365
*/
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-identical-title.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export default createRule({
};

const unknownFnCallENTER = (node: CallExpression) => {
if (isInsideTest && hasRecordedTestName) return; // Short circuit, already found
if (hasRecordedTestName) return; // Short circuit, already found

let fnName;
let objectName;
Expand Down
95 changes: 76 additions & 19 deletions tests/lib/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,84 @@
import { rules, generateRecommendedConfig } from "../../lib";
import fs from "fs/promises";
import { resolve } from "path";
import rulebook from "../../lib/rules";
import { configs } from "../../lib";
import "jest-extended";

it("should have all the rules", () => {
expect(Object.keys(rules).length).toBeGreaterThan(0);
let rulesSpy: jest.SpyInstance;

beforeEach(() => {
rulesSpy = jest.spyOn(rulebook, "rules", "get");
});

it.each(Object.keys(rules))("%s should export required fields", (ruleName) => {
const rule = rules[ruleName];
expect(rule).toHaveProperty("create", expect.any(Function));
expect(rule.meta.docs?.url).not.toBeEmpty();
expect(rule.meta.docs?.category).toBe("Best Practices");
expect(rule.meta.docs?.description).not.toBeEmpty();
afterEach(() => {
jest.restoreAllMocks();
});
it("should have a recommended config with recommended rules", () => {
expect(generateRecommendedConfig()).toEqual({
"testcafe-community/expectExpect": "error",
"testcafe-community/noDebug": "error",
"testcafe-community/noIdenticalTitle": "error",
"testcafe-community/noOnly": "error",
"testcafe-community/noSkip": "warn"

describe("Rule Definitions", () => {
it("should export at least one rule", () => {
expect(Object.keys(rulebook.rules).length).toBeGreaterThan(0);
});

it("should include all rule defintions", async () => {
const allFilesInRulesDir = await fs.readdir(
resolve(__dirname, "..", "..", "lib", "rules")
);
const allRuleFiles = allFilesInRulesDir.filter((filename) =>
/(.*(?<!^index)\.ts)$/.test(filename)
);
expect(Object.keys(rulebook.rules)).toBeArrayOfSize(
allRuleFiles.length
);
});

it.each(Object.keys(rulebook.rules))(
"%s should export required fields",
(ruleName) => {
const rule = rulebook.rules[ruleName];
expect(rule).toHaveProperty("create", expect.any(Function));
expect(rule.meta.docs?.url).not.toBeEmpty();
expect(rule.meta.docs?.category).toBe("Best Practices");
expect(rule.meta.docs?.description).not.toBeEmpty();
}
);
});

// it("should handle a config generation if a rule's RuleMetaDocs is obmitted", () => {
// //
// });
describe("recommended config", () => {
it("should include the testcafe expected global variables in context", () => {
expect(configs.recommended.globals).toEqual({
fixture: false,
test: false
});
});

it("should specify this plugin in the plugins list", () => {
expect(configs.recommended.plugins).toEqual(["testcafe-community"]);
});

it("should include the recommended rules from this plugin", () => {
expect(configs.recommended.rules).toEqual({
"testcafe-community/expectExpect": "error",
"testcafe-community/noDebug": "error",
"testcafe-community/noIdenticalTitle": "error",
"testcafe-community/noOnly": "error",
"testcafe-community/noSkip": "warn"
});
});

it("should not include rule in the recommended config if RuleMetaDocs is obmitted", () => {
const allRules = rulebook.rules;
rulesSpy.mockImplementation(() => {
const ruleEntry = Object.entries(allRules)[0];
const aRecommendedRule = JSON.parse(
JSON.stringify(ruleEntry[1])
) as typeof ruleEntry[1];
delete aRecommendedRule.meta.docs;
return { [ruleEntry[0]]: aRecommendedRule };
});
// eslint-disable-next-line global-require, @typescript-eslint/no-var-requires
const thisModule = require("../../lib") as typeof import("../../lib");
expect(
Object.keys(thisModule.configs.recommended.rules || {})
).toBeArrayOfSize(0);
});
});

0 comments on commit d46600a

Please sign in to comment.