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 215c8ef
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/services/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
13 changes: 12 additions & 1 deletion src/utils/getTestFrameworkName.ts
Original file line number Diff line number Diff line change
@@ -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";
}
Expand Down
45 changes: 40 additions & 5 deletions test/getTestFrameworkName.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
});

0 comments on commit 215c8ef

Please sign in to comment.