Skip to content
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

feat: nodePath & mochaPath #210

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ This extension automatically discovers and works with the `.mocharc.js/cjs/yaml/

- `mocha-vscode.env`: Additional environment variables set when executing tests. This is useful for setting things like `NODE_ENV`.

- `mocha-vscode.mochaPath`: Specifies the path to the Mocha executable. If not set, the extension will attempt to resolve the Mocha executable from the local `node_modules` directory.
- `mocha-vscode.nodePath`: Specifies the path to the Node.js executable. If not set, the extension will use the default Node.js executable.

## Features

### Show, Running and Debugging Tests
Expand Down
12 changes: 11 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@
"type": "object",
"additionalProperties": true,
"markdownDescription": "Additional environment variables set when executing tests. This is useful for setting things like `NODE_ENV`."
},
"mocha-vscode.nodePath": {
"type": "string",
"default": "",
"description": "Path to the Node.js executable."
},
"mocha-vscode.mochaPath": {
"type": "string",
"default": "",
"description": "Path to the Mocha executable."
}
}
}
Expand Down Expand Up @@ -205,4 +215,4 @@
"version": "v1.2.3+d27e65f",
"date": "2024-10-20T15:38:24.261Z"
}
}
}
3 changes: 2 additions & 1 deletion src/configurationFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ export class ConfigurationFile implements vscode.Disposable {
}

async getMochaSpawnArgs(customArgs: readonly string[]): Promise<string[]> {
this._pathToMocha ??= await this._resolveLocalMochaBinPath();
const mochaPath = vscode.workspace.getConfiguration('mocha-vscode').get<string>('mochaPath');
this._pathToMocha = mochaPath || (await this._resolveLocalMochaBinPath());
Comment on lines +96 to +97
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change breaks proper caching of the variable and cause re-evaluation on every spawn of mocha. We should keep the caching for the locally resolved path as it can be expensive in some scenarios.


return [
await getPathToNode(this.logChannel),
Expand Down
7 changes: 7 additions & 0 deletions src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ export async function getPathTo(logChannel: vscode.LogOutputChannel, bin: string
let pathToNode: string | null = null;

export async function getPathToNode(logChannel: vscode.LogOutputChannel) {
// Check if the nodePath setting is defined
const nodePath = vscode.workspace.getConfiguration('mocha-vscode').get<string>('nodePath');
if (nodePath) {
logChannel.debug(`Using nodePath from settings: '${nodePath}'`);
return nodePath;
}
Comment on lines +28 to +33
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your issue only talks about redirecting Mocha to anoter tooling. What's the real usecase behind redirecting node as well? Depending on your need also NPM would need support for reconfiguration.


// We cannot use process.execPath as this points to code.exe which is an electron application
// also with ELECTRON_RUN_AS_NODE this can lead to errors (e.g. with the --import option)
// we prefer to use the system level node
Expand Down