Skip to content

Instantly share code, notes, and snippets.

@Martinnord
Created April 11, 2023 15:41
Show Gist options
  • Save Martinnord/8c10b292f267818261124a8729337a3c to your computer and use it in GitHub Desktop.
Save Martinnord/8c10b292f267818261124a8729337a3c to your computer and use it in GitHub Desktop.
// Import required Node.js modules
const { exec } = require("child_process");
const { promisify } = require("util");
const fs = require("fs").promises;
// Promisify the exec function to use with async/await
const execAsync = promisify(exec);
(async () => {
try {
// Find all test files with a '.test.ts' extension in the 'src' directory
const { stdout: filesStdout } = await execAsync('find ./src -type f -iname "*.test.ts"');
const files = filesStdout.trim().split("\n");
let totalTests = 0;
// Iterate through each test file
for (const file of files) {
// Read the content of the test file
const fileContent = await fs.readFile(file, "utf-8");
// Count 'it', 'test', and 'it.each' function calls
const testCalls = (fileContent.match(/\s*(test|it|it\.each)\s*\(/g) || []).length;
// Count test cases within each 'it.each' block
const testCasesInEach = (fileContent.match(/it\.each[^`]*`[^`]*`/g) || []).length;
// Calculate the total test count for the current file
const testCount = testCalls + testCasesInEach;
// Update the total test count for the project
totalTests += testCount;
// Output the test count for the current file
console.log(`File: ${file} - Test cases: ${testCount}`);
console.log(`Current total number of Jest tests: ${totalTests}\n`);
}
// Output the final total number of Jest tests
console.log(`\nFinal total number of Jest tests: ${totalTests}`);
} catch (err) {
// Output any errors that occurred during execution
console.error(`Error: ${err}`);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment