Skip to content

Commit

Permalink
#296 | @WillWillman | debug currentPackagePath
Browse files Browse the repository at this point in the history
  • Loading branch information
WillWillman committed Aug 29, 2023
1 parent e3f7c76 commit 0a21956
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/jestRunnerConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,26 @@ 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 {
Expand All @@ -65,12 +70,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;
}
Expand Down

0 comments on commit 0a21956

Please sign in to comment.