index.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. "use strict";
  2. var _fs = require("fs");
  3. var _path = require("path");
  4. var _globals = _interopRequireDefault(require("./globals.json"));
  5. var snapshotProcessor = _interopRequireWildcard(require("./processors/snapshot-processor"));
  6. function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
  7. function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  9. // copied from https://github.com/babel/babel/blob/d8da63c929f2d28c401571e2a43166678c555bc4/packages/babel-helpers/src/helpers.js#L602-L606
  10. /* istanbul ignore next */
  11. const interopRequireDefault = obj => obj && obj.__esModule ? obj : {
  12. default: obj
  13. };
  14. const importDefault = moduleName => // eslint-disable-next-line @typescript-eslint/no-require-imports
  15. interopRequireDefault(require(moduleName)).default;
  16. const rulesDir = (0, _path.join)(__dirname, 'rules');
  17. const excludedFiles = ['__tests__', 'detectJestVersion', 'utils'];
  18. const rules = (0, _fs.readdirSync)(rulesDir).map(rule => (0, _path.parse)(rule).name).filter(rule => !excludedFiles.includes(rule)).reduce((acc, curr) => ({ ...acc,
  19. [curr]: importDefault((0, _path.join)(rulesDir, curr))
  20. }), {});
  21. const recommendedRules = Object.entries(rules).filter(([, rule]) => rule.meta.docs.recommended).reduce((acc, [name, rule]) => ({ ...acc,
  22. [`jest/${name}`]: rule.meta.docs.recommended
  23. }), {});
  24. const allRules = Object.entries(rules).filter(([, rule]) => !rule.meta.deprecated).reduce((acc, [name]) => ({ ...acc,
  25. [`jest/${name}`]: 'error'
  26. }), {});
  27. const createConfig = rules => ({
  28. plugins: ['jest'],
  29. env: {
  30. 'jest/globals': true
  31. },
  32. rules
  33. });
  34. module.exports = {
  35. configs: {
  36. all: createConfig(allRules),
  37. recommended: createConfig(recommendedRules),
  38. style: {
  39. plugins: ['jest'],
  40. rules: {
  41. 'jest/no-alias-methods': 'warn',
  42. 'jest/prefer-to-be': 'error',
  43. 'jest/prefer-to-contain': 'error',
  44. 'jest/prefer-to-have-length': 'error'
  45. }
  46. }
  47. },
  48. environments: {
  49. globals: {
  50. globals: _globals.default
  51. }
  52. },
  53. processors: {
  54. '.snap': snapshotProcessor
  55. },
  56. rules
  57. };