UNPKG

@atlaskit/eslint-plugin-design-system

Version:

The essential plugin for use with the Atlassian Design System.

60 lines (59 loc) 2.44 kB
import { isNodeOfType } from 'eslint-codemod-utils'; import { createLintRule } from '../utils/create-lint-rule'; export var name = 'no-to-match-snapshot'; var rule = createLintRule({ meta: { name: name, type: 'problem', docs: { description: 'Disallow using toMatchSnapshot() and toMatchInlineSnapshot() in unit tests. Snapshot assertions should be replaced with explicit assertions.', recommended: false, severity: 'error' }, messages: { avoidSnapshots: 'Avoid snapshot matchers in unit tests. Replace toMatchSnapshot()/toMatchInlineSnapshot() with explicit assertions.' } }, create: function create(context) { var snapshotMatchers = new Set(['toMatchSnapshot', 'toMatchInlineSnapshot']); var _getStaticString = function getStaticString(node) { if (!node) { return null; } if (node.type === 'Literal' && typeof node.value === 'string') { return node.value; } if (node.type === 'BinaryExpression' && node.operator === '+') { var left = _getStaticString(node.left); var right = _getStaticString(node.right); return left !== null && right !== null ? left + right : null; } return null; }; var isExpectCall = function isExpectCall(node) { return isNodeOfType(node, 'CallExpression') && isNodeOfType(node.callee, 'Identifier') && node.callee.name === 'expect'; }; return { CallExpression: function CallExpression(node) { // Handle expect(x).toMatchSnapshot() and expect(x).toMatchInlineSnapshot() if (isNodeOfType(node.callee, 'MemberExpression') && !node.callee.computed && isNodeOfType(node.callee.property, 'Identifier') && isExpectCall(node.callee.object) && snapshotMatchers.has(node.callee.property.name)) { context.report({ node: node.callee.property, messageId: 'avoidSnapshots' }); } // Handle expect(x)['toMatch' + 'Snapshot']() / ['toMatch' + 'InlineSnapshot'] if (isNodeOfType(node.callee, 'MemberExpression') && node.callee.computed && isExpectCall(node.callee.object)) { var prop = _getStaticString(node.callee.property); if (prop && snapshotMatchers.has(prop)) { context.report({ node: node.callee.property, messageId: 'avoidSnapshots' }); } } } }; } }); export default rule;