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

feat(rules): add prefer-inline-snapshots #129

Merged
merged 3 commits into from Jul 17, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 30 additions & 0 deletions docs/rules/prefer-inline-snapshots.md
@@ -0,0 +1,30 @@
# Suggest using inline snapshots (prefer-inline-snapshots)

In order to make snapshot tests more managable and reviewable
`toMatchInlineSnapshot()` and `toThrowErrorMatchingInlineSnapshot` should be
used to write the snapshots inline in the test file.

## Rule details

This rule triggers a warning if `toMatchSnapshot()` or
`toThrowErrorMatchingSnapshot` is used to capture a snapshot.

The following pattern is considered warning:

```js
expect(obj).toMatchSnapshot();
```

```js
expect(error).toThrowErrorMatchingSnapshot();
```

The following pattern is not warning:

```js
expect(obj).toMatchInlineSnapshot();
```

```js
expect(error).toThrowErrorMatchingInlineSnapshot();
```
2 changes: 2 additions & 0 deletions index.js
Expand Up @@ -17,6 +17,7 @@ const validDescribe = require('./rules/valid-describe');
const validExpect = require('./rules/valid-expect');
const preferExpectAssertions = require('./rules/prefer-expect-assertions');
const validExpectInPromise = require('./rules/valid-expect-in-promise');
const preferInlineSnapshots = require('./rules/prefer-inline-snapshots');

const snapshotProcessor = require('./processors/snapshot-processor');

Expand Down Expand Up @@ -81,5 +82,6 @@ module.exports = {
'valid-expect': validExpect,
'prefer-expect-assertions': preferExpectAssertions,
'valid-expect-in-promise': validExpectInPromise,
'prefer-inline-snapshots': preferInlineSnapshots,
},
};
37 changes: 37 additions & 0 deletions rules/__tests__/prefer-inline-snapshots.test.js
@@ -0,0 +1,37 @@
'use strict';

const RuleTester = require('eslint').RuleTester;
const rule = require('../prefer-inline-snapshots');

const ruleTester = new RuleTester();

ruleTester.run('prefer-inline-snapshots', rule, {
valid: [
'expect(something).toMatchInlineSnapshot();',
'expect(something).toThrowErrorMatchingInlineSnapshot();',
],
invalid: [
{
code: 'expect(something).toMatchSnapshot();',
errors: [
{
message: 'Use toMatchInlineSnapshot() instead',
column: 19,
line: 1,
},
],
output: 'expect(something).toMatchInlineSnapshot();',
},
{
code: 'expect(something).toThrowErrorMatchingSnapshot();',
errors: [
{
message: 'Use toThrowErrorMatchingInlineSnapshot() instead',
column: 19,
line: 1,
},
],
output: 'expect(something).toThrowErrorMatchingInlineSnapshot();',
},
],
});
46 changes: 46 additions & 0 deletions rules/prefer-inline-snapshots.js
@@ -0,0 +1,46 @@
'use strict';

const getDocsUrl = require('./util').getDocsUrl;

module.exports = {
meta: {
docs: {
url: getDocsUrl(__filename),
},
fixable: 'code',
},
create(context) {
return {
CallExpression(node) {
const propertyName = node.callee.property && node.callee.property.name;
if (propertyName === 'toMatchSnapshot') {
context.report({
fix(fixer) {
return [
fixer.replaceText(
node.callee.property,
'toMatchInlineSnapshot'
),
];
},
message: 'Use toMatchInlineSnapshot() instead',
node: node.callee.property,
});
} else if (propertyName === 'toThrowErrorMatchingSnapshot') {
context.report({
fix(fixer) {
return [
fixer.replaceText(
node.callee.property,
'toThrowErrorMatchingInlineSnapshot'
),
];
},
message: 'Use toThrowErrorMatchingInlineSnapshot() instead',
node: node.callee.property,
});
}
},
};
},
};