Skip to content

Commit

Permalink
fix(max-expects): properly reset counter when exiting a test case (#1550
Browse files Browse the repository at this point in the history
)
  • Loading branch information
G-Rath committed Apr 6, 2024
1 parent 6aaabc4 commit b4b7cbc
Show file tree
Hide file tree
Showing 2 changed files with 233 additions and 0 deletions.
229 changes: 229 additions & 0 deletions src/rules/__tests__/max-expects.test.ts
Expand Up @@ -119,6 +119,39 @@ ruleTester.run('max-expects', rule, {
expect(true).toBeDefined();
});
`,
dedent`
test('should not pass', () => {
const checkValue = (value) => {
expect(value).toBeDefined();
expect(value).toBeDefined();
};
checkValue(true);
});
test('should not pass', () => {
expect(true).toBeDefined();
expect(true).toBeDefined();
expect(true).toBeDefined();
});
`,
dedent`
test('should not pass', done => {
emitter.on('event', value => {
expect(value).toBeDefined();
expect(value).toBeDefined();
expect(value).toBeDefined();
expect(value).toBeDefined();
done();
});
});
test('should not pass', () => {
expect(true).toBeDefined();
expect(true).toBeDefined();
expect(true).toBeDefined();
expect(true).toBeDefined();
});
`,
dedent`
function myHelper() {
expect(true).toBeDefined();
Expand Down Expand Up @@ -204,6 +237,24 @@ ruleTester.run('max-expects', rule, {
},
],
},
{
code: dedent`
describe('given decimal places', () => {
it("test 1", fakeAsync(() => {
expect(true).toBeTrue();
expect(true).toBeTrue();
expect(true).toBeTrue();
}))
it("test 2", fakeAsync(() => {
expect(true).toBeTrue();
expect(true).toBeTrue();
expect(true).toBeTrue();
}))
})
`,
options: [{ max: 5 }],
},
],
invalid: [
{
Expand Down Expand Up @@ -314,6 +365,184 @@ ruleTester.run('max-expects', rule, {
},
],
},
{
code: dedent`
test('should not pass', () => {
const checkValue = (value) => {
expect(value).toBeDefined();
expect(value).toBeDefined();
};
checkValue(true);
});
test('should not pass', () => {
expect(true).toBeDefined();
expect(true).toBeDefined();
expect(true).toBeDefined();
});
`,
options: [{ max: 1 }],
errors: [
{
messageId: 'exceededMaxAssertion',
line: 4,
column: 5,
},
{
messageId: 'exceededMaxAssertion',
line: 11,
column: 3,
},
{
messageId: 'exceededMaxAssertion',
line: 12,
column: 3,
},
],
},
{
code: dedent`
test('should not pass', () => {
const checkValue = (value) => {
expect(value).toBeDefined();
expect(value).toBeDefined();
};
checkValue(true);
});
test('should not pass', () => {
expect(true).toBeDefined();
expect(true).toBeDefined();
expect(true).toBeDefined();
});
`,
options: [{ max: 2 }],
errors: [
{
messageId: 'exceededMaxAssertion',
line: 12,
column: 3,
},
],
},
{
code: dedent`
test('should not pass', () => {
const checkValue = (value) => {
expect(value).toBeDefined();
expect(value).toBeDefined();
};
expect(value).toBeDefined();
checkValue(true);
});
test('should not pass', () => {
expect(true).toBeDefined();
expect(true).toBeDefined();
expect(true).toBeDefined();
});
`,
options: [{ max: 2 }],
errors: [
{
messageId: 'exceededMaxAssertion',
line: 13,
column: 3,
},
],
},
{
code: dedent`
test('should not pass', done => {
emitter.on('event', value => {
expect(value).toBeDefined();
expect(value).toBeDefined();
done();
});
});
test('should not pass', () => {
expect(true).toBeDefined();
expect(true).toBeDefined();
expect(true).toBeDefined();
});
`,
options: [{ max: 1 }],
errors: [
{
messageId: 'exceededMaxAssertion',
line: 4,
column: 5,
},
{
messageId: 'exceededMaxAssertion',
line: 11,
column: 3,
},
{
messageId: 'exceededMaxAssertion',
line: 12,
column: 3,
},
],
},
{
code: dedent`
test('should not pass', done => {
emitter.on('event', value => {
expect(value).toBeDefined();
expect(value).toBeDefined();
done();
});
});
test('should not pass', () => {
expect(true).toBeDefined();
expect(true).toBeDefined();
expect(true).toBeDefined();
});
`,
options: [{ max: 2 }],
errors: [
{
messageId: 'exceededMaxAssertion',
line: 12,
column: 3,
},
],
},
{
code: dedent`
describe('given decimal places', () => {
it("test 1", fakeAsync(() => {
expect(true).toBeTrue();
expect(true).toBeTrue();
expect(true).toBeTrue();
}))
it("test 2", fakeAsync(() => {
expect(true).toBeTrue();
expect(true).toBeTrue();
expect(true).toBeTrue();
expect(true).toBeTrue();
expect(true).toBeTrue();
}))
})
`,
options: [{ max: 3 }],
errors: [
{
messageId: 'exceededMaxAssertion',
line: 12,
column: 5,
},
{
messageId: 'exceededMaxAssertion',
line: 13,
column: 5,
},
],
},
{
code: dedent`
describe('test', () => {
Expand Down
4 changes: 4 additions & 0 deletions src/rules/max-expects.ts
Expand Up @@ -52,6 +52,10 @@ export default createRule({
CallExpression(node) {
const jestFnCall = parseJestFnCall(node, context);

if (jestFnCall?.type === 'test') {
count = 0;
}

if (
jestFnCall?.type !== 'expect' ||
jestFnCall.head.node.parent?.type === AST_NODE_TYPES.MemberExpression
Expand Down

0 comments on commit b4b7cbc

Please sign in to comment.