From 3f4796dd57174e3723da1d36e2aaf0e58c9cbfdf Mon Sep 17 00:00:00 2001 From: Danielku15 Date: Fri, 13 Sep 2024 10:04:46 +0200 Subject: [PATCH] fix: resolve mocha binary from package.json --- src/configurationFile.ts | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/configurationFile.ts b/src/configurationFile.ts index 2defbb8..b196dba 100644 --- a/src/configurationFile.ts +++ b/src/configurationFile.ts @@ -93,7 +93,7 @@ export class ConfigurationFile implements vscode.Disposable { } async getMochaSpawnArgs(customArgs: readonly string[]): Promise { - this._pathToMocha ??= await this._resolveLocalMochaPath('/bin/mocha.js'); + this._pathToMocha ??= await this._resolveLocalMochaBinPath(); return [ await getPathToNode(this.logChannel), @@ -133,11 +133,29 @@ export class ConfigurationFile implements vscode.Disposable { throw new HumanError(`Could not find node_modules above '${mocha}'`); } - private _resolveLocalMochaPath(suffix?: string): Promise { + private async _resolveLocalMochaBinPath(): Promise { + try { + const packageJsonPath = await this._resolveLocalMochaPath('/package.json'); + const packageJson = JSON.parse(await fs.promises.readFile(packageJsonPath, 'utf-8')); + let binPath = packageJson?.bin?.mocha; + if (binPath) { + binPath = path.join(path.dirname(packageJsonPath), binPath); + await fs.promises.access(binPath); + return binPath; + } + } catch (e) { + // ignore + } + + this.logChannel.warn('Could not resolve mocha bin path from package.json, fallback to default'); + return await this._resolveLocalMochaPath('/bin/mocha.js'); + } + + private _resolveLocalMochaPath(suffix: string = ''): Promise { return new Promise((resolve, reject) => { const dir = path.dirname(this.uri.fsPath); this.logChannel.debug(`resolving 'mocha${suffix}' via ${dir}`); - this.getResolver().resolve({}, dir, 'mocha' + (suffix ?? ''), {}, (err, res) => { + this.getResolver().resolve({}, dir, 'mocha' + suffix, {}, (err, res) => { if (err) { this.logChannel.error(`resolving 'mocha${suffix}' failed with error ${err}`); reject(