Skip to content

Commit 41c7c39

Browse files
committed
feat(prefer-snapshot-hint): check nested scope for multiple snapshot matchers
1 parent 298f9e4 commit 41c7c39

File tree

3 files changed

+319
-65
lines changed

3 files changed

+319
-65
lines changed

docs/rules/prefer-snapshot-hint.md

+16-9
Original file line numberDiff line numberDiff line change
@@ -108,18 +108,20 @@ describe('cli', () => {
108108
### `'multi'` (default)
109109

110110
Require a hint to be provided when there are multiple external snapshot matchers
111-
within the same test body.
111+
within the scope (meaning it includes nested calls).
112112

113113
Examples of **incorrect** code for the `'multi'` option:
114114

115115
```js
116+
const snapshotOutput = ({ stdout, stderr }) => {
117+
expect(stdout).toMatchSnapshot();
118+
expect(stderr).toMatchSnapshot();
119+
};
120+
116121
describe('cli', () => {
117122
describe('--version flag', () => {
118123
it('prints the version', async () => {
119-
const { stdout, stderr } = await runCli(['--version']);
120-
121-
expect(stdout).toMatchSnapshot();
122-
expect(stderr).toMatchSnapshot();
124+
snapshotOutput(await runCli(['--version']));
123125
});
124126
});
125127

@@ -146,13 +148,18 @@ describe('cli', () => {
146148
Examples of **correct** code for the `'multi'` option:
147149

148150
```js
151+
const snapshotOutput = ({ stdout, stderr }, hints) => {
152+
expect(stdout).toMatchSnapshot({}, `stdout: ${hints.stdout}`);
153+
expect(stderr).toMatchSnapshot({}, `stderr: ${hints.stderr}`);
154+
};
155+
149156
describe('cli', () => {
150157
describe('--version flag', () => {
151158
it('prints the version', async () => {
152-
const { stdout, stderr } = await runCli(['--version']);
153-
154-
expect(stdout).toMatchSnapshot('stdout: version string');
155-
expect(stderr).toMatchSnapshot('stderr: empty');
159+
snapshotOutput(await runCli(['--version']), {
160+
stdout: 'version string',
161+
stderr: 'empty',
162+
});
156163
});
157164
});
158165

src/rules/__tests__/prefer-snapshot-hint.test.ts

+262
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,21 @@ ruleTester.run('prefer-snapshot-hint (always)', rule, {
157157
},
158158
],
159159
},
160+
{
161+
code: dedent`
162+
it('is true', () => {
163+
{ expect(1).toMatchSnapshot(); }
164+
});
165+
`,
166+
options: ['always'],
167+
errors: [
168+
{
169+
messageId: 'missingHint',
170+
column: 15,
171+
line: 2,
172+
},
173+
],
174+
},
160175
],
161176
});
162177

