Skip to content

Updated detection of Node and Japa test runners #96

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [18.x]
node-version: [20.x]
fail-fast: false
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
Expand Down
41 changes: 38 additions & 3 deletions src/utils/getTestFrameworkName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
import type { PackageJSON } from "@nodesecure/npm-types";

// CONSTANTS
const kNodeTestRunnerRegex = /(?:node\s+--test|tsx)/;
const kNodeTestRunnerRegex = /[node|tsx]\s+.*--test/;

export function getTestFrameworkName(packageJson: PackageJSON): string {
const { devDependencies: deps = {}, scripts = {} } = packageJson;

if (kNodeTestRunnerRegex.test(scripts.test)) {
if (isUsingNodeTestRunner(scripts, "test")) {
return "node:test";
}
if ("ava" in deps) {
Expand All @@ -16,7 +16,7 @@ export function getTestFrameworkName(packageJson: PackageJSON): string {
if ("jest" in deps) {
return "jest";
}
if ("japa" in deps) {
if ("@japa/runner" in deps) {
return "japa";
}
if ("tape" in deps) {
Expand All @@ -28,3 +28,38 @@ export function getTestFrameworkName(packageJson: PackageJSON): string {

return "N/A";
}

function isUsingNodeTestRunner(scripts: Record<string, string>, scriptName: string, visited = new Set<string>()): boolean {
if (visited.has(scriptName)) {
return false;
}

const scriptCommand = scripts[scriptName];
if (scriptCommand && kNodeTestRunnerRegex.test(scriptCommand)) {
return true;
}

const calledScripts = extractCalledScripts(scriptCommand);

for (const calledScript of calledScripts) {
if (isUsingNodeTestRunner(scripts, calledScript, visited)) {
return true;
}
}

return false;
}

function extractCalledScripts(scriptCommand: string): string[] {
const npmRunRegex = /npm run (\S+)/g;

const calledScripts: string[] = [];

let match: RegExpExecArray | null;

while ((match = npmRunRegex.exec(scriptCommand)) !== null) {
calledScripts.push(match[1]);
}

return calledScripts;
}
68 changes: 59 additions & 9 deletions test/getTestFrameworkName.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import { getTestFrameworkName } from "../src/utils";

describe("getTestFrameworkName()", () => {
it("Should return N/A", () => {
assert.equal(getTestFrameworkName({ test: "2.0.0", test2: "3.0.0" }), "N/A");
assert.equal(getTestFrameworkName({ name: "test", version: "1.0.0", test: "2.0.0", test2: "3.0.0" }), "N/A");
});

it("Should return ava", () => {
const packageJson = {
name: "test",
version: "1.0.0",
devDependencies: {
ava: "^2.0.0"
}
Expand All @@ -21,6 +23,8 @@ describe("getTestFrameworkName()", () => {

it("Should return jest", () => {
const packageJson = {
name: "test",
version: "1.0.0",
devDependencies: {
jest: "^2.0.0"
}
Expand All @@ -30,15 +34,19 @@ describe("getTestFrameworkName()", () => {

it("Should return japa", () => {
const packageJson = {
name: "test",
version: "1.0.0",
devDependencies: {
japa: "^2.0.0"
"@japa/runner": "^2.0.0"
}
};
assert.equal(getTestFrameworkName(packageJson), "japa");
});

it("Should return tape", () => {
const packageJson = {
name: "test",
version: "1.0.0",
devDependencies: {
tape: "^2.0.0"
}
Expand All @@ -48,6 +56,8 @@ describe("getTestFrameworkName()", () => {

it("Should return mocha", () => {
const packageJson = {
name: "test",
version: "1.0.0",
devDependencies: {
mocha: "^2.0.0"
}
Expand All @@ -56,12 +66,52 @@ describe("getTestFrameworkName()", () => {
assert.equal(getTestFrameworkName(packageJson), "mocha");
});

it("Should return node:test", () => {
const packageJson = {
scripts: {
test: "node --test"
}
};
assert.equal(getTestFrameworkName(packageJson), "node:test");
describe("node:test", () => {
it("Should return node:test for test script", () => {
const packageJson = {
name: "test",
version: "1.0.0",
scripts: {
test: "node --test"
}
};
assert.equal(getTestFrameworkName(packageJson), "node:test");
});

it("Should return node:test when a nested script uses node --test", () => {
const packageJson = {
name: "test",
version: "1.0.0",
scripts: {
test: "c8 --all --src ./src -r html npm run test-only",
"test-only": "glob -c \"node --loader=esmock --no-warnings --test-concurrency 1 --test\" \"test/**/*.test.js\""
}
};
assert.equal(getTestFrameworkName(packageJson), "node:test");
});

it("Should return node:test when a nested script uses node --test, recursively", () => {
const packageJson = {
name: "test",
version: "1.0.0",
scripts: {
test: "npm run foo",
foo: "npm run bar",
bar: "node --test"
}
};
assert.equal(getTestFrameworkName(packageJson), "node:test");
});

it("Should return node:test for test script when using tsx", () => {
const packageJson = {
name: "test",
version: "1.0.0",
scripts: {
test: "tsx --test"
}
};
assert.equal(getTestFrameworkName(packageJson), "node:test");
});
});
});
Loading