-
Notifications
You must be signed in to change notification settings - Fork 129
/
Copy pathjestRunnerConfig.ts
178 lines (151 loc) · 6.12 KB
/
jestRunnerConfig.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import * as path from 'path';
import * as fs from 'fs';
import * as vscode from 'vscode';
import { normalizePath, quote, validateCodeLensOptions, CodeLensOption, isNodeExecuteAbleFile } from './util';
export class JestRunnerConfig {
/**
* The command that runs jest.
* Defaults to: node "node_modules/.bin/jest"
*/
public get jestCommand(): string {
// custom
const jestCommand: string = vscode.workspace.getConfiguration().get('jestrunner.jestCommand');
if (jestCommand) {
return jestCommand;
}
// default
if (this.isYarnPnpSupportEnabled) {
return `yarn jest`;
}
return `node ${quote(this.jestBinPath)}`;
}
public get changeDirectoryToWorkspaceRoot(): boolean {
return vscode.workspace.getConfiguration().get('jestrunner.changeDirectoryToWorkspaceRoot');
}
public get preserveEditorFocus(): boolean {
return vscode.workspace.getConfiguration().get('jestrunner.preserveEditorFocus') || false;
}
public get jestBinPath(): string {
// custom
let jestPath: string = vscode.workspace.getConfiguration().get('jestrunner.jestPath');
if (jestPath) {
return jestPath;
}
// 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);
return normalizePath(jestPath);
}
public get projectPath(): string {
return this.projectPathFromConfig || this.currentWorkspaceFolderPath;
}
public get cwd(): string {
return this.projectPathFromConfig || this.currentPackagePath || this.currentWorkspaceFolderPath;
}
public get projectPathFromConfig(): string | undefined {
const projectPathFromConfig = vscode.workspace.getConfiguration().get<string>('jestrunner.projectPath');
if (projectPathFromConfig) {
return path.resolve(this.currentWorkspaceFolderPath, projectPathFromConfig);
}
}
private get currentPackagePath() {
let currentFolderPath: string = path.dirname(vscode.window.activeTextEditor.document.fileName);
do {
// 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');
if (fs.existsSync(pkg) && fs.existsSync(jest)) {
return currentFolderPath;
}
currentFolderPath = path.join(currentFolderPath, '..');
} while (currentFolderPath !== this.currentWorkspaceFolderPath);
return '';
}
public get currentWorkspaceFolderPath(): string {
const editor = vscode.window.activeTextEditor;
return vscode.workspace.getWorkspaceFolder(editor.document.uri).uri.fsPath;
}
public get jestConfigPath(): string {
// custom
const configPath: string = vscode.workspace.getConfiguration().get('jestrunner.configPath');
if (!configPath) {
return this.findConfigPath();
}
// default
return normalizePath(path.join(this.currentWorkspaceFolderPath, configPath));
}
getJestConfigPath(targetPath: string): string {
// custom
const configPath: string = vscode.workspace.getConfiguration().get('jestrunner.configPath');
if (!configPath) {
return this.findConfigPath(targetPath);
}
// default
return normalizePath(path.join(this.currentWorkspaceFolderPath, configPath));
}
private findConfigPath(targetPath?: string): string {
let currentFolderPath: string = targetPath || path.dirname(vscode.window.activeTextEditor.document.fileName);
let currentFolderConfigPath: string;
do {
for (const configFilename of ['jest.config.js', 'jest.config.ts', 'jest.config.cjs', 'jest.config.mjs']) {
currentFolderConfigPath = path.join(currentFolderPath, configFilename);
if (fs.existsSync(currentFolderConfigPath)) {
return currentFolderConfigPath;
}
}
currentFolderPath = path.join(currentFolderPath, '..');
} while (currentFolderPath !== this.currentWorkspaceFolderPath);
return '';
}
public get runOptions(): string[] | null {
const runOptions = vscode.workspace.getConfiguration().get('jestrunner.runOptions');
if (runOptions) {
if (Array.isArray(runOptions)) {
return runOptions;
} else {
vscode.window.showWarningMessage(
'Please check your vscode settings. "jestrunner.runOptions" must be an Array. '
);
}
}
return null;
}
public get debugOptions(): Partial<vscode.DebugConfiguration> {
const debugOptions = vscode.workspace.getConfiguration().get('jestrunner.debugOptions');
if (debugOptions) {
return debugOptions;
}
// default
return {};
}
public get isCodeLensDisabled(): boolean {
const isCodeLensDisabled: boolean = vscode.workspace.getConfiguration().get('jestrunner.disableCodeLens');
return isCodeLensDisabled ? isCodeLensDisabled : false;
}
public get isRunInExternalNativeTerminal(): boolean {
const isRunInExternalNativeTerminal: boolean = vscode.workspace
.getConfiguration()
.get('jestrunner.runInOutsideTerminal');
return isRunInExternalNativeTerminal ? isRunInExternalNativeTerminal : false;
}
public get codeLensOptions(): CodeLensOption[] {
const codeLensOptions = vscode.workspace.getConfiguration().get('jestrunner.codeLens');
if (Array.isArray(codeLensOptions)) {
return validateCodeLensOptions(codeLensOptions);
}
return [];
}
public get isYarnPnpSupportEnabled(): boolean {
const isYarnPnp: boolean = vscode.workspace.getConfiguration().get('jestrunner.enableYarnPnpSupport');
return isYarnPnp ? isYarnPnp : false;
}
public get getYarnPnpCommand(): string {
const yarnPnpCommand: string = vscode.workspace.getConfiguration().get('jestrunner.yarnPnpCommand');
return yarnPnpCommand;
}
}