-
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: create
no-interpolation-in-snapshots
rule (#553)
* feat(rules): add no-interpolation-inline-snapshot rule * docs: fix fromat in rule documentation * fix: add chamges requested in review * test: update number of rules * docs: update docs/rules/no-interpolation-inline-snapshot.md Co-authored-by: Gareth Jones <[email protected]> * feat: add toThrowErrorMatchingInlineSnapshot * docs: update rules table in README * docs: apply suggestions from code review Co-authored-by: Gareth Jones <[email protected]> * docs: fix format in README * fix: add review suggestions * docs: add examples to README * chore: rename rule to no-interpolation-in-snapshots * chore: remove changes from CHANGELOG.md * docs: update rule table with new name * fix: apply suggestions from code review Co-authored-by: Gareth Jones <[email protected]> Co-authored-by: Simen Bekkhus <[email protected]> * test: add test to cover missing branch * docs: regenerate table with the new script * fix: edit rule description Co-authored-by: Gareth Jones <[email protected]> Co-authored-by: Simen Bekkhus <[email protected]>
- Loading branch information
1 parent
00523e9
commit 8d2c17c
Showing
6 changed files
with
251 additions
and
42 deletions.
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
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,60 @@ | ||
# Disallow string interpolation inside snapshots (`no-interpolation-in-snapshots`) | ||
|
||
Prevents the use of string interpolations in snapshots. | ||
|
||
## Rule Details | ||
|
||
Interpolation prevents snapshots from being updated. Instead, properties should | ||
be overloaded with a matcher by using | ||
[property matchers](https://jestjs.io/docs/en/snapshot-testing#property-matchers). | ||
|
||
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} | ||
}`, | ||
); | ||
|
||
expect(errorThrowingFunction).toThrowErrorMatchingInlineSnapshot( | ||
`${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> | ||
}`, | ||
); | ||
|
||
expect(errorThrowingFunction).toThrowErrorMatchingInlineSnapshot(); | ||
|
||
expect(errorThrowingFunction).toThrowErrorMatchingInlineSnapshot( | ||
`Error Message`, | ||
); | ||
``` | ||
|
||
## 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
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,93 @@ | ||
import { TSESLint } from '@typescript-eslint/experimental-utils'; | ||
import resolveFrom from 'resolve-from'; | ||
import rule from '../no-interpolation-in-snapshots'; | ||
|
||
const ruleTester = new TSESLint.RuleTester({ | ||
parser: resolveFrom(require.resolve('eslint'), 'espree'), | ||
parserOptions: { | ||
ecmaVersion: 2017, | ||
}, | ||
}); | ||
|
||
ruleTester.run('no-interpolation-in-snapshots', rule, { | ||
valid: [ | ||
'expect("something").toEqual("else");', | ||
'expect(something).toMatchInlineSnapshot();', | ||
'expect(something).toMatchInlineSnapshot(`No interpolation`);', | ||
'expect(something).toMatchInlineSnapshot({}, `No interpolation`);', | ||
'expect(something);', | ||
'expect(something).not;', | ||
'expect.toHaveAssertions();', | ||
'myObjectWants.toMatchInlineSnapshot({}, `${interpolated}`);', | ||
'myObjectWants.toMatchInlineSnapshot({}, `${interpolated1} ${interpolated2}`);', | ||
'toMatchInlineSnapshot({}, `${interpolated}`);', | ||
'toMatchInlineSnapshot({}, `${interpolated1} ${interpolated2}`);', | ||
'expect(something).toThrowErrorMatchingInlineSnapshot();', | ||
'expect(something).toThrowErrorMatchingInlineSnapshot(`No interpolation`);', | ||
], | ||
invalid: [ | ||
{ | ||
code: 'expect(something).toMatchInlineSnapshot(`${interpolated}`);', | ||
errors: [ | ||
{ | ||
endColumn: 58, | ||
column: 41, | ||
messageId: 'noInterpolation', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'expect(something).not.toMatchInlineSnapshot(`${interpolated}`);', | ||
errors: [ | ||
{ | ||
endColumn: 62, | ||
column: 45, | ||
messageId: 'noInterpolation', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'expect(something).toMatchInlineSnapshot({}, `${interpolated}`);', | ||
errors: [ | ||
{ | ||
endColumn: 62, | ||
column: 45, | ||
messageId: 'noInterpolation', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: | ||
'expect(something).not.toMatchInlineSnapshot({}, `${interpolated}`);', | ||
errors: [ | ||
{ | ||
endColumn: 66, | ||
column: 49, | ||
messageId: 'noInterpolation', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: | ||
'expect(something).toThrowErrorMatchingInlineSnapshot(`${interpolated}`);', | ||
errors: [ | ||
{ | ||
endColumn: 71, | ||
column: 54, | ||
messageId: 'noInterpolation', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: | ||
'expect(something).not.toThrowErrorMatchingInlineSnapshot(`${interpolated}`);', | ||
errors: [ | ||
{ | ||
endColumn: 75, | ||
column: 58, | ||
messageId: 'noInterpolation', | ||
}, | ||
], | ||
}, | ||
], | ||
}); |
Oops, something went wrong.