Skip to content

Commit

Permalink
template-indent: Check Jest inline snapshots by default (#1637)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Dec 17, 2021
1 parent 0362c09 commit 64460e2
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 1 deletion.
9 changes: 8 additions & 1 deletion rules/template-indent.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,26 @@ const stripIndent = require('strip-indent');
const indentString = require('indent-string');
const esquery = require('esquery');
const {replaceTemplateElement} = require('./fix/index.js');
const {callExpressionSelector, methodCallSelector} = require('./selectors/index.js');

const MESSAGE_ID_IMPROPERLY_INDENTED_TEMPLATE = 'template-indent';
const messages = {
[MESSAGE_ID_IMPROPERLY_INDENTED_TEMPLATE]: 'Templates should be properly indented.',
};

const jestInlineSnapshotSelector = [
callExpressionSelector({name: 'expect', path: 'callee.object', argumentsLength: 1}),
methodCallSelector({method: 'toMatchInlineSnapshot', argumentsLength: 1}),
' > TemplateLiteral.arguments:first-child',
].join('');

/** @param {import('eslint').Rule.RuleContext} context */
const create = context => {
const sourceCode = context.getSourceCode();
const options = {
tags: ['outdent', 'dedent', 'gql', 'sql', 'html', 'styled'],
functions: ['dedent', 'stripIndent'],
selectors: [],
selectors: [jestInlineSnapshotSelector],
comments: ['HTML', 'indent'],
...context.options[0],
};
Expand Down
36 changes: 36 additions & 0 deletions test/snapshots/template-indent.mjs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Snapshot report for `test/template-indent.mjs`

The actual snapshot is saved in `template-indent.mjs.snap`.

Generated by [AVA](https://avajs.dev).

## Invalid #1
1 | expect(foo).toMatchInlineSnapshot(`
2 | one
3 | three
4 | `
5 | )

> Output
`␊
1 | expect(foo).toMatchInlineSnapshot(\`␊
2 | one␊
3 | three␊
4 | \`␊
5 | )␊
`

> Error 1/1
`␊
> 1 | expect(foo).toMatchInlineSnapshot(\`␊
| ^␊
> 2 | one␊
| ^^^^^^^␊
> 3 | three␊
| ^^^^^^^␊
> 4 | \`␊
| ^^^^^^^^ Templates should be properly indented.␊
5 | )␊
`
Binary file added test/snapshots/template-indent.mjs.snap
Binary file not shown.
35 changes: 35 additions & 0 deletions test/template-indent.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import outdent from 'outdent';
import stripIndent from 'strip-indent';
import {getTester} from './utils/test.mjs';

Expand Down Expand Up @@ -587,3 +588,37 @@ test({
`),
],
});

// `expect().toMatchInlineSnapshot()`
const INVALID_SNAPSHOT = `\`
one
three
\`
`;
test.snapshot({
valid: [
`expect(foo)[toMatchInlineSnapshot](${INVALID_SNAPSHOT})`,
`expect(foo).toMatchInlineSnapshot?.(${INVALID_SNAPSHOT})`,
`expect(foo)?.toMatchInlineSnapshot(${INVALID_SNAPSHOT})`,
`expect(foo).toMatchInlineSnapshot(${INVALID_SNAPSHOT}, extraArgument)`,
`expect(foo).toMatchInlineSnapshot(extraArgument, ${INVALID_SNAPSHOT})`,
'expect(foo).toMatchInlineSnapshot()',
`expect(foo, extraArgument).toMatchInlineSnapshot(${INVALID_SNAPSHOT})`,
`expect().toMatchInlineSnapshot(${INVALID_SNAPSHOT})`,
`expect(foo).notToMatchInlineSnapshot(${INVALID_SNAPSHOT})`,
`assert.expect(foo).toMatchInlineSnapshot(${INVALID_SNAPSHOT})`,
`expect.toMatchInlineSnapshot(${INVALID_SNAPSHOT})`,
`notExpect(foo).toMatchInlineSnapshot(${INVALID_SNAPSHOT})`,
`new expect(foo).toMatchInlineSnapshot(${INVALID_SNAPSHOT})`,
`new (expect(foo).toMatchInlineSnapshot)(${INVALID_SNAPSHOT})`,
outdent`
expect(foo).toMatchInlineSnapshot(\`
foo
bar
\`)
`,
],
invalid: [
`expect(foo).toMatchInlineSnapshot(${INVALID_SNAPSHOT})`,
],
});

0 comments on commit 64460e2

Please sign in to comment.