Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Apr 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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