Skip to content

Commit

Permalink
fix(prefer-snapshot-hint): don't report multi snapshots in different …
Browse files Browse the repository at this point in the history
…tests within the same describe (#1078)

* test(prefer-snapshot-hint): add cases for new bug

* fix(prefer-snapshot-hint): don't report multi snapshots in different tests within the same describe

* chore: ignore line in coverage
  • Loading branch information
G-Rath authored Apr 8, 2022
1 parent 9d7fb3c commit 98e5166
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 1 deletion.
127 changes: 126 additions & 1 deletion src/rules/__tests__/prefer-snapshot-hint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,14 +257,30 @@ ruleTester.run('prefer-snapshot-hint (multi)', rule, {
`,
options: ['multi'],
},
{
code: dedent`
describe('my tests', () => {
it('is true', () => {
expect(1).toMatchSnapshot('this is a hint, all by itself');
});
it('is false', () => {
expect(2).toMatchSnapshot('this is a hint');
expect(2).toMatchSnapshot('and so is this');
});
});
`,
options: ['multi'],
},
{
code: dedent`
it('is true', () => {
expect(1).toMatchSnapshot();
});
it('is false', () => {
expect(2).toMatchSnapshot();
expect(2).toMatchSnapshot('this is a hint');
expect(2).toMatchSnapshot('and so is this');
});
`,
options: ['multi'],
Expand Down Expand Up @@ -311,6 +327,18 @@ ruleTester.run('prefer-snapshot-hint (multi)', rule, {
`,
options: ['multi'],
},
{
code: dedent`
it('is true', () => {
expect(1).toMatchSnapshot();
});
it('is false', () => {
expect(1).toMatchSnapshot();
});
`,
options: ['multi'],
},
{
code: dedent`
const myReusableTestBody = (value, snapshotHint) => {
Expand Down Expand Up @@ -606,6 +634,103 @@ ruleTester.run('prefer-snapshot-hint (multi)', rule, {
},
],
},
{
code: dedent`
describe('my tests', () => {
it('is true', () => {
expect(1).toMatchSnapshot();
});
it('is false', () => {
expect(2).toMatchSnapshot();
expect(2).toMatchSnapshot();
});
});
`,
options: ['multi'],
errors: [
{
messageId: 'missingHint',
column: 15,
line: 7,
},
{
messageId: 'missingHint',
column: 15,
line: 8,
},
],
},
{
code: dedent`
describe('my tests', () => {
it('is true', () => {
expect(1).toMatchSnapshot();
});
it('is false', () => {
expect(2).toMatchSnapshot();
expect(2).toMatchSnapshot('hello world');
});
});
`,
options: ['multi'],
errors: [
{
messageId: 'missingHint',
column: 15,
line: 7,
},
],
},
{
code: dedent`
describe('my tests', () => {
describe('more tests', () => {
it('is true', () => {
expect(1).toMatchSnapshot();
});
});
it('is false', () => {
expect(2).toMatchSnapshot();
expect(2).toMatchSnapshot('hello world');
});
});
`,
options: ['multi'],
errors: [
{
messageId: 'missingHint',
column: 15,
line: 9,
},
],
},
{
code: dedent`
describe('my tests', () => {
it('is true', () => {
expect(1).toMatchSnapshot();
});
describe('more tests', () => {
it('is false', () => {
expect(2).toMatchSnapshot();
expect(2).toMatchSnapshot('hello world');
});
});
});
`,
options: ['multi'],
errors: [
{
messageId: 'missingHint',
column: 17,
line: 8,
},
],
},
{
code: dedent`
const myReusableTestBody = (value, snapshotHint) => {
Expand Down
14 changes: 14 additions & 0 deletions src/rules/prefer-snapshot-hint.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import {
ParsedExpectMatcher,
createRule,
isDescribeCall,
isExpectCall,
isStringNode,
isTestCaseCall,
parseExpectCall,
} from './utils';

Expand Down Expand Up @@ -60,6 +62,7 @@ export default createRule<[('always' | 'multi')?], keyof typeof messages>({
defaultOptions: ['multi'],
create(context, [mode]) {
const snapshotMatchers: ParsedExpectMatcher[] = [];
const depths: number[] = [];
let expressionDepth = 0;

const reportSnapshotMatchersWithoutHints = () => {
Expand Down Expand Up @@ -103,7 +106,18 @@ export default createRule<[('always' | 'multi')?], keyof typeof messages>({
'FunctionExpression:exit': exitExpression,
ArrowFunctionExpression: enterExpression,
'ArrowFunctionExpression:exit': exitExpression,
'CallExpression:exit'(node) {
if (isDescribeCall(node) || isTestCaseCall(node)) {
/* istanbul ignore next */
expressionDepth = depths.pop() ?? 0;
}
},
CallExpression(node) {
if (isDescribeCall(node) || isTestCaseCall(node)) {
depths.push(expressionDepth);
expressionDepth = 0;
}

if (!isExpectCall(node)) {
return;
}
Expand Down

0 comments on commit 98e5166

Please sign in to comment.