Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
MatanYadaev committed Aug 16, 2023
1 parent 5bc3be2 commit 8ec6d73
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/breezy-tigers-talk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-testing": patch
---

Enforce Arrange comment
5 changes: 5 additions & 0 deletions .changeset/chilly-garlics-thank.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-testing": patch
---

Require AAA to be in correct order
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"scripts": {
"build": "tsup src/index.ts --format cjs,esm --clean --minify",
"release": "pnpm build && changeset publish",
"changesets:add": "changeset add",
"test": "vitest run",
"eslint-docs:update": "pnpm build && eslint-doc-generator",
"eslint-docs:check": "pnpm build && eslint-doc-generator --check",
Expand Down
32 changes: 26 additions & 6 deletions src/rules/aaa-comments.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ ruleTester.run(RULE_NAME, rule, {
`,
`
test('bar', () => {
// Arrange
// Act
const data = getData();
Expand Down Expand Up @@ -95,7 +96,7 @@ ruleTester.run(RULE_NAME, rule, {
`,
errors: [
{
messageId: 'default',
messageId: 'missing',
line: 2,
}
]
Expand All @@ -110,7 +111,7 @@ ruleTester.run(RULE_NAME, rule, {
`,
errors: [
{
messageId: 'default',
messageId: 'missing',
line: 2,
}
]
Expand All @@ -128,7 +129,7 @@ ruleTester.run(RULE_NAME, rule, {
`,
errors: [
{
messageId: 'default',
messageId: 'missing',
line: 2,
}
]
Expand All @@ -141,7 +142,7 @@ ruleTester.run(RULE_NAME, rule, {
`,
errors: [
{
messageId: 'default',
messageId: 'missing',
line: 2,
}
]
Expand All @@ -154,10 +155,29 @@ ruleTester.run(RULE_NAME, rule, {
`,
errors: [
{
messageId: 'default',
messageId: 'missing',
line: 2,
}
]
},
{
code: `
test('bar', () => {
// Act
const num1 = 1;
const num2 = 2;
// Arrange
const result = sum(num1, num2);
// Assert
expect(result).toBe(3);
});
`,
errors: [
{
messageId: 'wrong-order',
line: 2,
}
]
}
},
]
})
32 changes: 22 additions & 10 deletions src/rules/aaa-comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {createRule} from "../utils/create-rule.js";

export const RULE_NAME = 'aaa-comments'
export type Options = []
export type MessageIds = 'default';
export type MessageIds = 'missing' | 'wrong-order';

const testFunctionNames = ['it', 'test'];

Expand Down Expand Up @@ -33,6 +33,10 @@ const isTestCallExpression = (node: TSESTree.CallExpression) => {
return isEachCallExpression;
}

const findCommentByKeyword = (comments: TSESTree.Comment[], keyword: string) =>
comments.find(comment => comment.value.trim().toLowerCase().startsWith(keyword));


export default createRule<Options, MessageIds>({
create: (context) => ({
CallExpression(node) {
Expand All @@ -42,18 +46,25 @@ export default createRule<Options, MessageIds>({

const comments = context.getSourceCode().getCommentsInside(node);

const hasActComment = comments.some((comment) =>
comment.value.trim().toLowerCase().startsWith('act')
);
const arrangeComment = findCommentByKeyword(comments, 'arrange');
const actComment = findCommentByKeyword(comments, 'act');
const assertComment = findCommentByKeyword(comments, 'assert');

const hasAssertComment = comments.some((comment) =>
comment.value.trim().toLowerCase().startsWith('assert')
);
if (!arrangeComment || !actComment || !assertComment) {
context.report({
node,
messageId: 'missing',
});
return;
}

if (!(hasActComment && hasAssertComment)) {
if (
arrangeComment.loc.start.line > actComment.loc.start.line ||
actComment.loc.start.line > assertComment.loc.start.line
) {
context.report({
node,
messageId: 'default',
messageId: 'wrong-order',
});
}
},
Expand All @@ -65,7 +76,8 @@ export default createRule<Options, MessageIds>({
description: 'Enforce AAA comments',
},
messages: {
default: 'All tests should include Arrange, Act, and Assert comments.',
missing: 'Test should have arrange, act, and assert comments',
'wrong-order': 'Test should have arrange, act, and assert comments in the right order',
},
schema: [],
},
Expand Down

0 comments on commit 8ec6d73

Please sign in to comment.