-
Notifications
You must be signed in to change notification settings - Fork 236
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rules): add no-interpolation-inline-snapshot rule
- Loading branch information
Showing
5 changed files
with
144 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# No string interpolation inside inline snapshots (no-interpolation-inline-snapshot) | ||
|
||
Prevents the use of string interpolations within `toMatchInlineSnapshot`. | ||
|
||
## Rule Details | ||
|
||
Interpolation prevents snapshots from being updated. Instead, properties should | ||
be overloaded with a matcher by passing the optional `propertyMatchers` argument | ||
to `toMatchInlineSnapshot`. | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```js | ||
expect(something).toMatchInlineSnapshot( | ||
`Object { | ||
property: ${interpolated} | ||
}`, | ||
); | ||
|
||
expect(something).toMatchInlineSnapshot( | ||
{ other: expect.any(Number) }, | ||
`Object { | ||
other: Any<Number>, | ||
property: ${interpolated} | ||
}`, | ||
); | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```js | ||
expect(something).toMatchInlineSnapshot(); | ||
|
||
expect(something).toMatchInlineSnapshot( | ||
`Object { | ||
property: 1 | ||
}`, | ||
); | ||
|
||
expect(something).toMatchInlineSnapshot( | ||
{ property: expect.any(Date) }, | ||
`Object { | ||
property: Any<Date> | ||
}`, | ||
); | ||
``` | ||
|
||
\*Note that this rule will not trigger if the helper function is never used even | ||
thought the `expect` will not execute. Rely on a rule like no-unused-vars for | ||
this case. | ||
|
||
## When Not To Use It | ||
|
||
Don't use this rule on non-jest test files. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/rules/__tests__/no-interpolation-inline-snapshot.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { TSESLint } from '@typescript-eslint/experimental-utils'; | ||
import resolveFrom from 'resolve-from'; | ||
import rule from '../no-interpolation-inline-snapshot'; | ||
|
||
const ruleTester = new TSESLint.RuleTester({ | ||
parser: resolveFrom(require.resolve('eslint'), 'espree'), | ||
parserOptions: { | ||
ecmaVersion: 2017, | ||
}, | ||
}); | ||
|
||
ruleTester.run('no-interpolation-inline-snapshot', rule, { | ||
valid: [ | ||
'expect(something).toMatchInlineSnapshot();', | ||
'expect(something).toMatchInlineSnapshot(`No interpolation`);', | ||
'expect(something).toMatchInlineSnapshot({}, `No interpolation`);', | ||
], | ||
invalid: [ | ||
{ | ||
code: 'expect(something).toMatchInlineSnapshot(`${interpolated}`);', | ||
errors: [ | ||
{ | ||
endColumn: 58, | ||
column: 41, | ||
messageId: 'noInterpolation', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'expect(something).toMatchInlineSnapshot({}, `${interpolated}`);', | ||
errors: [ | ||
{ | ||
endColumn: 62, | ||
column: 45, | ||
messageId: 'noInterpolation', | ||
}, | ||
], | ||
}, | ||
], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { AST_NODE_TYPES } from '@typescript-eslint/experimental-utils'; | ||
import { createRule } from './utils'; | ||
|
||
export default createRule({ | ||
name: __filename, | ||
meta: { | ||
docs: { | ||
category: 'Best Practices', | ||
description: 'Disallow string interpolation inside inline snapshots.', | ||
recommended: false, | ||
}, | ||
messages: { | ||
noInterpolation: | ||
'Do not use string interpolation inside of inline snapshots', | ||
}, | ||
schema: [], | ||
type: 'suggestion', | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
return { | ||
CallExpression(node) { | ||
const { callee, arguments: nodeArguments } = node; | ||
|
||
if ( | ||
callee.type !== AST_NODE_TYPES.MemberExpression || | ||
callee.property.type !== AST_NODE_TYPES.Identifier || | ||
callee.property.name !== 'toMatchInlineSnapshot' | ||
) { | ||
return; | ||
} | ||
|
||
// Check all since the optional 'propertyMatchers' argument might be present | ||
nodeArguments.forEach(argument => { | ||
if ( | ||
argument.type === AST_NODE_TYPES.TemplateLiteral && | ||
argument.expressions.length > 0 | ||
) { | ||
context.report({ | ||
messageId: 'noInterpolation', | ||
node: argument, | ||
}); | ||
} | ||
}); | ||
}, | ||
}; | ||
}, | ||
}); |