Skip to content

Commit

Permalink
refactor(getTestFrameworkName.js): detect node test runner
Browse files Browse the repository at this point in the history
  • Loading branch information
fabnguess committed Aug 5, 2024
1 parent 6159d52 commit 625697f
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/utils/getTestFrameworkName.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
// Import Node.js Dependencies
import fs from "fs";
import path from "path";

export function getTestFrameworkName(deps = {}) {
if (IsNodeTestingUsed()) {
return "node:test";
}
if ("ava" in deps) {
return "ava";
}
Expand All @@ -17,3 +24,19 @@ export function getTestFrameworkName(deps = {}) {

return "N/A";
}

function IsNodeTestingUsed() {
const [majorVersion] = process.versions.node.split(".").map(Number);
if (majorVersion <= 18) {
return false;
}

const nodeTestRegex = /(?:node\s+--test|tsx)/;

const packageJsonPath = path.join(process.cwd(), "package.json");

const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf-8"));

return nodeTestRegex.test(packageJson.scripts.test);
}

0 comments on commit 625697f

Please sign in to comment.