Advertisement
djbob2000

Untitled

Apr 4th, 2025
370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //custom-eslint-rules/run-eslint-tests.ts
  2. import { readdirSync } from "fs";
  3.  
  4. console.log("Running all ESLint rule tests...\n");
  5.  
  6. // Find all test files in the current directory
  7. const testFiles = readdirSync(".")
  8.     .filter((file) => file.endsWith(".test.ts"))
  9.     .map((file) => "./" + file.replace(".ts", ".js"));
  10.  
  11. // Import and run test files
  12. await Promise.all(testFiles.map((file) => import(file)));
  13.  
  14. export {};
  15.  
  16.  
  17. //custom-eslint-rules/test-utils/eslint-rule-tester.ts
  18. import { RuleTester } from "@typescript-eslint/rule-tester";
  19. import parser from "@typescript-eslint/parser";
  20.  
  21. // Console colors
  22. const colors = {
  23.     green: "\x1b[32m",
  24.     red: "\x1b[31m",
  25.     yellow: "\x1b[33m",
  26.     blue: "\x1b[34m",
  27.     reset: "\x1b[0m",
  28.     bold: "\x1b[1m",
  29. };
  30.  
  31. interface TestStats {
  32.     passedTests: number;
  33.     failedTests: number;
  34.     totalTests: number;
  35. }
  36.  
  37. export class ESLintRuleTestRunner {
  38.     private stats: TestStats = {
  39.         passedTests: 0,
  40.         failedTests: 0,
  41.         totalTests: 0,
  42.     };
  43.  
  44.     private ruleTester: RuleTester;
  45.  
  46.     constructor() {
  47.         // Setup test runner
  48.         RuleTester.afterAll = () => {};
  49.         RuleTester.describe = (_name, fn) => fn();
  50.         RuleTester.it = (name, fn) => {
  51.             this.stats.totalTests++;
  52.             try {
  53.                 fn();
  54.                 console.log(`${colors.green}✓${colors.reset} ${name}`);
  55.                 this.stats.passedTests++;
  56.             } catch (error: any) {
  57.                 console.log(`${colors.red}✗${colors.reset} ${name}`);
  58.                 console.log(`${colors.red}Error: ${error.message}${colors.reset}\n`);
  59.                 this.stats.failedTests++;
  60.                 throw error;
  61.             }
  62.         };
  63.  
  64.         // Create RuleTester instance
  65.         this.ruleTester = new RuleTester({
  66.             languageOptions: {
  67.                 parser,
  68.                 parserOptions: {
  69.                     ecmaVersion: 2022,
  70.                     sourceType: "module",
  71.                 },
  72.             },
  73.         });
  74.     }
  75.  
  76.     runTests(
  77.         ruleName: string,
  78.         rule: any,
  79.         testCases: {
  80.             valid?: { code: string }[];
  81.             invalid?: {
  82.                 code: string;
  83.                 errors: { messageId: string }[];
  84.                 output?: string;
  85.             }[];
  86.         }[],
  87.     ) {
  88.         console.log(`\n${colors.bold}Testing ESLint rule: ${colors.blue}${ruleName}${colors.reset}\n`);
  89.  
  90.         // Run valid cases
  91.         if (testCases.some((cases) => cases.valid?.length)) {
  92.             console.log(`${colors.bold}Testing Valid Cases (Should Pass):${colors.reset}`);
  93.             this.ruleTester.run(`${ruleName}-valid`, rule, {
  94.                 valid: testCases.flatMap((cases) => cases.valid || []),
  95.                 invalid: [],
  96.             });
  97.         }
  98.  
  99.         // Run invalid cases
  100.         if (testCases.some((cases) => cases.invalid?.length)) {
  101.             console.log(`\n${colors.bold}Testing Invalid Cases (Should Fail):${colors.reset}`);
  102.             this.ruleTester.run(`${ruleName}-invalid`, rule, {
  103.                 valid: [],
  104.                 invalid: testCases.flatMap((cases) => cases.invalid || []),
  105.             });
  106.         }
  107.  
  108.         // Print summary
  109.         this.printSummary();
  110.     }
  111.  
  112.     private printSummary() {
  113.         console.log(`\n${colors.bold}Test Summary:${colors.reset}`);
  114.         console.log(`${colors.green}✓${colors.reset} ${this.stats.passedTests} tests passed`);
  115.         console.log(`${colors.red}✗${colors.reset} ${this.stats.failedTests} tests failed`);
  116.         console.log(`${colors.blue}Total:${colors.reset} ${this.stats.totalTests} tests`);
  117.  
  118.         if (this.stats.failedTests > 0) {
  119.             console.log(`\n${colors.bold}${colors.red}Some tests failed!${colors.reset}\n`);
  120.             process.exit(1);
  121.         } else {
  122.             console.log(`\n${colors.bold}${colors.green}All tests passed successfully!${colors.reset}\n`);
  123.         }
  124.     }
  125. }
  126.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement