From 52a7dc0be8257eba31a982466db6c133b8d8dfd9 Mon Sep 17 00:00:00 2001 From: WillWillman Date: Tue, 29 Aug 2023 15:35:54 -0700 Subject: [PATCH] #296 | @willwillman | debug currentPackagePath --- src/jestRunnerConfig.ts | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/src/jestRunnerConfig.ts b/src/jestRunnerConfig.ts index 5b2b632..7e7c806 100644 --- a/src/jestRunnerConfig.ts +++ b/src/jestRunnerConfig.ts @@ -31,21 +31,27 @@ export class JestRunnerConfig { } public get jestBinPath(): string { - // custom - let jestPath: string = vscode.workspace.getConfiguration().get('jestrunner.jestPath'); - if (jestPath) { - return jestPath; + const customJestPath: string = vscode.workspace.getConfiguration().get('jestrunner.jestPath'); + if (customJestPath) { + return customJestPath; + } + + // using separate key for possible backwards compatibility issues + const customJestPathFromWorkspace: string = vscode.workspace.getConfiguration().get('jestrunner.jestPathFromWorkspace'); + if (customJestPathFromWorkspace) { + return path.join(this.currentWorkspaceFolderPath, customJestPathFromWorkspace); } + // default const fallbackRelativeJestBinPath = 'node_modules/jest/bin/jest.js'; const mayRelativeJestBin = ['node_modules/.bin/jest', 'node_modules/jest/bin/jest.js']; const cwd = this.cwd; - jestPath = mayRelativeJestBin.find((relativeJestBin) => isNodeExecuteAbleFile(path.join(cwd, relativeJestBin))); - jestPath = jestPath || path.join(cwd, fallbackRelativeJestBinPath); + const jestPath = mayRelativeJestBin.find((relativeJestBin) => isNodeExecuteAbleFile(path.join(cwd, relativeJestBin))); + const defaultJestPath = path.join(cwd, fallbackRelativeJestBinPath); - return normalizePath(jestPath); + return normalizePath(jestPath || defaultJestPath); } public get projectPath(): string { @@ -65,12 +71,16 @@ export class JestRunnerConfig { private get currentPackagePath() { let currentFolderPath: string = path.dirname(vscode.window.activeTextEditor.document.fileName); + const customJestPath: string = vscode.workspace.getConfiguration().get('jestrunner.jestPath'); + const customJestPathFromWorkspace: string = vscode.workspace.getConfiguration().get('jestrunner.jestPathFromWorkspace'); + const customJestPathFromWorkspaceWithRootPath: string = customJestPathFromWorkspace && path.join(this.currentWorkspaceFolderPath, customJestPathFromWorkspace); do { + const defaultJestPath: string = path.join(currentFolderPath, 'node_modules', 'jest'); // Try to find where jest is installed relatively to the current opened file. // Do not assume that jest is always installed at the root of the opened project, this is not the case // such as in multi-module projects. const pkg = path.join(currentFolderPath, 'package.json'); - const jest = path.join(currentFolderPath, 'node_modules', 'jest'); + const jest = customJestPath || customJestPathFromWorkspaceWithRootPath || defaultJestPath; if (fs.existsSync(pkg) && fs.existsSync(jest)) { return currentFolderPath; }