|
| 1 | +/*! |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +/** |
| 7 | + * Scans test files for skipped Mocha tests. |
| 8 | + * |
| 9 | + * It uses a regex instead of mocha's loader, because mocha's |
| 10 | + * loader can't resolve vscode by default |
| 11 | + * |
| 12 | + * Note: This script doesn't handle cases where teams use this.skip() inside of their tests |
| 13 | + * |
| 14 | + * Usage: node skippedTestReport.js <directoryPath> |
| 15 | + */ |
| 16 | + |
| 17 | +import * as fs from 'fs' |
| 18 | +import * as path from 'path' |
| 19 | + |
| 20 | +interface SkippedTest { |
| 21 | + file: string |
| 22 | + testName: string |
| 23 | + lineNumber: number |
| 24 | +} |
| 25 | + |
| 26 | +function findSkippedTests(directoryPath: string): SkippedTest[] { |
| 27 | + const skippedTests: SkippedTest[] = [] |
| 28 | + |
| 29 | + const skipPatterns = [/\b(describe|it)\.skip\(['"`](.*?)['"`]/g] |
| 30 | + |
| 31 | + function searchInFile(filePath: string): void { |
| 32 | + try { |
| 33 | + const content = fs.readFileSync(filePath, 'utf8') |
| 34 | + const lines = content.split('\n') |
| 35 | + |
| 36 | + lines.forEach((line, index) => { |
| 37 | + for (const pattern of skipPatterns) { |
| 38 | + const matches = line.matchAll(pattern) |
| 39 | + for (const match of matches) { |
| 40 | + skippedTests.push({ |
| 41 | + file: filePath, |
| 42 | + testName: match[2], |
| 43 | + lineNumber: index + 1, |
| 44 | + }) |
| 45 | + } |
| 46 | + } |
| 47 | + }) |
| 48 | + } catch (error) { |
| 49 | + console.error(`Error reading file ${filePath}:`, error) |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + function visitDirectory(currentPath: string): void { |
| 54 | + const files = fs.readdirSync(currentPath) |
| 55 | + |
| 56 | + files.forEach((file) => { |
| 57 | + const fullPath = path.join(currentPath, file) |
| 58 | + const stat = fs.statSync(fullPath) |
| 59 | + |
| 60 | + if (stat.isDirectory()) { |
| 61 | + // Skip hidden directories |
| 62 | + if (!file.startsWith('.')) { |
| 63 | + visitDirectory(fullPath) |
| 64 | + } |
| 65 | + } else if (stat.isFile() && file.endsWith('.ts')) { |
| 66 | + searchInFile(fullPath) |
| 67 | + } |
| 68 | + }) |
| 69 | + } |
| 70 | + |
| 71 | + visitDirectory(directoryPath) |
| 72 | + return skippedTests |
| 73 | +} |
| 74 | + |
| 75 | +function main() { |
| 76 | + const targetDirectory = process.argv[2] || '.' |
| 77 | + |
| 78 | + try { |
| 79 | + const skippedTests = findSkippedTests(targetDirectory) |
| 80 | + |
| 81 | + if (skippedTests.length === 0) { |
| 82 | + console.log('No skipped tests found.') |
| 83 | + return |
| 84 | + } |
| 85 | + |
| 86 | + const testsByFile = skippedTests.reduce( |
| 87 | + (acc, test) => { |
| 88 | + const file = test.file |
| 89 | + if (!acc[file]) { |
| 90 | + acc[file] = [] |
| 91 | + } |
| 92 | + acc[file].push(test) |
| 93 | + return acc |
| 94 | + }, |
| 95 | + {} as Record<string, SkippedTest[]> |
| 96 | + ) |
| 97 | + |
| 98 | + console.log('\nSkipped Tests Report') |
| 99 | + console.log(`Total skipped tests: ${skippedTests.length}`) |
| 100 | + console.log(`Files affected: ${Object.keys(testsByFile).length}`) |
| 101 | + console.log('===================\n') |
| 102 | + |
| 103 | + Object.entries(testsByFile).forEach(([file, tests]) => { |
| 104 | + console.log(`📁 ${file}`) |
| 105 | + console.log(' Skipped tests:') |
| 106 | + tests.forEach((test) => { |
| 107 | + console.log(` • ${test.testName} (line ${test.lineNumber})`) |
| 108 | + }) |
| 109 | + console.log('') |
| 110 | + }) |
| 111 | + } catch (error) { |
| 112 | + console.error('Error:', error) |
| 113 | + process.exit(1) |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +main() |
0 commit comments