Skip to content

Commit 7a73a5f

Browse files
committed
adding existing jest.nodeEnv into debug config
1 parent 8cec4c9 commit 7a73a5f

9 files changed

+326
-139
lines changed

src/DebugConfigurationProvider.ts

+50-35
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,17 @@ import {
1111
toAbsoluteRootPath,
1212
} from './helpers';
1313
import { platform } from 'os';
14+
import { PluginResourceSettings } from './Settings';
1415

1516
export const DEBUG_CONFIG_PLATFORMS = ['windows', 'linux', 'osx'];
1617
const testNamePatternRegex = /\$\{jest.testNamePattern\}/g;
1718
const testFileRegex = /\$\{jest.testFile\}/g;
1819
const testFilePatternRegex = /\$\{jest.testFilePattern\}/g;
20+
21+
export type DebugConfigOptions = Partial<
22+
Pick<PluginResourceSettings, 'jestCommandLine' | 'rootPath' | 'nodeEnv'>
23+
>;
24+
type PartialDebugConfig = Partial<vscode.DebugConfiguration>;
1925
export class DebugConfigurationProvider implements vscode.DebugConfigurationProvider {
2026
private fileNameToRun = '';
2127
private testToRun = '';
@@ -176,7 +182,7 @@ export class DebugConfigurationProvider implements vscode.DebugConfigurationProv
176182
}
177183

