-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First stab at a Node.js CLI wrapping the Mocha Remote client (#212)
* Improve client errors * Start implementing a "mocha-remote-node" CLI
- Loading branch information
1 parent
ac0b16d
commit 3cb6471
Showing
8 changed files
with
200 additions
and
12 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/usr/bin/env node | ||
import "./dist/cli.js" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"name": "mocha-remote-node", | ||
"version": "1.12.3", | ||
"type": "module", | ||
"description": "Node.js wrapper for the Mocha Remote Client", | ||
"bin": "./mocha-remote-node.js", | ||
"scripts": { | ||
"start": "tsx src/cli.ts", | ||
"build": "tsc" | ||
}, | ||
"dependencies": { | ||
"glob": "^10.4.5" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import os from "node:os"; | ||
import { globSync } from "glob"; | ||
import { Client } from "mocha-remote-client"; | ||
|
||
/* eslint-disable no-console */ | ||
|
||
// TODO: Add support for existing Mocha runtime options for "file handling" | ||
|
||
/* | ||
* File Handling | ||
* --extension File extension(s) to load | ||
* [array] [default: ["js","cjs","mjs"]] | ||
* --file Specify file(s) to be loaded prior to root suite | ||
* execution [array] [default: (none)] | ||
* --ignore, --exclude Ignore file(s) or glob pattern(s) | ||
* [array] [default: (none)] | ||
* --recursive Look for tests in subdirectories [boolean] | ||
* -r, --require Require module [array] [default: (none)] | ||
* -S, --sort Sort test files [boolean] | ||
* -w, --watch Watch files in the current working directory for | ||
* changes [boolean] | ||
* --watch-files List of paths or globs to watch [array] | ||
* --watch-ignore List of paths or globs to exclude from watching | ||
* [array] [default: ["node_modules",".git"]] | ||
*/ | ||
|
||
interface ConnectionRefusedError extends AggregateError { | ||
errors: { code: "ECONNREFUSED", address: string, port: number }[]; | ||
} | ||
|
||
function isConnectionRefusedError(error: unknown): error is ConnectionRefusedError { | ||
return error instanceof AggregateError && error.errors.every((error: unknown) => { | ||
return error instanceof Error && | ||
"code" in error && error.code === "ECONNREFUSED" && | ||
"port" in error && typeof error.port === "number"; | ||
}); | ||
} | ||
|
||
const client = new Client({ | ||
title: `Node.js v${process.versions.node} on ${os.platform()}`, | ||
autoConnect: false, | ||
autoReconnect: false, | ||
async tests(context) { | ||
Object.assign(global, { | ||
environment: { ...context, node: true }, | ||
}); | ||
// TODO: Is there a more reliable way to get the interpreter and command skipped? | ||
const [, , patterns] = process.argv; | ||
const testPaths = globSync(patterns, { absolute: true }); | ||
for (const testPath of testPaths) { | ||
await import(testPath); | ||
} | ||
}, | ||
}); | ||
|
||
try { | ||
await client.connect(); | ||
} catch (error) { | ||
process.exitCode = 1; | ||
if (isConnectionRefusedError(error)) { | ||
const attempts = error.errors.map(error => `${error.address}:${error.port}`); | ||
const command = "npx mocha-remote -- mocha-remote-node src/*.test.ts"; | ||
const suggestion = "Are you wrapping the mocha-remote-node CLI with the mocha-remote?"; | ||
console.error(`Connection refused (tried ${attempts.join(" / ")}).\n${suggestion}\n${command}`); | ||
} else if (error instanceof Error) { | ||
console.error("Mocha Remote Client failed:", error.stack); | ||
} else { | ||
console.error("Mocha Remote Client failed:", error); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"extends": ["@tsconfig/node16"], | ||
"compilerOptions": { | ||
"outDir": "dist", | ||
"target": "es2022", | ||
"lib": ["es2022"] | ||
}, | ||
"include": ["src"] | ||
} |