From 625697f9ac3443568a353edf310c04d690374463 Mon Sep 17 00:00:00 2001 From: Kouadio Fabrice Nguessan Date: Mon, 5 Aug 2024 16:10:51 +0000 Subject: [PATCH] refactor(getTestFrameworkName.js): detect node test runner --- src/utils/getTestFrameworkName.ts | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/utils/getTestFrameworkName.ts b/src/utils/getTestFrameworkName.ts index 2a34fe5..4127684 100644 --- a/src/utils/getTestFrameworkName.ts +++ b/src/utils/getTestFrameworkName.ts @@ -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"; } @@ -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); +} +