From b5a13fdcc953a240833f2df2fb0495ff20b69eb4 Mon Sep 17 00:00:00 2001 From: Tryggvi Gylfason Date: Tue, 17 Jul 2018 10:11:09 +0200 Subject: [PATCH] feat(rules): add prefer-inline-snapshots (#129) --- README.md | 2 + docs/rules/prefer-inline-snapshots.md | 30 ++++++++++++ index.js | 2 + .../__tests__/prefer-inline-snapshots.test.js | 37 +++++++++++++++ rules/prefer-inline-snapshots.js | 46 +++++++++++++++++++ 5 files changed, 117 insertions(+) create mode 100644 docs/rules/prefer-inline-snapshots.md create mode 100644 rules/__tests__/prefer-inline-snapshots.test.js create mode 100644 rules/prefer-inline-snapshots.js diff --git a/README.md b/README.md index 973880042..ff3e001a1 100644 --- a/README.md +++ b/README.md @@ -94,6 +94,7 @@ for more information about extending configuration files. | [prefer-to-be-null][] | Suggest using `toBeNull()` | | ![fixable-green][] | | [prefer-to-be-undefined][] | Suggest using `toBeUndefined()` | | ![fixable-green][] | | [prefer-to-have-length][] | Suggest using `toHaveLength()` | ![recommended][] | ![fixable-green][] | +| [prefer-inline-snapshots][] | Suggest using `toMatchInlineSnapshot()` | | ![fixable-green][] | | [valid-describe][] | Enforce valid `describe()` callback | | | | [valid-expect-in-promise][] | Enforce having return statement when testing with promises | | | | [valid-expect][] | Enforce valid `expect()` usage | ![recommended][] | | @@ -117,6 +118,7 @@ for more information about extending configuration files. [prefer-to-be-null]: docs/rules/prefer-to-be-null.md [prefer-to-be-undefined]: docs/rules/prefer-to-be-undefined.md [prefer-to-have-length]: docs/rules/prefer-to-have-length.md +[prefer-inline-snapshots]: docs/rules/prefer-inline-snapshots.md [valid-describe]: docs/rules/valid-describe.md [valid-expect-in-promise]: docs/rules/valid-expect-in-promise.md [valid-expect]: docs/rules/valid-expect.md diff --git a/docs/rules/prefer-inline-snapshots.md b/docs/rules/prefer-inline-snapshots.md new file mode 100644 index 000000000..319419f52 --- /dev/null +++ b/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(); +``` diff --git a/index.js b/index.js index 3e7da34d3..0d7e3e483 100644 --- a/index.js +++ b/index.js @@ -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'); @@ -81,5 +82,6 @@ module.exports = { 'valid-expect': validExpect, 'prefer-expect-assertions': preferExpectAssertions, 'valid-expect-in-promise': validExpectInPromise, + 'prefer-inline-snapshots': preferInlineSnapshots, }, }; diff --git a/rules/__tests__/prefer-inline-snapshots.test.js b/rules/__tests__/prefer-inline-snapshots.test.js new file mode 100644 index 000000000..6f70e3134 --- /dev/null +++ b/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();', + }, + ], +}); diff --git a/rules/prefer-inline-snapshots.js b/rules/prefer-inline-snapshots.js new file mode 100644 index 000000000..ad93f2a30 --- /dev/null +++ b/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, + }); + } + }, + }; + }, +};