Skip to content

Commit

Permalink
fix(max-expect): reset expect counter when entering and exiting tes…
Browse files Browse the repository at this point in the history
…t function calls (#1206)
  • Loading branch information
G-Rath authored Aug 21, 2022
1 parent 1f0d87f commit 3908ab8
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 3 deletions.
64 changes: 64 additions & 0 deletions src/rules/__tests__/max-expects.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,70 @@ ruleTester.run('max-expects', rule, {
expect(true).toBeDefined();
});
`,
dedent`
function myHelper() {
expect(true).toBeDefined();
expect(true).toBeDefined();
expect(true).toBeDefined();
};
test('should pass', () => {
expect(true).toBeDefined();
expect(true).toBeDefined();
expect(true).toBeDefined();
});
`,
dedent`
function myHelper1() {
expect(true).toBeDefined();
expect(true).toBeDefined();
expect(true).toBeDefined();
};
test('should pass', () => {
expect(true).toBeDefined();
expect(true).toBeDefined();
expect(true).toBeDefined();
});
function myHelper2() {
expect(true).toBeDefined();
expect(true).toBeDefined();
expect(true).toBeDefined();
};
`,
dedent`
test('should pass', () => {
expect(true).toBeDefined();
expect(true).toBeDefined();
expect(true).toBeDefined();
});
function myHelper() {
expect(true).toBeDefined();
expect(true).toBeDefined();
expect(true).toBeDefined();
};
`,
dedent`
const myHelper1 = () => {
expect(true).toBeDefined();
expect(true).toBeDefined();
expect(true).toBeDefined();
};
test('should pass', function() {
expect(true).toBeDefined();
expect(true).toBeDefined();
expect(true).toBeDefined();
});
const myHelper2 = function() {
expect(true).toBeDefined();
expect(true).toBeDefined();
expect(true).toBeDefined();
};
`,
{
code: dedent`
test('should pass', () => {
Expand Down
8 changes: 5 additions & 3 deletions src/rules/max-expects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default createRule({
create(context, [{ max }]) {
let count = 0;

const onFunctionExpressionEnter = (node: FunctionExpression) => {
const maybeResetCount = (node: FunctionExpression) => {
const isTestFn =
node.parent?.type !== AST_NODE_TYPES.CallExpression ||
isTypeOfJestFnCall(node.parent, context, ['test']);
Expand All @@ -47,8 +47,10 @@ export default createRule({
};

return {
FunctionExpression: onFunctionExpressionEnter,
ArrowFunctionExpression: onFunctionExpressionEnter,
FunctionExpression: maybeResetCount,
'FunctionExpression:exit': maybeResetCount,
ArrowFunctionExpression: maybeResetCount,
'ArrowFunctionExpression:exit': maybeResetCount,
CallExpression(node) {
const jestFnCall = parseJestFnCall(node, context);

Expand Down

0 comments on commit 3908ab8

Please sign in to comment.