Skip to content

Commit

Permalink
feat(prefer-snapshot-hint): check nested scope for multiple snapshot …
Browse files Browse the repository at this point in the history
…matchers
  • Loading branch information
G-Rath committed Jan 24, 2022
1 parent 35aa923 commit 2533443
Show file tree
Hide file tree
Showing 3 changed files with 319 additions and 65 deletions.
25 changes: 16 additions & 9 deletions docs/rules/prefer-snapshot-hint.md
Expand Up @@ -108,18 +108,20 @@ describe('cli', () => {
### `'multi'` (default)

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

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

```js
const snapshotOutput = ({ stdout, stderr }) => {
expect(stdout).toMatchSnapshot();
expect(stderr).toMatchSnapshot();
};

describe('cli', () => {
describe('--version flag', () => {
it('prints the version', async () => {
const { stdout, stderr } = await runCli(['--version']);

expect(stdout).toMatchSnapshot();
expect(stderr).toMatchSnapshot();
snapshotOutput(await runCli(['--version']));
});
});

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

```js
const snapshotOutput = ({ stdout, stderr }, hints) => {
expect(stdout).toMatchSnapshot({}, `stdout: ${hints.stdout}`);
expect(stderr).toMatchSnapshot({}, `stderr: ${hints.stderr}`);
};

describe('cli', () => {
describe('--version flag', () => {
it('prints the version', async () => {
const { stdout, stderr } = await runCli(['--version']);

expect(stdout).toMatchSnapshot('stdout: version string');
expect(stderr).toMatchSnapshot('stderr: empty');
snapshotOutput(await runCli(['--version']), {
stdout: 'version string',
stderr: 'empty',
});
});
});

Expand Down
262 changes: 262 additions & 0 deletions src/rules/__tests__/prefer-snapshot-hint.test.ts
Expand Up @@ -157,6 +157,21 @@ ruleTester.run('prefer-snapshot-hint (always)', rule, {
},
],
},
{
code: dedent`
it('is true', () => {
{ expect(1).toMatchSnapshot(); }
});
`,
options: ['always'],
errors: [
{
messageId: 'missingHint',
column: 15,
line: 2,
},
],
},
],
});

Expand Down Expand Up @@ -260,6 +275,57 @@ ruleTester.run('prefer-snapshot-hint (multi)', rule, {
`,
options: ['multi'],
},
{
code: dedent`
const myReusableTestBody = (value, snapshotHint) => {
const innerFn = anotherValue => {
expect(anotherValue).toMatchSnapshot();
expect(value).toBe(1);
};
expect(value).toBe(1);
};
it('my test', () => {
expect(1).toMatchSnapshot();
});
`,
options: ['multi'],
},
{
code: dedent`
const myReusableTestBody = (value, snapshotHint) => {
const innerFn = anotherValue => {
expect(value).toBe(1);
};
expect(value).toBe(1);
expect(anotherValue).toMatchSnapshot();
};
it('my test', () => {
expect(1).toMatchSnapshot();
});
`,
options: ['multi'],
},
{
code: dedent`
const myReusableTestBody = (value, snapshotHint) => {
const innerFn = anotherValue => {
expect(anotherValue).toMatchSnapshot();
expect(value).toBe(1);
};
expect(value).toBe(1);
};
expect(1).toMatchSnapshot();
`,
options: ['multi'],
},
],
invalid: [
{
Expand Down Expand Up @@ -346,6 +412,50 @@ ruleTester.run('prefer-snapshot-hint (multi)', rule, {
},
],
},
{
code: dedent`
it('is true', () => {
expect(1).toMatchSnapshot({});
{
expect(2).toMatchSnapshot({});
}
});
`,
options: ['multi'],
errors: [
{
messageId: 'missingHint',
column: 13,
line: 2,
},
{
messageId: 'missingHint',
column: 15,
line: 4,
},
],
},
{
code: dedent`
it('is true', () => {
{ expect(1).toMatchSnapshot(); }
{ expect(2).toMatchSnapshot(); }
});
`,
options: ['multi'],
errors: [
{
messageId: 'missingHint',
column: 15,
line: 2,
},
{
messageId: 'missingHint',
column: 15,
line: 3,
},
],
},
{
code: dedent`
it('is true', () => {
Expand Down Expand Up @@ -460,5 +570,157 @@ ruleTester.run('prefer-snapshot-hint (multi)', rule, {
},
],
},
{
code: dedent`
const myReusableTestBody = (value, snapshotHint) => {
expect(value).toMatchSnapshot();
const innerFn = anotherValue => {
expect(anotherValue).toMatchSnapshot();
};
expect(value).toBe(1);
expect(value + 1).toMatchSnapshot(null);
expect(value + 2).toThrowErrorMatchingSnapshot(snapshotHint);
};
`,
options: ['multi'],
errors: [
{
messageId: 'missingHint',
column: 17,
line: 2,
},
{
messageId: 'missingHint',
column: 26,
line: 5,
},
{
messageId: 'missingHint',
column: 21,
line: 9,
},
],
},
{
code: dedent`
const myReusableTestBody = (value, snapshotHint) => {
expect(value).toMatchSnapshot();
const innerFn = anotherValue => {
expect(anotherValue).toMatchSnapshot();
expect(value).toBe(1);
expect(value + 1).toMatchSnapshot(null);
expect(value + 2).toMatchSnapshot(null, snapshotHint);
};
};
`,
options: ['multi'],
errors: [
{
messageId: 'missingHint',
column: 17,
line: 2,
},
{
messageId: 'missingHint',
column: 26,
line: 5,
},
{
messageId: 'missingHint',
column: 23,
line: 8,
},
],
},
{
code: dedent`
const myReusableTestBody = (value, snapshotHint) => {
const innerFn = anotherValue => {
expect(anotherValue).toMatchSnapshot();
expect(value).toBe(1);
expect(value + 1).toMatchSnapshot(null);
expect(value + 2).toMatchSnapshot(null, snapshotHint);
};
expect(value).toThrowErrorMatchingSnapshot();
};
`,
options: ['multi'],
errors: [
{
messageId: 'missingHint',
column: 26,
line: 3,
},
{
messageId: 'missingHint',
column: 23,
line: 6,
},
{
messageId: 'missingHint',
column: 17,
line: 10,
},
],
},
{
code: dedent`
const myReusableTestBody = (value, snapshotHint) => {
const innerFn = anotherValue => {
expect(anotherValue).toMatchSnapshot();
expect(value).toBe(1);
};
expect(value).toMatchSnapshot();
};
it('my test', () => {
expect(1).toMatchSnapshot();
});
`,
options: ['multi'],
errors: [
{
messageId: 'missingHint',
column: 26,
line: 3,
},
{
messageId: 'missingHint',
column: 17,
line: 8,
},
],
},
{
code: dedent`
const myReusableTestBody = value => {
expect(value).toMatchSnapshot();
};
expect(1).toMatchSnapshot();
expect(1).toThrowErrorMatchingSnapshot();
`,
options: ['multi'],
errors: [
{
messageId: 'missingHint',
column: 11,
line: 5,
},
{
messageId: 'missingHint',
column: 11,
line: 6,
},
],
},
],
});

0 comments on commit 2533443

Please sign in to comment.