From b631c2359091628a816098d2a3cc7f838cc47563 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jenifer=20L=C3=B3pez?= Date: Tue, 15 Oct 2024 12:21:32 +0200 Subject: [PATCH] feat(packages/lint-repository-sui): Add jest adoption rule to lint --- packages/lint-repository-sui/src/index.js | 4 +- .../src/rules/jest-adoption.js | 45 +++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 packages/lint-repository-sui/src/rules/jest-adoption.js diff --git a/packages/lint-repository-sui/src/index.js b/packages/lint-repository-sui/src/index.js index b103f77cc..6f8db7b1a 100644 --- a/packages/lint-repository-sui/src/index.js +++ b/packages/lint-repository-sui/src/index.js @@ -11,6 +11,7 @@ const TSvsJS = require('./rules/ts-vs-js-files.js') const Sass = require('./rules/sass-files.js') const Spark = require('./rules/spark-adoption.js') const ComponentsLocation = require('./rules/components-location.js') +const JestAdoption = require('./rules/jest-adoption.js') // ------------------------------------------------------------------------------ // Plugin Definition @@ -31,6 +32,7 @@ module.exports = { 'ts-vs-js-files': TSvsJS, 'sass-files': Sass, 'spark-adoption': Spark, - 'components-location': ComponentsLocation + 'components-location': ComponentsLocation, + 'jest-adoption': JestAdoption } } diff --git a/packages/lint-repository-sui/src/rules/jest-adoption.js b/packages/lint-repository-sui/src/rules/jest-adoption.js new file mode 100644 index 000000000..e0d76f3d4 --- /dev/null +++ b/packages/lint-repository-sui/src/rules/jest-adoption.js @@ -0,0 +1,45 @@ +const dedent = require('string-dedent') + +module.exports = { + meta: { + type: 'problem', + docs: { + description: 'This metric reports the number component that use Jest', + recommended: true, + url: null + }, + fixable: null, + schema: [], + messages: { + percentage: dedent` + Currently, {{percentage}}% of your components use Jest. We have {{totalKarmaTests}}% tests in Karma and {{totalJestTests}}% in Jest + ` + } + }, + + create: function (context) { + return { + 'components/**/(test|__tests__)/*.(j|t)s(x)?': matches => { + const totalTests = matches.length + const totalJestTests = matches.filter(({fullPath}) => fullPath.includes('__tests__')).length + const totalKarmaTests = totalTests - totalJestTests + let percentage = (totalJestTests * 100) / totalTests + percentage = Math.round((percentage + Number.EPSILON) * 100) / 100 + + context.report({ + messageId: 'percentage', + data: {percentage, totalKarmaTests, totalJestTests} + }) + return context.monitoring(percentage) + }, + + missmatch: key => { + context.report({ + messageId: 'percentage', + data: {percentage: 0} + }) + context.monitoring(0) + } + } + } +}