Skip to content

Commit

Permalink
fix(prefer-expect-assertions): report expects in loops in functions i…
Browse files Browse the repository at this point in the history
…n tests
  • Loading branch information
G-Rath committed Jan 15, 2022
1 parent 89145e3 commit ab99a4e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
35 changes: 21 additions & 14 deletions src/rules/__tests__/prefer-expect-assertions.test.ts
Expand Up @@ -374,20 +374,6 @@ ruleTester.run('prefer-expect-assertions (loops)', rule, {
`,
options: [{ onlyFunctionsWithExpectInLoop: true }],
},
{
code: dedent`
it('returns numbers that are greater than two', function () {
const expectNumbersToBeGreaterThan = (numbers, value) => {
for (let number of numbers) {
expect(number).toBeGreaterThan(value);
}
};
expectNumbersToBeGreaterThan(getNumbers(), 2);
});
`,
options: [{ onlyFunctionsWithExpectInLoop: true }],
},
{
code: dedent`
it("returns numbers that are greater than five", function () {
Expand Down Expand Up @@ -431,6 +417,27 @@ ruleTester.run('prefer-expect-assertions (loops)', rule, {
},
],
},
{
code: dedent`
it('returns numbers that are greater than two', function () {
const expectNumbersToBeGreaterThan = (numbers, value) => {
for (let number of numbers) {
expect(number).toBeGreaterThan(value);
}
};
expectNumbersToBeGreaterThan(getNumbers(), 2);
});
`,
options: [{ onlyFunctionsWithExpectInLoop: true }],
errors: [
{
messageId: 'haveExpectAssertions',
column: 1,
line: 1,
},
],
},
{
code: dedent`
it("only returns numbers that are greater than seven", function () {
Expand Down
9 changes: 1 addition & 8 deletions src/rules/prefer-expect-assertions.ts
Expand Up @@ -105,7 +105,6 @@ export default createRule<[RuleOptions], MessageIds>({
},
],
create(context, [options]) {
let expressionDepth = 0;
let hasExpectInLoop = false;
let inTestCaseCall = false;
let inForLoop = false;
Expand Down Expand Up @@ -133,16 +132,10 @@ export default createRule<[RuleOptions], MessageIds>({
return false;
};

const enterExpression = () => inTestCaseCall && expressionDepth++;
const exitExpression = () => inTestCaseCall && expressionDepth--;
const enterForLoop = () => (inForLoop = true);
const exitForLoop = () => (inForLoop = false);

return {
FunctionExpression: enterExpression,
'FunctionExpression:exit': exitExpression,
ArrowFunctionExpression: enterExpression,
'ArrowFunctionExpression:exit': exitExpression,
ForStatement: enterForLoop,
'ForStatement:exit': exitForLoop,
ForInStatement: enterForLoop,
Expand All @@ -156,7 +149,7 @@ export default createRule<[RuleOptions], MessageIds>({
return;
}

if (isExpectCall(node) && expressionDepth === 1 && inForLoop) {
if (isExpectCall(node) && inTestCaseCall && inForLoop) {
hasExpectInLoop = true;
}
},
Expand Down

0 comments on commit ab99a4e

Please sign in to comment.