source: frontend/node_modules/eslint-plugin-jest/lib/rules/no-large-snapshots.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 2 weeks ago

Fix frontend appearance

  • Property mode set to 100644
File size: 3.5 KB
RevLine 
[9af201e]1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = void 0;
7
8var _path = require("path");
9
10var _experimentalUtils = require("@typescript-eslint/experimental-utils");
11
12var _utils = require("./utils");
13
14const reportOnViolation = (context, node, {
15 maxSize: lineLimit = 50,
16 allowedSnapshots = {}
17}) => {
18 const startLine = node.loc.start.line;
19 const endLine = node.loc.end.line;
20 const lineCount = endLine - startLine;
21 const allPathsAreAbsolute = Object.keys(allowedSnapshots).every(_path.isAbsolute);
22
23 if (!allPathsAreAbsolute) {
24 throw new Error('All paths for allowedSnapshots must be absolute. You can use JS config and `path.resolve`');
25 }
26
27 let isAllowed = false;
28
29 if (node.type === _experimentalUtils.AST_NODE_TYPES.ExpressionStatement && 'left' in node.expression && (0, _utils.isExpectMember)(node.expression.left)) {
30 const fileName = context.getFilename();
31 const allowedSnapshotsInFile = allowedSnapshots[fileName];
32
33 if (allowedSnapshotsInFile) {
34 const snapshotName = (0, _utils.getAccessorValue)(node.expression.left.property);
35 isAllowed = allowedSnapshotsInFile.some(name => {
36 if (name instanceof RegExp) {
37 return name.test(snapshotName);
38 }
39
40 return snapshotName === name;
41 });
42 }
43 }
44
45 if (!isAllowed && lineCount > lineLimit) {
46 context.report({
47 messageId: lineLimit === 0 ? 'noSnapshot' : 'tooLongSnapshots',
48 data: {
49 lineLimit,
50 lineCount
51 },
52 node
53 });
54 }
55};
56
57var _default = (0, _utils.createRule)({
58 name: __filename,
59 meta: {
60 docs: {
61 category: 'Best Practices',
62 description: 'disallow large snapshots',
63 recommended: false
64 },
65 messages: {
66 noSnapshot: '`{{ lineCount }}`s should begin with lowercase',
67 tooLongSnapshots: 'Expected Jest snapshot to be smaller than {{ lineLimit }} lines but was {{ lineCount }} lines long'
68 },
69 type: 'suggestion',
70 schema: [{
71 type: 'object',
72 properties: {
73 maxSize: {
74 type: 'number'
75 },
76 inlineMaxSize: {
77 type: 'number'
78 },
79 allowedSnapshots: {
80 type: 'object',
81 additionalProperties: {
82 type: 'array'
83 }
84 }
85 },
86 additionalProperties: false
87 }]
88 },
89 defaultOptions: [{}],
90
91 create(context, [options]) {
92 if (context.getFilename().endsWith('.snap')) {
93 return {
94 ExpressionStatement(node) {
95 reportOnViolation(context, node, options);
96 }
97
98 };
99 }
100
101 return {
102 CallExpression(node) {
103 var _matcher$arguments;
104
105 if (!(0, _utils.isExpectCall)(node)) {
106 return;
107 }
108
109 const {
110 matcher
111 } = (0, _utils.parseExpectCall)(node);
112
113 if ((matcher === null || matcher === void 0 ? void 0 : matcher.node.parent.type) !== _experimentalUtils.AST_NODE_TYPES.CallExpression) {
114 return;
115 }
116
117 if (['toMatchInlineSnapshot', 'toThrowErrorMatchingInlineSnapshot'].includes(matcher.name) && (_matcher$arguments = matcher.arguments) !== null && _matcher$arguments !== void 0 && _matcher$arguments.length) {
118 var _options$inlineMaxSiz;
119
120 reportOnViolation(context, matcher.arguments[0], { ...options,
121 maxSize: (_options$inlineMaxSiz = options.inlineMaxSize) !== null && _options$inlineMaxSiz !== void 0 ? _options$inlineMaxSiz : options.maxSize
122 });
123 }
124 }
125
126 };
127 }
128
129});
130
131exports.default = _default;
Note: See TracBrowser for help on using the repository browser.