From 215c8ef71e8b7b9bb48f3dda6b2022b3b6088e9a 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/services/repository.ts | 2 +- src/utils/getTestFrameworkName.ts | 13 ++++++++- test/getTestFrameworkName.spec.ts | 45 +++++++++++++++++++++++++++---- 3 files changed, 53 insertions(+), 7 deletions(-) diff --git a/src/services/repository.ts b/src/services/repository.ts index 0fd8896..ea7004d 100644 --- a/src/services/repository.ts +++ b/src/services/repository.ts @@ -176,7 +176,7 @@ export default class Repository { license: (this.#repository.license || {}).name || "N/A", fork: this.#repository.fork, fork_count: this.#repository.forks_count, - test_framework: getTestFrameworkName(devDependencies), + test_framework: getTestFrameworkName(packageJSON), coverage_lib: getCoverageLib(devDependencies), size: this.#repository.size, stars: this.#repository.stargazers_count, diff --git a/src/utils/getTestFrameworkName.ts b/src/utils/getTestFrameworkName.ts index 2a34fe5..2460bb0 100644 --- a/src/utils/getTestFrameworkName.ts +++ b/src/utils/getTestFrameworkName.ts @@ -1,4 +1,15 @@ -export function getTestFrameworkName(deps = {}) { +// Import Third-party Dependencies +import type { PackageJSON } from "@nodesecure/npm-types"; + +// CONSTANTS +const kNodeTestRunnerRegex = /(?:node\s+--test|tsx)/; + +export function getTestFrameworkName(packageJson : PackageJSON) : string { + const { devDependencies: deps = {}, scripts = {} } = packageJson; + + if (kNodeTestRunnerRegex.test(scripts.test)) { + return "node:test"; + } if ("ava" in deps) { return "ava"; } diff --git a/test/getTestFrameworkName.spec.ts b/test/getTestFrameworkName.spec.ts index 4fca57b..229aef5 100644 --- a/test/getTestFrameworkName.spec.ts +++ b/test/getTestFrameworkName.spec.ts @@ -11,22 +11,57 @@ describe("getTestFrameworkName()", () => { }); it("Should return ava", () => { - assert.equal(getTestFrameworkName({ ava: "2.0.0" }), "ava"); + const packageJson = { + devDependencies: { + ava: "^2.0.0" + } + }; + assert.equal(getTestFrameworkName(packageJson), "ava"); }); it("Should return jest", () => { - assert.equal(getTestFrameworkName({ jest: "2.0.0" }), "jest"); + const packageJson = { + devDependencies: { + jest: "^2.0.0" + } + }; + assert.equal(getTestFrameworkName(packageJson), "jest"); }); it("Should return japa", () => { - assert.equal(getTestFrameworkName({ japa: "2.0.0" }), "japa"); + const packageJson = { + devDependencies: { + japa: "^2.0.0" + } + }; + assert.equal(getTestFrameworkName(packageJson), "japa"); }); it("Should return tape", () => { - assert.equal(getTestFrameworkName({ tape: "2.0.0" }), "tape"); + const packageJson = { + devDependencies: { + tape: "^2.0.0" + } + }; + assert.equal(getTestFrameworkName(packageJson), "tape"); }); it("Should return mocha", () => { - assert.equal(getTestFrameworkName({ mocha: "2.0.0" }), "mocha"); + const packageJson = { + devDependencies: { + mocha: "^2.0.0" + } + }; + + assert.equal(getTestFrameworkName(packageJson), "mocha"); + }); + + it("Should return node:test", () => { + const packageJson = { + scripts: { + test: "node --test" + } + }; + assert.equal(getTestFrameworkName(packageJson), "node:test"); }); });