@@ -260,6 +275,57 @@ ruleTester.run('prefer-snapshot-hint (multi)', rule, {
260275
`,
261276
options: ['multi'],
262277
},
278+
{
279+
code: dedent`
280+
const myReusableTestBody = (value, snapshotHint) => {
281+
const innerFn = anotherValue => {
282+
expect(anotherValue).toMatchSnapshot();
283+
284+
expect(value).toBe(1);
285+
};
286+
287+
expect(value).toBe(1);
288+
};
289+
290+
it('my test', () => {
291+
expect(1).toMatchSnapshot();
292+
});
293+
`,
294+
options: ['multi'],
295+
},
296+
{
297+
code: dedent`
298+
const myReusableTestBody = (value, snapshotHint) => {
299+
const innerFn = anotherValue => {
300+
expect(value).toBe(1);
301+
};
302+
303+
expect(value).toBe(1);
304+
expect(anotherValue).toMatchSnapshot();
305+
};
306+
307+
it('my test', () => {
308+
expect(1).toMatchSnapshot();
309+
});
310+
`,
311+
options: ['multi'],
312+
},
313+
{
314+
code: dedent`
315+
const myReusableTestBody = (value, snapshotHint) => {
316+
const innerFn = anotherValue => {
317+
expect(anotherValue).toMatchSnapshot();
318+
319+
expect(value).toBe(1);
320+
};
321+
322+
expect(value).toBe(1);
323+
};
324+
325+
expect(1).toMatchSnapshot();
326+
`,
327+
options: ['multi'],
328+
},
263329
],
264330
invalid: [
265331
{
@@ -346,6 +412,50 @@ ruleTester.run('prefer-snapshot-hint (multi)', rule, {
346412
},
347413
],
348414
},
415+
{
416+
code: dedent`
417+
it('is true', () => {
418+
expect(1).toMatchSnapshot({});
419+
{
420+
expect(2).toMatchSnapshot({});
421+
}
422+
});
423+
`,
424+
options: ['multi'],
425+
errors: [
426+
{
427+
messageId: 'missingHint',
428+
column: 13,
429+
line: 2,
430+
},
431+
{
432+
messageId: 'missingHint',
433+
column: 15,
434+
line: 4,
435+
},
436+
],
437+
},
438+
{
439+
code: dedent`
440+
it('is true', () => {
441+
{ expect(1).toMatchSnapshot(); }
442+
{ expect(2).toMatchSnapshot(); }
443+
});
444+
`,
445+
options: ['multi'],
446+
errors: [
447+
{
448+
messageId: 'missingHint',
449+
column: 15,
450+
line: 2,
451+
},
452+
{
453+
messageId: 'missingHint',
454+
column: 15,
455+
line: 3,
456+
},
457+
],
458+
},
349459
{
350460
code: dedent`
351461
it('is true', () => {
@@ -460,5 +570,157 @@ ruleTester.run('prefer-snapshot-hint (multi)', rule, {
460570
},
461571
],
462572
},
573+
{
574+
code: dedent`
575+
const myReusableTestBody = (value, snapshotHint) => {
576+
expect(value).toMatchSnapshot();
577+
578+
const innerFn = anotherValue => {
579+
expect(anotherValue).toMatchSnapshot();
580+
};
581+
582+
expect(value).toBe(1);
583+
expect(value + 1).toMatchSnapshot(null);
584+
expect(value + 2).toThrowErrorMatchingSnapshot(snapshotHint);
585+
};
586+
`,
587+
options: ['multi'],
588+
errors: [
589+
{
590+
messageId: 'missingHint',
591+
column: 17,
592+
line: 2,
593+
},
594+
{
595+
messageId: 'missingHint',
596+
column: 26,
597+
line: 5,
598+
},
599+
{
600+
messageId: 'missingHint',
601+
column: 21,
602+
line: 9,
603+
},
604+
],
605+
},
606+
{
607+
code: dedent`
608+
const myReusableTestBody = (value, snapshotHint) => {
609+
expect(value).toMatchSnapshot();
610+
611+
const innerFn = anotherValue => {
612+
expect(anotherValue).toMatchSnapshot();
613+
614+
expect(value).toBe(1);
615+
expect(value + 1).toMatchSnapshot(null);
616+
expect(value + 2).toMatchSnapshot(null, snapshotHint);
617+
};
618+
};
619+
`,
620+
options: ['multi'],
621+
errors: [
622+
{
623+
messageId: 'missingHint',
624+
column: 17,
625+
line: 2,
626+
},
627+
{
628+
messageId: 'missingHint',
629+
column: 26,
630+
line: 5,
631+
},
632+
{
633+
messageId: 'missingHint',
634+
column: 23,
635+
line: 8,
636+
},
637+
],
638+
},
639+
{
640+
code: dedent`
641+
const myReusableTestBody = (value, snapshotHint) => {
642+
const innerFn = anotherValue => {
643+
expect(anotherValue).toMatchSnapshot();
644+
645+
expect(value).toBe(1);
646+
expect(value + 1).toMatchSnapshot(null);
647+
expect(value + 2).toMatchSnapshot(null, snapshotHint);
648+
};
649+
650+
expect(value).toThrowErrorMatchingSnapshot();
651+
};
652+
`,
653+
options: ['multi'],
654+
errors: [
655+
{
656+
messageId: 'missingHint',
657+
column: 26,
658+
line: 3,
659+
},
660+
{
661+
messageId: 'missingHint',
662+
column: 23,
663+
line: 6,
664+
},
665+
{
666+
messageId: 'missingHint',
667+
column: 17,
668+
line: 10,
669+
},
670+
],
671+
},
672+
{
673+
code: dedent`
674+
const myReusableTestBody = (value, snapshotHint) => {
675+
const innerFn = anotherValue => {
676+
expect(anotherValue).toMatchSnapshot();
677+
678+
expect(value).toBe(1);
679+
};
680+
681+
expect(value).toMatchSnapshot();
682+
};
683+
684+
it('my test', () => {
685+
expect(1).toMatchSnapshot();
686+
});
687+
`,
688+
options: ['multi'],
689+
errors: [
690+
{
691+
messageId: 'missingHint',
692+
column: 26,
693+
line: 3,
694+
},
695+
{
696+
messageId: 'missingHint',
697+
column: 17,
698+
line: 8,
699+
},
700+
],
701+
},
702+
{
703+
code: dedent`
704+
const myReusableTestBody = value => {
705+
expect(value).toMatchSnapshot();
706+
};
707+
708+
expect(1).toMatchSnapshot();
709+
expect(1).toThrowErrorMatchingSnapshot();
710+
`,
711+
options: ['multi'],
712+
errors: [
713+
{
714+
messageId: 'missingHint',
715+
column: 11,
716+
line: 5,
717+
},
718+
{
719+
messageId: 'missingHint',
720+
column: 11,
721+
line: 6,
722+
},
723+
],
724+
},
463725
],
464726
});

0 commit comments

Comments
 (0)