Skip to content

Commit 98e5166

Browse files
authored
fix(prefer-snapshot-hint): don't report multi snapshots in different 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
1 parent 9d7fb3c commit 98e5166

File tree

2 files changed

+140
-1
lines changed

2 files changed

+140
-1
lines changed

Diff for: src/rules/__tests__/prefer-snapshot-hint.test.ts

+126-1
Original file line numberDiff line numberDiff line change
@@ -257,14 +257,30 @@ ruleTester.run('prefer-snapshot-hint (multi)', rule, {
257257
`,
258258
options: ['multi'],
259259
},
260+
{
261+
code: dedent`
262+
describe('my tests', () => {
263+
it('is true', () => {
264+
expect(1).toMatchSnapshot('this is a hint, all by itself');
265+
});
266+
267+
it('is false', () => {
268+
expect(2).toMatchSnapshot('this is a hint');
269+
expect(2).toMatchSnapshot('and so is this');
270+
});
271+
});
272+
`,
273+
options: ['multi'],
274+
},
260275
{
261276
code: dedent`
262277
it('is true', () => {
263278
expect(1).toMatchSnapshot();
264279
});
265280
266281
it('is false', () => {
267-
expect(2).toMatchSnapshot();
282+
expect(2).toMatchSnapshot('this is a hint');
283+
expect(2).toMatchSnapshot('and so is this');
268284
});
269285
`,
270286
options: ['multi'],
@@ -311,6 +327,18 @@ ruleTester.run('prefer-snapshot-hint (multi)', rule, {
311327
`,
312328
options: ['multi'],
313329
},
330+
{
331+
code: dedent`
332+
it('is true', () => {
333+
expect(1).toMatchSnapshot();
334+
});
335+
336+
it('is false', () => {
337+
expect(1).toMatchSnapshot();
338+
});
339+
`,
340+
options: ['multi'],
341+
},
314342
{
315343
code: dedent`
316344
const myReusableTestBody = (value, snapshotHint) => {
@@ -606,6 +634,103 @@ ruleTester.run('prefer-snapshot-hint (multi)', rule, {
606634
},
607635
],
608636
},
637+
{
638+
code: dedent`
639+
describe('my tests', () => {
640+
it('is true', () => {
641+
expect(1).toMatchSnapshot();
642+
});
643+
644+
it('is false', () => {
645+
expect(2).toMatchSnapshot();
646+
expect(2).toMatchSnapshot();
647+
});
648+
});
649+
`,
650+
options: ['multi'],
651+
errors: [
652+
{
653+
messageId: 'missingHint',
654+
column: 15,
655+
line: 7,
656+
},
657+
{
658+
messageId: 'missingHint',
659+
column: 15,
660+
line: 8,
661+
},
662+
],
663+
},
664+
{
665+
code: dedent`
666+
describe('my tests', () => {
667+
it('is true', () => {
668+
expect(1).toMatchSnapshot();
669+
});
670+
671+
it('is false', () => {
672+
expect(2).toMatchSnapshot();
673+
expect(2).toMatchSnapshot('hello world');
674+
});
675+
});
676+
`,
677+
options: ['multi'],
678+
errors: [
679+
{
680+
messageId: 'missingHint',
681+
column: 15,
682+
line: 7,
683+
},
684+
],
685+
},
686+
{
687+
code: dedent`
688+
describe('my tests', () => {
689+
describe('more tests', () => {
690+
it('is true', () => {
691+
expect(1).toMatchSnapshot();
692+
});
693+
});
694+
695+
it('is false', () => {
696+
expect(2).toMatchSnapshot();
697+
expect(2).toMatchSnapshot('hello world');
698+
});
699+
});
700+
`,
701+
options: ['multi'],
702+
errors: [
703+
{
704+
messageId: 'missingHint',
705+
column: 15,
706+
line: 9,
707+
},
708+
],
709+
},
710+
{
711+
code: dedent`
712+
describe('my tests', () => {
713+
it('is true', () => {
714+
expect(1).toMatchSnapshot();
715+
});
716+
717+
describe('more tests', () => {
718+
it('is false', () => {
719+
expect(2).toMatchSnapshot();
720+
expect(2).toMatchSnapshot('hello world');
721+
});
722+
});
723+
});
724+
`,
725+
options: ['multi'],
726+
errors: [
727+
{
728+
messageId: 'missingHint',
729+
column: 17,
730+
line: 8,
731+
},
732+
],
733+
},
609734
{
610735
code: dedent`
611736
const myReusableTestBody = (value, snapshotHint) => {

Diff for: src/rules/prefer-snapshot-hint.ts

+14
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import {
22
ParsedExpectMatcher,
33
createRule,
4+
isDescribeCall,
45
isExpectCall,
56
isStringNode,
7+
isTestCaseCall,
68
parseExpectCall,
79
} from './utils';
810

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

6568
const reportSnapshotMatchersWithoutHints = () => {
@@ -103,7 +106,18 @@ export default createRule<[('always' | 'multi')?], keyof typeof messages>({
103106
'FunctionExpression:exit': exitExpression,
104107
ArrowFunctionExpression: enterExpression,
105108
'ArrowFunctionExpression:exit': exitExpression,
109+
'CallExpression:exit'(node) {
110+
if (isDescribeCall(node) || isTestCaseCall(node)) {
111+
/* istanbul ignore next */
112+
expressionDepth = depths.pop() ?? 0;
113+
}
114+
},
106115
CallExpression(node) {
116+
if (isDescribeCall(node) || isTestCaseCall(node)) {
117+
depths.push(expressionDepth);
118+
expressionDepth = 0;
119+
}
120+
107121
if (!isExpectCall(node)) {
108122
return;
109123
}

0 commit comments

Comments
 (0)