2020-08-26 01:57:08 +02:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
|
|
value: true
|
|
|
|
});
|
|
|
|
exports.default = void 0;
|
|
|
|
var _path = require("path");
|
2022-11-10 11:43:16 +01:00
|
|
|
var _utils = require("@typescript-eslint/utils");
|
|
|
|
var _utils2 = require("./utils");
|
2020-08-26 01:57:08 +02:00
|
|
|
const reportOnViolation = (context, node, {
|
|
|
|
maxSize: lineLimit = 50,
|
2021-02-26 04:58:33 +01:00
|
|
|
allowedSnapshots = {}
|
2020-08-26 01:57:08 +02:00
|
|
|
}) => {
|
|
|
|
const startLine = node.loc.start.line;
|
|
|
|
const endLine = node.loc.end.line;
|
|
|
|
const lineCount = endLine - startLine;
|
|
|
|
const allPathsAreAbsolute = Object.keys(allowedSnapshots).every(_path.isAbsolute);
|
|
|
|
if (!allPathsAreAbsolute) {
|
|
|
|
throw new Error('All paths for allowedSnapshots must be absolute. You can use JS config and `path.resolve`');
|
|
|
|
}
|
|
|
|
let isAllowed = false;
|
2022-11-10 11:43:16 +01:00
|
|
|
if (node.type === _utils.AST_NODE_TYPES.ExpressionStatement && 'left' in node.expression && node.expression.left.type === _utils.AST_NODE_TYPES.MemberExpression && (0, _utils2.isSupportedAccessor)(node.expression.left.property)) {
|
2020-08-26 01:57:08 +02:00
|
|
|
const fileName = context.getFilename();
|
|
|
|
const allowedSnapshotsInFile = allowedSnapshots[fileName];
|
|
|
|
if (allowedSnapshotsInFile) {
|
2022-11-10 11:43:16 +01:00
|
|
|
const snapshotName = (0, _utils2.getAccessorValue)(node.expression.left.property);
|
2020-08-26 01:57:08 +02:00
|
|
|
isAllowed = allowedSnapshotsInFile.some(name => {
|
|
|
|
if (name instanceof RegExp) {
|
|
|
|
return name.test(snapshotName);
|
|
|
|
}
|
|
|
|
return snapshotName === name;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!isAllowed && lineCount > lineLimit) {
|
|
|
|
context.report({
|
|
|
|
messageId: lineLimit === 0 ? 'noSnapshot' : 'tooLongSnapshots',
|
|
|
|
data: {
|
|
|
|
lineLimit,
|
|
|
|
lineCount
|
|
|
|
},
|
|
|
|
node
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2022-11-10 11:43:16 +01:00
|
|
|
var _default = (0, _utils2.createRule)({
|
2020-08-26 01:57:08 +02:00
|
|
|
name: __filename,
|
|
|
|
meta: {
|
|
|
|
docs: {
|
|
|
|
category: 'Best Practices',
|
2022-11-10 11:43:16 +01:00
|
|
|
description: 'Disallow large snapshots',
|
2020-08-26 01:57:08 +02:00
|
|
|
recommended: false
|
|
|
|
},
|
|
|
|
messages: {
|
|
|
|
noSnapshot: '`{{ lineCount }}`s should begin with lowercase',
|
|
|
|
tooLongSnapshots: 'Expected Jest snapshot to be smaller than {{ lineLimit }} lines but was {{ lineCount }} lines long'
|
|
|
|
},
|
|
|
|
type: 'suggestion',
|
|
|
|
schema: [{
|
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
maxSize: {
|
|
|
|
type: 'number'
|
|
|
|
},
|
|
|
|
inlineMaxSize: {
|
|
|
|
type: 'number'
|
|
|
|
},
|
|
|
|
allowedSnapshots: {
|
|
|
|
type: 'object',
|
|
|
|
additionalProperties: {
|
|
|
|
type: 'array'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
additionalProperties: false
|
|
|
|
}]
|
|
|
|
},
|
|
|
|
defaultOptions: [{}],
|
|
|
|
create(context, [options]) {
|
|
|
|
if (context.getFilename().endsWith('.snap')) {
|
|
|
|
return {
|
|
|
|
ExpressionStatement(node) {
|
|
|
|
reportOnViolation(context, node, options);
|
|
|
|
}
|
|
|
|
};
|
2021-02-26 04:58:33 +01:00
|
|
|
}
|
|
|
|
return {
|
|
|
|
CallExpression(node) {
|
2022-11-10 11:43:16 +01:00
|
|
|
const jestFnCall = (0, _utils2.parseJestFnCall)(node, context);
|
|
|
|
if ((jestFnCall === null || jestFnCall === void 0 ? void 0 : jestFnCall.type) !== 'expect') {
|
2021-02-26 04:58:33 +01:00
|
|
|
return;
|
|
|
|
}
|
2022-11-10 11:43:16 +01:00
|
|
|
if (['toMatchInlineSnapshot', 'toThrowErrorMatchingInlineSnapshot'].includes((0, _utils2.getAccessorValue)(jestFnCall.matcher)) && jestFnCall.args.length) {
|
|
|
|
reportOnViolation(context, jestFnCall.args[0], {
|
|
|
|
...options,
|
|
|
|
maxSize: options.inlineMaxSize ?? options.maxSize
|
2021-02-26 04:58:33 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2020-08-26 01:57:08 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
exports.default = _default;
|