178184
/** return a config if cmd is a package-manager */
179-
private usePM(cmd: string, args: string[]): Partial<vscode.DebugConfiguration | undefined> {
185+
private usePM(cmd: string, args: string[]): PartialDebugConfig | undefined {
180186
const commonConfig = {
181187
program: undefined,
182188
};
@@ -191,49 +197,58 @@ export class DebugConfigurationProvider implements vscode.DebugConfigurationProv
191197
}
192198

193199
/**
194-
* generate a debug config incorporating commandLine and rootPath. Throw exception if error.
195-
* @param cmdLine
196-
* @param rootPath
197-
* @returns a debug config.
200+
* Creates a debug configuration for a given workspace.
201+
*
202+
* @param {vscode.WorkspaceFolder} workspace - The workspace folder for which the debug configuration is created.
203+
* @param {DebugConfigOptions} [options] - Optional parameters to override the default debug configuration.
204+
* @returns {vscode.DebugConfiguration} The final debug configuration.
205+
*
206+
* @throws {Error} If the provided jestCommandLine is invalid.
207+
*
208+
* This function customizes the default debug configuration with the settings from the options parameter,
209+
* such as `rootPath`, `jestCommandLine`, and `nodeEnv`.
210+
* Please note, the platform-specific settings that were not converted are removed.
198211
*/
199-
withCommandLine(
212+
createDebugConfig(
200213
workspace: vscode.WorkspaceFolder,
201-
cmdLine: string,
202-
rootPath?: string
214+
options?: DebugConfigOptions
203215
): vscode.DebugConfiguration {
204216
const config = this.provideDebugConfigurations(workspace)[0];
205-
const [cmd, ...cmdArgs] = parseCmdLine(cmdLine);
206-
if (!cmd) {
207-
throw new Error(`invalid cmdLine: ${cmdLine}`);
208-
}
209-
210-
const absoluteRootPath = rootPath && toAbsoluteRootPath(workspace, rootPath);
211-
212-
let finalConfig: vscode.DebugConfiguration = { ...config };
217+
let args: string[] = [];
218+
let override: PartialDebugConfig = {};
213219

220+
const absoluteRootPath = options?.rootPath && toAbsoluteRootPath(workspace, options.rootPath);
214221
const cwd = absoluteRootPath ?? config.cwd;
215222

216-
const pmConfig = this.usePM(cmd, cmdArgs);
217-
if (pmConfig) {
218-
const args = [...cmdArgs, ...pmConfig.args, ...config.args];
219-
finalConfig = {
220-
...finalConfig,
221-
...pmConfig,
222-
cwd,
223-
args,
224-
};
225-
} else {
226-
// convert the cmd to absolute path
227-
let program = path.isAbsolute(cmd)
228-
? cmd
229-
: absoluteRootPath
230-
? path.resolve(absoluteRootPath, cmd)
231-
: ['${workspaceFolder}', cmd].join(path.sep);
232-
program = this.adjustProgram(program);
233-
const args = [...cmdArgs, ...config.args];
234-
finalConfig = { ...finalConfig, cwd, program, args };
223+
// handle jestCommandLine related overrides
224+
if (options?.jestCommandLine) {
225+
const [cmd, ...cmdArgs] = parseCmdLine(options.jestCommandLine);
226+
if (!cmd) {
227+
throw new Error(`invalid cmdLine: ${options.jestCommandLine}`);
228+
}
229+
const pmConfig = this.usePM(cmd, cmdArgs);
230+
if (pmConfig) {
231+
args = [...cmdArgs, ...pmConfig.args, ...config.args];
232+
override = { ...pmConfig, args };
233+
} else {
234+
let program = path.isAbsolute(cmd)
235+
? cmd
236+
: absoluteRootPath
237+
? path.resolve(absoluteRootPath, cmd)
238+
: ['${workspaceFolder}', cmd].join(path.sep);
239+
program = this.adjustProgram(program);
240+
args = [...cmdArgs, ...config.args];
241+
override = { program, args };
242+
}
235243
}
236244

245+
//handle nodeEnv
246+
if (options?.nodeEnv) {
247+
override = { env: options.nodeEnv, ...override };
248+
}
249+
250+
const finalConfig: vscode.DebugConfiguration = { ...config, cwd, ...override };
251+
237252
// delete platform specific settings since we did not convert them
238253
DEBUG_CONFIG_PLATFORMS.forEach((p) => delete finalConfig[p]);
239254

src/JestExt/core.ts

+5-11
Original file line numberDiff line numberDiff line change
@@ -605,17 +605,11 @@ export class JestExt {
605605
'debug',
606606
'No debug config named "vscode-jest-tests.v2" or "vscode-jest-tests" found in launch.json, will use a default config.'
607607
);
608-
if (this.extContext.settings.jestCommandLine) {
609-
debugConfig = this.debugConfigurationProvider.withCommandLine(
610-
this.extContext.workspace,
611-
this.extContext.settings.jestCommandLine,
612-
this.extContext.settings.rootPath
613-
);
614-
} else {
615-
debugConfig = this.debugConfigurationProvider.provideDebugConfigurations(
616-
this.extContext.workspace
617-
)[0];
618-
}
608+
debugConfig = this.debugConfigurationProvider.createDebugConfig(this.extContext.workspace, {
609+
jestCommandLine: this.extContext.settings.jestCommandLine,
610+
rootPath: this.extContext.settings.rootPath,
611+
nodeEnv: this.extContext.settings.nodeEnv,
612+
});
619613

620614
this.debugConfig = debugConfig;
621615
this.extContext.output.write('auto config debug config:', 'info');

src/setup-wizard/tasks/setup-jest-debug.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,11 @@ export const setupJestDebug: SetupTask = async (context: WizardContext): Promise
110110
}
111111
}
112112

113-
const debugConfig = debugConfigProvider.withCommandLine(workspace, jestCommandLine, rootPath);
113+
const debugConfig = debugConfigProvider.createDebugConfig(workspace, {
114+
jestCommandLine,
115+
rootPath,
116+
nodeEnv: settings.nodeEnv,
117+
});
114118
message('generated a debug config with jestCommandLine and rootPath:', 'info');
115119
message(`${JSON.stringify(debugConfig, undefined, ' ')}`, 'new-line');
116120

src/setup-wizard/types.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as vscode from 'vscode';
2-
import { DebugConfigurationProvider } from '../DebugConfigurationProvider';
2+
import { DebugConfigurationProvider, DebugConfigOptions } from '../DebugConfigurationProvider';
33
import { JestExtOutput } from '../JestExt/output-terminal';
44
import { WorkspaceManager } from '../workspace-manager';
55

@@ -57,9 +57,7 @@ export interface ActionInputBoxOptions<T> extends AllowBackButton, Verbose {
5757
export type SetupTask = (context: WizardContext) => Promise<WizardStatus>;
5858

5959
// settings
60-
export interface WizardSettings {
61-
jestCommandLine?: string;
62-
rootPath?: string;
60+
export interface WizardSettings extends DebugConfigOptions {
6361
absoluteRootPath?: string;
6462
configurations?: vscode.DebugConfiguration[];
6563
}

src/setup-wizard/wizard-helper.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {
2020
isActionableButton,
2121
WizardContext,
2222
} from './types';
23-
import { VirtualFolderSettings, createJestSettingGetter } from '../Settings';
23+
import { NodeEnv, VirtualFolderSettings, createJestSettingGetter } from '../Settings';
2424
import { existsSync } from 'fs';
2525
import { parseCmdLine, removeSurroundingQuote, toAbsoluteRootPath } from '../helpers';
2626
import { VirtualWorkspaceFolder, isVirtualWorkspaceFolder } from '../virtual-workspace-folder';
@@ -269,6 +269,7 @@ export const getWizardSettings = (workspaceFolder: vscode.WorkspaceFolder): Wiza
269269
const wsSettings: WizardSettings = {
270270
jestCommandLine: getSetting<string>('jestCommandLine')?.trim() || undefined,
271271
rootPath: getSetting<string>('rootPath')?.trim() || undefined,
272+
nodeEnv: getSetting<NodeEnv>('nodeEnv') || undefined,
272273
};
273274

274275
// populate jest settings

0 commit comments

Comments
 (0)