Skip to content

Commit

Permalink
removed while true
Browse files Browse the repository at this point in the history
  • Loading branch information
Rlyehan committed May 16, 2023
1 parent 7c72b7d commit bc5456d
Show file tree
Hide file tree
Showing 19 changed files with 306 additions and 310 deletions.
108 changes: 54 additions & 54 deletions .cylintrc.json
Original file line number Diff line number Diff line change
@@ -1,55 +1,55 @@
{
"testDirectory": "./sample_tests",
"rules": [
{
"id": "no-hardcoded-wait",
"description": "Disallow the use of hardcoded wait times.",
"enabled": true
},
{
"id": "use-data-attributes",
"description": "Use data-* attributes for selectors.",
"enabled": true
},
{
"id": "avoid-tag-class-id-selectors",
"description": "Avoid using tag, class, or ID selectors.",
"enabled": true
},
{
"id": "avoid-command-return-value-assignments",
"description": "Avoid assigning the return value of Cypress commands with const, let, or var.",
"enabled": true
},
{
"id": "no-external-site-visit",
"description": "Avoid visiting external sites or servers you do not control.",
"enabled": true
},
{
"id": "no-test-state-dependence",
"description": "Avoid having tests rely on the state of previous tests.",
"enabled": true
},
{
"id": "no-web-server-in-cypress",
"description": "Avoid starting a web server from within Cypress scripts with cy.exec() or cy.task().",
"enabled": true
},
{
"id": "at-least-one-assertion",
"description": "There should be at least one assertion in each it block.",
"enabled": true
},
{
"id": "use-base-url",
"description": "Use base URL from config instead of full URLs.",
"enabled": true
},
{
"id": "no-hardcoded-credentials",
"description": "Avoid using hardcoded credentials in tests, use environment variables or fixtures instead.",
"enabled": true
}
]
}
"testDirectory": "./sample_tests",
"rules": [
{
"id": "no-hardcoded-wait",
"description": "Disallow the use of hardcoded wait times.",
"enabled": true
},
{
"id": "use-data-attributes",
"description": "Use data-* attributes for selectors.",
"enabled": true
},
{
"id": "avoid-tag-class-id-selectors",
"description": "Avoid using tag, class, or ID selectors.",
"enabled": true
},
{
"id": "avoid-command-return-value-assignments",
"description": "Avoid assigning the return value of Cypress commands with const, let, or var.",
"enabled": true
},
{
"id": "no-external-site-visit",
"description": "Avoid visiting external sites or servers you do not control.",
"enabled": true
},
{
"id": "no-test-state-dependence",
"description": "Avoid having tests rely on the state of previous tests.",
"enabled": true
},
{
"id": "no-web-server-in-cypress",
"description": "Avoid starting a web server from within Cypress scripts with cy.exec() or cy.task().",
"enabled": true
},
{
"id": "at-least-one-assertion",
"description": "There should be at least one assertion in each it block.",
"enabled": true
},
{
"id": "use-base-url",
"description": "Use base URL from config instead of full URLs.",
"enabled": true
},
{
"id": "no-hardcoded-credentials",
"description": "Avoid using hardcoded credentials in tests, use environment variables or fixtures instead.",
"enabled": true
}
]
}
2 changes: 1 addition & 1 deletion report.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"timestamp": "2023-05-16T07:27:41.830Z",
"timestamp": "2023-05-16T07:58:32.617Z",
"violations": [
{
"filepath": "sample_tests/example.cy.ts",
Expand Down
10 changes: 5 additions & 5 deletions sample_tests/example.cy.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"use strict";
// sample_tests/example.spec.ts
describe("Sample test suite", () => {
it("Sample test case", () => {
cy.visit("/");
cy.wait(1000);
cy.get("#element");
});
it("Sample test case", () => {
cy.visit("/");
cy.wait(1000);
cy.get("#element");
});
});
4 changes: 2 additions & 2 deletions sample_tests/example.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
describe("Sample test suite", () => {
it("Sample test case", () => {
cy.visit("/");
cy.wait(1000);
cy.get("#element");
cy.wait(1000);
cy.get("#element");
});
});
44 changes: 22 additions & 22 deletions sample_tests/example2.cy.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
"use strict";
describe("Violations of all rules", () => {
it("State dependence violation", () => {
cy.visit("/users/new");
cy.get('[data-testid="first-name"]').type("Johnny");
cy.get('[data-testid="last-name"]').type("Appleseed");
cy.get("form").submit();
it("State dependence violation", () => {
cy.visit("/users/new");
cy.get('[data-testid="first-name"]').type("Johnny");
cy.get('[data-testid="last-name"]').type("Appleseed");
cy.get("form").submit();
});
it("No assertion violation", () => {
cy.visit("/users");
// No assertions here
});
describe("Base URL violation", () => {
it("does something", () => {
cy.visit("https://example.com/users");
});
it("No assertion violation", () => {
cy.visit("/users");
// No assertions here
});
describe("Hardcoded credentials violation", () => {
beforeEach(() => {
cy.visit("/login");
cy.get("#username").type("test_user");
cy.get("#password").type("test_password");
});
describe("Base URL violation", () => {
it("does something", () => {
cy.visit("https://example.com/users");
});
});
describe("Hardcoded credentials violation", () => {
beforeEach(() => {
cy.visit("/login");
cy.get("#username").type("test_user");
cy.get("#password").type("test_password");
});
it("does something with hardcoded credentials", () => {
// Test code here
});
it("does something with hardcoded credentials", () => {
// Test code here
});
});
});
32 changes: 17 additions & 15 deletions src/__tests__/atleastoneAssertion.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
const atLeastOneAssertion_1 = require("../rules/implementations/atLeastOneAssertion");
const test_utils_1 = require("../test_utils");
describe("atLeastOneAssertionPerItBlock rule", () => {
it("should pass when there is at least one assertion in each it block", () => {
const code = `
it("should pass when there is at least one assertion in each it block", () => {
const code = `
describe('my form', () => {
it('displays form validation', () => {
cy.get('.error').should('be.visible');
Expand All @@ -15,14 +15,14 @@ describe("atLeastOneAssertionPerItBlock rule", () => {
});
});
`;
const violations = [];
(0, test_utils_1.traverseAst)(code, (node) => {
violations.push(...(0, atLeastOneAssertion_1.atLeastOneAssertion)(node));
});
expect(violations.length).toBe(0);
const violations = [];
(0, test_utils_1.traverseAst)(code, (node) => {
violations.push(...(0, atLeastOneAssertion_1.atLeastOneAssertion)(node));
});
it("should fail when there is no assertion in an it block", () => {
const code = `
expect(violations.length).toBe(0);
});
it("should fail when there is no assertion in an it block", () => {
const code = `
describe('my form', () => {
it('displays form validation', () => {
cy.get('.error').should('be.visible');
Expand All @@ -33,11 +33,13 @@ describe("atLeastOneAssertionPerItBlock rule", () => {
});
});
`;
const violations = [];
(0, test_utils_1.traverseAst)(code, (node) => {
violations.push(...(0, atLeastOneAssertion_1.atLeastOneAssertion)(node));
});
expect(violations.length).toBe(1);
expect(violations[0].description).toBe("There should be at least one assertion in each it block");
const violations = [];
(0, test_utils_1.traverseAst)(code, (node) => {
violations.push(...(0, atLeastOneAssertion_1.atLeastOneAssertion)(node));
});
expect(violations.length).toBe(1);
expect(violations[0].description).toBe(
"There should be at least one assertion in each it block"
);
});
});
42 changes: 27 additions & 15 deletions src/__tests__/avoidCommandReturnValueAssignments.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,47 @@ Object.defineProperty(exports, "__esModule", { value: true });
const avoidCommandReturnValueAssignments_1 = require("../rules/implementations/avoidCommandReturnValueAssignments");
const test_utils_1 = require("../test_utils");
describe("avoidCommandReturnValueAssignments rule", () => {
it("should pass when no command return value assignments are present", () => {
const code = `
it("should pass when no command return value assignments are present", () => {
const code = `
describe('Test without command return value assignments', () => {
it('does something', () => {
cy.visit('/users');
cy.get('#username').type('test_user');
});
});
`;
const violations = [];
(0, test_utils_1.traverseAst)(code, (node) => {
violations.push(...(0, avoidCommandReturnValueAssignments_1.avoidCommandReturnValueAssignments)(node));
});
expect(violations.length).toBe(0);
const violations = [];
(0, test_utils_1.traverseAst)(code, (node) => {
violations.push(
...(0,
avoidCommandReturnValueAssignments_1.avoidCommandReturnValueAssignments)(
node
)
);
});
it("should fail when command return value assignments are present", () => {
const code = `
expect(violations.length).toBe(0);
});
it("should fail when command return value assignments are present", () => {
const code = `
describe('Test with command return value assignments', () => {
it('does something', () => {
const element = cy.get('#username');
element.type('test_user');
});
});
`;
const violations = [];
(0, test_utils_1.traverseAst)(code, (node) => {
violations.push(...(0, avoidCommandReturnValueAssignments_1.avoidCommandReturnValueAssignments)(node));
});
expect(violations.length).toBe(1);
expect(violations[0].description).toBe("Avoid assigning the return value of Cypress commands with const, let, or var");
const violations = [];
(0, test_utils_1.traverseAst)(code, (node) => {
violations.push(
...(0,
avoidCommandReturnValueAssignments_1.avoidCommandReturnValueAssignments)(
node
)
);
});
expect(violations.length).toBe(1);
expect(violations[0].description).toBe(
"Avoid assigning the return value of Cypress commands with const, let, or var"
);
});
});
40 changes: 23 additions & 17 deletions src/__tests__/avoidTagClassIdSelectors.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@ Object.defineProperty(exports, "__esModule", { value: true });
const avoidTagClassId_1 = require("../rules/implementations/avoidTagClassId");
const test_utils_1 = require("../test_utils");
describe("avoidTagClassIdSelectors rule", () => {
it("should pass when no tag, class, or ID selectors are used", () => {
const code = `
it("should pass when no tag, class, or ID selectors are used", () => {
const code = `
describe('Test without tag, class, or ID selectors', () => {
it('does something', () => {
cy.get('[data-testid="submit-button"]').click();
});
});
`;
const violations = [];
(0, test_utils_1.traverseAst)(code, (node) => {
violations.push(...(0, avoidTagClassId_1.avoidTagClassIdSelectors)(node));
});
expect(violations.length).toBe(0);
const violations = [];
(0, test_utils_1.traverseAst)(code, (node) => {
violations.push(...(0, avoidTagClassId_1.avoidTagClassIdSelectors)(node));
});
it("should fail when tag, class, or ID selectors are used", () => {
const code = `
expect(violations.length).toBe(0);
});
it("should fail when tag, class, or ID selectors are used", () => {
const code = `
describe('Test with tag, class, or ID selectors', () => {
it('does something', () => {
cy.get('#submit-button').click();
Expand All @@ -27,13 +27,19 @@ describe("avoidTagClassIdSelectors rule", () => {
});
});
`;
const violations = [];
(0, test_utils_1.traverseAst)(code, (node) => {
violations.push(...(0, avoidTagClassId_1.avoidTagClassIdSelectors)(node));
});
expect(violations.length).toBe(3);
expect(violations[0].description).toBe("Avoid using tag, class, or ID selectors. Found: '#submit-button'");
expect(violations[1].description).toBe("Avoid using tag, class, or ID selectors. Found: '.submit-button'");
expect(violations[2].description).toBe("Avoid using tag, class, or ID selectors. Found: 'button'");
const violations = [];
(0, test_utils_1.traverseAst)(code, (node) => {
violations.push(...(0, avoidTagClassId_1.avoidTagClassIdSelectors)(node));
});
expect(violations.length).toBe(3);
expect(violations[0].description).toBe(
"Avoid using tag, class, or ID selectors. Found: '#submit-button'"
);
expect(violations[1].description).toBe(
"Avoid using tag, class, or ID selectors. Found: '.submit-button'"
);
expect(violations[2].description).toBe(
"Avoid using tag, class, or ID selectors. Found: 'button'"
);
});
});
Loading

0 comments on commit bc5456d

Please sign in to comment.